python_fabm.F90 23.1 KB
Newer Older
Jorn Bruggeman's avatar
Jorn Bruggeman committed
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "fabm_driver.h"
!-----------------------------------------------------------------------
!BOP
!
! !MODULE: Python interface to the Framework for Aquatic Biogeochemical Models (FABM)
!
! !INTERFACE:
   module fabm_python
!
! !DESCRIPTION:
! TODO
!
! !USES:
14
   use iso_c_binding, only: c_double, c_int, c_char, C_NULL_CHAR, c_f_pointer, c_loc, c_ptr
15 16 17

   !DIR$ ATTRIBUTES DLLEXPORT :: STATE_VARIABLE,DIAGNOSTIC_VARIABLE,CONSERVED_QUANTITY

Jorn Bruggeman's avatar
Jorn Bruggeman committed
18 19
   use fabm
   use fabm_config
Jorn Bruggeman's avatar
Jorn Bruggeman committed
20 21
   use fabm_types, only:rk,attribute_length,type_model_list_node,type_base_model, &
                        factory,type_link,type_link_list,type_internal_variable
22
   use fabm_driver, only: type_base_driver, driver
23
   use fabm_properties
Jorn Bruggeman's avatar
Jorn Bruggeman committed
24
   use fabm_python_helper
25
   use fabm_c_helper
Jorn Bruggeman's avatar
Jorn Bruggeman committed
26 27 28 29 30 31

   implicit none
!
! !PUBLIC MEMBER FUNCTIONS:
   public

32 33 34 35 36 37 38
   integer,parameter :: BULK_STATE_VARIABLE            = 1
   integer,parameter :: SURFACE_STATE_VARIABLE         = 2
   integer,parameter :: BOTTOM_STATE_VARIABLE          = 3
   integer,parameter :: BULK_DIAGNOSTIC_VARIABLE       = 4
   integer,parameter :: HORIZONTAL_DIAGNOSTIC_VARIABLE = 5
   integer,parameter :: CONSERVED_QUANTITY             = 6

39
   class (type_model),private,pointer,save :: model => null()
40
   real(8),dimension(:),pointer :: state
Jorn Bruggeman's avatar
Jorn Bruggeman committed
41
   character(len=1024),dimension(:),allocatable :: environment_names,environment_units
42
   type (type_link_list),save :: coupling_link_list
Jorn Bruggeman's avatar
Jorn Bruggeman committed
43

44
   type (type_property_dictionary),save,private :: forced_parameters,forced_couplings
45 46 47

   type,extends(type_base_driver) :: type_python_driver
   contains
48 49
      procedure :: fatal_error => python_driver_fatal_error
      procedure :: log_message => python_driver_log_message
50
   end type
Jorn Bruggeman's avatar
Jorn Bruggeman committed
51 52 53 54 55
!EOP
!-----------------------------------------------------------------------

   contains

56 57 58 59 60 61 62 63 64 65 66 67
   subroutine get_version(length,version_string) bind(c)
!DIR$ ATTRIBUTES DLLEXPORT :: get_version
      use fabm_version, only: fabm_commit_id=>git_commit_id, &
                              fabm_branch_name=>git_branch_name

      integer(c_int),value,intent(in) :: length
      character(kind=c_char)          :: version_string(length)

      call copy_to_c_string(fabm_commit_id//' ('//fabm_branch_name//' branch)', version_string)
   end subroutine get_version


Jorn Bruggeman's avatar
Jorn Bruggeman committed
68 69 70 71 72 73
!-----------------------------------------------------------------------
!BOP
!
! !IROUTINE: Initialise the model
!
! !INTERFACE:
74
   subroutine initialize(path) bind(c)
75
!DIR$ ATTRIBUTES DLLEXPORT :: initialize
76
      character(kind=c_char),target,intent(in) :: path(*)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
77 78 79 80 81 82 83 84
!
! !DESCRIPTION:
!
! !REVISION HISTORY:
!  Original author(s): Jorn Bruggeman
!
!EOP
!
85
      character(len=attribute_length),pointer :: ppath
86
      class (type_property),          pointer :: property
Jorn Bruggeman's avatar
Jorn Bruggeman committed
87 88
!-----------------------------------------------------------------------
!BOC
89 90
      call c_f_pointer(c_loc(path), ppath)

91
      if (associated(model)) call finalize()
Jorn Bruggeman's avatar
Jorn Bruggeman committed
92

93 94
      if (.not.associated(driver)) allocate(type_python_driver::driver)

Jorn Bruggeman's avatar
Jorn Bruggeman committed
95
      ! Build FABM model tree (configuration will be read from fabm.yaml).
96 97 98
      allocate(model)
      call fabm_create_model_from_yaml_file(model,path=ppath(:index(ppath,C_NULL_CHAR)-1),parameters=forced_parameters)

99
      ! Get a list of all parameters that had an explicit value specified.
100 101
      property => model%root%parameters%first
      do while (associated(property))
102
         if (.not.model%root%parameters%missing%contains(property%name)) &
103 104 105
            call forced_parameters%set_property(property)
         property => property%next
      end do
Jorn Bruggeman's avatar
Jorn Bruggeman committed
106

107 108 109 110 111 112 113 114
      ! Get a list of all active couplings.
      property => model%root%couplings%first
      do while (associated(property))
         if (.not.model%root%couplings%missing%contains(property%name)) &
            call forced_couplings%set_property(property)
         property => property%next
      end do

Jorn Bruggeman's avatar
Jorn Bruggeman committed
115 116 117
      ! Send information on spatial domain to FABM (this also allocates memory for diagnostics)
      call fabm_set_domain(model)

118 119 120
      ! Retrieve arrays to hold values for environmental variables and corresponding metadata.
      call get_environment_metadata(model,environment_names,environment_units)

121
      call get_couplings(model,coupling_link_list)
122 123 124
   end subroutine initialize
!EOC

125 126 127 128 129 130 131 132 133 134
   subroutine reinitialize()
      type (type_model),           pointer :: newmodel
      type (type_model_list_node), pointer :: node
      class (type_base_model),     pointer :: childmodel
      class (type_property),       pointer :: property,next

      allocate(newmodel)

      ! Transfer forced parameters to root of the model.
      call newmodel%root%parameters%update(forced_parameters)
135
      call newmodel%root%couplings%update(forced_couplings)
136 137 138 139

      ! Re-create original models
      node => model%root%children%first
      do while (associated(node))
140
         if (node%model%user_created) then
141
            call factory%create(node%model%type_name,childmodel)
142
            childmodel%user_created = .true.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
            call newmodel%root%add_child(childmodel,node%model%name,node%model%long_name,configunit=-1)
         end if
         node => node%next
      end do

      ! Clean up old model
      call finalize()
      model => newmodel

      ! Initialize new model
      call fabm_initialize(model)

      ! Removed unused forced parameters from root model.
      property => model%root%parameters%first
      do while (associated(property))
158
         if (.not. model%root%parameters%retrieved%contains(property%name)) then
159 160 161 162 163 164 165 166 167 168 169 170 171
            next => property%next
            call model%root%parameters%delete(property%name)
            property => next
         else
            property => property%next
         end if
      end do

      ! Send information on spatial domain to FABM (this also allocates memory for diagnostics)
      call fabm_set_domain(model)

      ! Retrieve arrays to hold values for environmental variables and corresponding metadata.
      call get_environment_metadata(model,environment_names,environment_units)
172 173

      call get_couplings(model,coupling_link_list)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
174
   end subroutine reinitialize
175

176
   subroutine check_ready() bind(c)
177 178
      !DIR$ ATTRIBUTES DLLEXPORT :: check_ready
      call fabm_check_ready(model)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
179
   end subroutine check_ready
180

181
   integer(c_int) function model_count() bind(c)
182 183 184 185 186 187 188 189 190
      !DIR$ ATTRIBUTES DLLEXPORT :: model_count

      type (type_model_list_node), pointer :: node

      model_count = 0
      node => model%root%children%first
      do while (associated(node))
         model_count = model_count + 1
         node => node%next
Jorn Bruggeman's avatar
Jorn Bruggeman committed
191
      end do
Jorn Bruggeman's avatar
Jorn Bruggeman committed
192
   end function model_count
193

Jorn Bruggeman's avatar
Jorn Bruggeman committed
194
   subroutine get_counts(nstate_bulk,nstate_surface,nstate_bottom,ndiagnostic_bulk,ndiagnostic_horizontal,nconserved, &
195
      ndependencies,nparameters,ncouplings) bind(c)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
196
      !DIR$ ATTRIBUTES DLLEXPORT :: get_counts
197 198
      integer(c_int),intent(out) :: nstate_bulk,nstate_surface,nstate_bottom
      integer(c_int),intent(out) :: ndiagnostic_bulk,ndiagnostic_horizontal
199
      integer(c_int),intent(out) :: nconserved,ndependencies,nparameters,ncouplings
200 201 202 203 204 205 206 207
      nstate_bulk = size(model%state_variables)
      nstate_surface = size(model%surface_state_variables)
      nstate_bottom = size(model%bottom_state_variables)
      ndiagnostic_bulk = size(model%diagnostic_variables)
      ndiagnostic_horizontal = size(model%horizontal_diagnostic_variables)
      nconserved = size(model%conserved_quantities)
      ndependencies = size(environment_names)
      nparameters = model%root%parameters%size()
208
      ncouplings = coupling_link_list%count()
Jorn Bruggeman's avatar
Jorn Bruggeman committed
209
   end subroutine get_counts
210

211
   subroutine get_variable_metadata(category,index,length,name,units,long_name,path) bind(c)
212
      !DIR$ ATTRIBUTES DLLEXPORT :: get_variable_metadata
213
      integer(c_int),        intent(in), value             :: category,index,length
214
      character(kind=c_char),intent(out),dimension(length) :: name,units,long_name,path
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

      class (type_external_variable),pointer :: variable

      ! Get a pointer to the target variable
      select case (category)
      case (BULK_STATE_VARIABLE)
         variable => model%state_variables(index)
      case (SURFACE_STATE_VARIABLE)
         variable => model%surface_state_variables(index)
      case (BOTTOM_STATE_VARIABLE)
         variable => model%bottom_state_variables(index)
      case (BULK_DIAGNOSTIC_VARIABLE)
         variable => model%diagnostic_variables(index)
      case (HORIZONTAL_DIAGNOSTIC_VARIABLE)
         variable => model%horizontal_diagnostic_variables(index)
      case (CONSERVED_QUANTITY)
         variable => model%conserved_quantities(index)
      end select
233 234 235 236
      call copy_to_c_string(variable%name,           name)
      call copy_to_c_string(variable%units,          units)
      call copy_to_c_string(variable%local_long_name,long_name)
      call copy_to_c_string(variable%path,           path)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
237
   end subroutine get_variable_metadata
Jorn Bruggeman's avatar
Jorn Bruggeman committed
238

Jorn Bruggeman's avatar
Jorn Bruggeman committed
239 240 241 242
   function get_variable(category,index) bind(c) result(pvariable)
      !DIR$ ATTRIBUTES DLLEXPORT :: get_variable
      integer(c_int),intent(in), value :: category,index
      type (c_ptr)                     :: pvariable
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

      type (type_internal_variable),pointer :: variable

      ! Get a pointer to the target variable
      select case (category)
      case (BULK_STATE_VARIABLE)
         variable => model%state_variables(index)%target
      case (SURFACE_STATE_VARIABLE)
         variable => model%surface_state_variables(index)%target
      case (BOTTOM_STATE_VARIABLE)
         variable => model%bottom_state_variables(index)%target
      case (BULK_DIAGNOSTIC_VARIABLE)
         variable => model%diagnostic_variables(index)%target
      case (HORIZONTAL_DIAGNOSTIC_VARIABLE)
         variable => model%horizontal_diagnostic_variables(index)%target
      case (CONSERVED_QUANTITY)
         variable => model%conserved_quantities(index)%target
      end select
      pvariable = c_loc(variable)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
262
   end function get_variable
263

264
   subroutine get_parameter_metadata(index,length,name,units,long_name,typecode,has_default) bind(c)
265
      !DIR$ ATTRIBUTES DLLEXPORT :: get_parameter_metadata
266
      integer(c_int),        intent(in), value             :: index,length
267
      character(kind=c_char),intent(out),dimension(length) :: name,units,long_name
268
      integer(c_int),        intent(out)                   :: typecode,has_default
269 270 271 272 273 274 275 276 277 278

      integer                       :: i
      class (type_property),pointer :: property

      i = 1
      property => model%root%parameters%first
      do while (associated(property))
         if (index==i) exit
         property => property%next
         i = i + 1
Jorn Bruggeman's avatar
Jorn Bruggeman committed
279 280
      end do

281 282 283 284
      call copy_to_c_string(property%name,     name)
      call copy_to_c_string(property%units,    units)
      call copy_to_c_string(property%long_name,long_name)
      typecode = property%typecode()
285
      has_default = logical2int(property%has_default)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
286
   end subroutine get_parameter_metadata
Jorn Bruggeman's avatar
Jorn Bruggeman committed
287

288 289
   subroutine get_dependency_metadata(index,length,name,units) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: get_dependency_metadata
Jorn Bruggeman's avatar
Jorn Bruggeman committed
290
      integer(c_int),        intent(in), value             :: index,length
291
      character(kind=c_char),intent(out),dimension(length) :: name,units
Jorn Bruggeman's avatar
Jorn Bruggeman committed
292

293 294
      call copy_to_c_string(environment_names(index),name)
      call copy_to_c_string(environment_units(index),units)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
295
   end subroutine get_dependency_metadata
296

297 298 299 300 301 302 303
   subroutine get_coupling(index,slave,master) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: get_coupling
      integer(c_int),        intent(in), value :: index
      type (c_ptr),          intent(out)       :: slave,master

      type (type_link),pointer :: link_slave
      integer                  :: i
Knut's avatar
Knut committed
304

305 306 307 308 309 310
      link_slave => coupling_link_list%first
      do i=2,index
         link_slave => link_slave%next
      end do
      slave = c_loc(link_slave%original)
      master = c_loc(link_slave%target)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
311
   end subroutine get_coupling
312

Jorn Bruggeman's avatar
Jorn Bruggeman committed
313 314
   function variable_get_suitable_masters(pvariable) result(plist) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: variable_get_suitable_masters
315 316
      type (c_ptr), intent(in), value :: pvariable
      type (c_ptr)                    :: plist
317

318
      type (type_internal_variable),pointer :: variable
319
      type (type_link_list),        pointer :: list
320 321 322 323

      call c_f_pointer(pvariable, variable)
      list => get_suitable_masters(model,variable)
      plist = c_loc(list)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
324
   end function variable_get_suitable_masters
325

326
   subroutine get_model_metadata(name,length,long_name,user_created) bind(c)
327
      !DIR$ ATTRIBUTES DLLEXPORT :: get_model_metadata
328 329 330
      character(kind=c_char),intent(in), target :: name(*)
      integer(c_int),        intent(in), value  :: length
      character(kind=c_char),intent(out)        :: long_name(length)
331
      integer(c_int),        intent(out)        :: user_created
332

333 334
      character(len=attribute_length),pointer   :: pname
      class (type_base_model),        pointer   :: found_model
335 336 337

      call c_f_pointer(c_loc(name), pname)
      found_model => model%root%find_model(pname(:index(pname,C_NULL_CHAR)-1))
Jorn Bruggeman's avatar
Jorn Bruggeman committed
338 339
      if (.not.associated(found_model)) call driver%fatal_error('get_model_metadata', &
         'model "'//pname(:index(pname,C_NULL_CHAR)-1)//'" not found.')
340
      call copy_to_c_string(found_model%long_name,long_name)
341
      user_created = logical2int(found_model%user_created)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
342
   end subroutine get_model_metadata
343

344 345 346 347 348 349 350 351
   subroutine link_dependency_data(index,value) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: link_dependency_data
      integer(c_int),intent(in),value  :: index
      real(c_double),intent(in),target :: value

      call fabm_link_bulk_data(model,environment_names(index),value)
      call fabm_link_horizontal_data(model,environment_names(index),value)
      call fabm_link_scalar_data(model,environment_names(index),value)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
352
   end subroutine link_dependency_data
353 354 355 356 357

   subroutine link_bulk_state_data(index,value) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: link_bulk_state_data
      integer(c_int),intent(in),   value  :: index
      real(c_double),intent(inout),target :: value
Jorn Bruggeman's avatar
Jorn Bruggeman committed
358

359 360
      value = model%state_variables(index)%initial_value
      call fabm_link_bulk_state_data(model,index,value)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
361
   end subroutine link_bulk_state_data
Jorn Bruggeman's avatar
Jorn Bruggeman committed
362

363 364 365 366
   subroutine link_surface_state_data(index,value) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: link_surface_state_data
      integer(c_int),intent(in),   value  :: index
      real(c_double),intent(inout),target :: value
Jorn Bruggeman's avatar
Jorn Bruggeman committed
367

368 369
      value = model%surface_state_variables(index)%initial_value
      call fabm_link_surface_state_data(model,index,value)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
370
   end subroutine link_surface_state_data
Knut's avatar
Knut committed
371

372 373 374 375 376 377 378
   subroutine link_bottom_state_data(index,value) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: link_bottom_state_data
      integer(c_int),intent(in),   value  :: index
      real(c_double),intent(inout),target :: value

      value = model%bottom_state_variables(index)%initial_value
      call fabm_link_bottom_state_data(model,index,value)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
379
   end subroutine link_bottom_state_data
380 381 382

   subroutine get_rates(pelagic_rates_) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: get_rates
383
      real(c_double),target,intent(in) :: pelagic_rates_(*)
384 385

      real(c_double),pointer :: pelagic_rates(:)
386
      real(rk)               :: ext
387

388 389
      call fabm_get_light_extinction(model,ext)
      call fabm_get_light(model)
390 391
      call c_f_pointer(c_loc(pelagic_rates_),pelagic_rates, &
        (/size(model%state_variables)+size(model%surface_state_variables)+size(model%bottom_state_variables)/))
Jorn Bruggeman's avatar
Jorn Bruggeman committed
392
      pelagic_rates = 0.0_rk
Jorn Bruggeman's avatar
Jorn Bruggeman committed
393 394 395 396
      call fabm_do_bottom(model,pelagic_rates(1:size(model%state_variables)), &
         pelagic_rates(size(model%state_variables)+size(model%surface_state_variables)+1:))
      call fabm_do_surface(model,pelagic_rates(1:size(model%state_variables)), &
         pelagic_rates(size(model%state_variables)+1:size(model%state_variables)+size(model%surface_state_variables)))
397
      call fabm_do(model,pelagic_rates(1:size(model%state_variables)))
Jorn Bruggeman's avatar
Jorn Bruggeman committed
398 399

      ! Compute rate of change in conserved quantities
400
      !call fabm_state_to_conserved_quantities(model,pelagic_rates,conserved_rates)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
401 402

      ! Normalize rate of change in conserved quantities to sum of absolute rates of change.
403 404
      !call fabm_state_to_conserved_quantities(model,abs(pelagic_rates),abs_conserved_rates)
      !where (abs_conserved_rates>0.0_rk) conserved_rates = conserved_rates/abs_conserved_rates
Jorn Bruggeman's avatar
Jorn Bruggeman committed
405
   end subroutine get_rates
406 407 408 409 410 411

   subroutine get_bulk_diagnostic_data(index,ptr) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: get_bulk_diagnostic_data
      integer(c_int),intent(in),value :: index
      type(c_ptr),   intent(out)      :: ptr
      ptr = c_loc(fabm_get_bulk_diagnostic_data(model,index))
Jorn Bruggeman's avatar
Jorn Bruggeman committed
412
   end subroutine get_bulk_diagnostic_data
413 414 415 416 417 418

   subroutine get_horizontal_diagnostic_data(index,ptr) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: get_horizontal_diagnostic_data
      integer(c_int),intent(in),value :: index
      type(c_ptr),   intent(out)      :: ptr
      ptr = c_loc(fabm_get_horizontal_diagnostic_data(model,index))
Jorn Bruggeman's avatar
Jorn Bruggeman committed
419
   end subroutine get_horizontal_diagnostic_data
Jorn Bruggeman's avatar
Jorn Bruggeman committed
420

421
   subroutine finalize() bind(c)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
422 423 424
      call fabm_finalize(model)
      if (allocated(environment_names)) deallocate(environment_names)
      if (allocated(environment_units)) deallocate(environment_units)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
425
   end subroutine finalize
Jorn Bruggeman's avatar
Jorn Bruggeman committed
426

427
   subroutine reset_parameter(index) bind(c)
428
      !DIR$ ATTRIBUTES DLLEXPORT :: reset_parameter
429 430
      integer(c_int),value,intent(in) :: index
      class (type_property),pointer   :: property
431

432 433 434
      property => model%root%parameters%get_property(index)
      if (.not.associated(property)) return
      call forced_parameters%delete(property%name)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
435 436

      ! Re-initialize the model using updated parameter values
437
      call reinitialize()
Jorn Bruggeman's avatar
Jorn Bruggeman committed
438
   end subroutine reset_parameter
Jorn Bruggeman's avatar
Jorn Bruggeman committed
439

440 441
   subroutine set_real_parameter(name,value) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: set_real_parameter
442 443
      character(kind=c_char),target,intent(in) :: name(*)
      real(c_double),value,         intent(in) :: value
444 445 446 447 448

      character(len=attribute_length),pointer :: pname

      call c_f_pointer(c_loc(name), pname)
      call forced_parameters%set_real(pname(:index(pname,C_NULL_CHAR)-1),value)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
449 450

      ! Re-initialize the model using updated parameter values
451
      call reinitialize()
Jorn Bruggeman's avatar
Jorn Bruggeman committed
452
   end subroutine set_real_parameter
Jorn Bruggeman's avatar
Jorn Bruggeman committed
453

454
   function get_real_parameter(index,default) bind(c) result(value)
455
      !DIR$ ATTRIBUTES DLLEXPORT :: get_real_parameter
456
      integer(c_int),value,intent(in) :: index,default
457
      real(c_double)                  :: value
458 459 460 461 462
      class (type_property),pointer   :: property

      property => model%root%parameters%get_property(index)
      select type (property)
      class is (type_real_property)
463
         if (int2logical(default)) then
464 465 466 467 468 469 470
            value = property%default
         else
            value = property%value
         end if
      class default
         call driver%fatal_error('get_real_parameter','not a real variable')
      end select
Jorn Bruggeman's avatar
Jorn Bruggeman committed
471
   end function get_real_parameter
Jorn Bruggeman's avatar
Jorn Bruggeman committed
472

473 474
   subroutine set_integer_parameter(name,value) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: set_integer_parameter
475 476
      character(kind=c_char),target,intent(in) :: name(*)
      integer(c_int),value,         intent(in) :: value
477 478 479 480 481

      character(len=attribute_length),pointer :: pname

      call c_f_pointer(c_loc(name), pname)
      call forced_parameters%set_integer(pname(:index(pname,C_NULL_CHAR)-1),value)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
482 483

      ! Re-initialize the model using updated parameter values
484
      call reinitialize()
Jorn Bruggeman's avatar
Jorn Bruggeman committed
485
   end subroutine set_integer_parameter
Jorn Bruggeman's avatar
Jorn Bruggeman committed
486

487
   function get_integer_parameter(index,default) bind(c) result(value)
488
      !DIR$ ATTRIBUTES DLLEXPORT :: get_integer_parameter
489
      integer(c_int),value,intent(in) :: index,default
490
      integer(c_int)                  :: value
491 492 493 494 495
      class (type_property),pointer   :: property

      property => model%root%parameters%get_property(index)
      select type (property)
      class is (type_integer_property)
496
         if (int2logical(default)) then
497 498 499 500 501 502 503
            value = property%default
         else
            value = property%value
         end if
      class default
         call driver%fatal_error('get_integer_parameter','not an integer variable')
      end select
Jorn Bruggeman's avatar
Jorn Bruggeman committed
504
   end function get_integer_parameter
Jorn Bruggeman's avatar
Jorn Bruggeman committed
505

506 507
   subroutine set_logical_parameter(name,value) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: set_logical_parameter
508
      character(kind=c_char),target,    intent(in) :: name(*)
509 510 511 512 513
      integer(c_int),value,intent(in) :: value

      character(len=attribute_length),pointer :: pname

      call c_f_pointer(c_loc(name), pname)
514
      call forced_parameters%set_logical(pname(:index(pname,C_NULL_CHAR)-1),int2logical(value))
Jorn Bruggeman's avatar
Jorn Bruggeman committed
515 516

      ! Re-initialize the model using updated parameter values
517
      call reinitialize()
Jorn Bruggeman's avatar
Jorn Bruggeman committed
518
   end subroutine set_logical_parameter
Jorn Bruggeman's avatar
Jorn Bruggeman committed
519

520
   function get_logical_parameter(index,default) bind(c) result(value)
521
      !DIR$ ATTRIBUTES DLLEXPORT :: get_logical_parameter
522
      integer(c_int),value,intent(in) :: index,default
523
      integer(c_int)                  :: value
524 525 526 527 528
      class (type_property),pointer   :: property

      property => model%root%parameters%get_property(index)
      select type (property)
      class is (type_logical_property)
529 530
         if (int2logical(default)) then
            value = logical2int(property%default)
531
         else
532
            value = logical2int(property%value)
533 534 535 536
         end if
      class default
         call driver%fatal_error('get_logical_parameter','not a logical variable')
      end select
Jorn Bruggeman's avatar
Jorn Bruggeman committed
537
   end function get_logical_parameter
Jorn Bruggeman's avatar
Jorn Bruggeman committed
538

539 540 541 542 543
   subroutine set_string_parameter(name,value) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: set_string_parameter
      character(kind=c_char),target,intent(in) :: name(*),value(*)

      character(len=attribute_length),pointer :: pname,pvalue
Knut's avatar
Knut committed
544

545 546 547
      call c_f_pointer(c_loc(name),  pname)
      call c_f_pointer(c_loc(value), pvalue)
      call forced_parameters%set_string(pname(:index(pname,C_NULL_CHAR)-1),pvalue(:index(pname,C_NULL_CHAR)-1))
Jorn Bruggeman's avatar
Jorn Bruggeman committed
548 549

      ! Re-initialize the model using updated parameter values
550
      call reinitialize()
Jorn Bruggeman's avatar
Jorn Bruggeman committed
551
   end subroutine set_string_parameter
Jorn Bruggeman's avatar
Jorn Bruggeman committed
552

553 554 555 556 557
   subroutine get_string_parameter(index,default,length,value) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: get_string_parameter
      integer(c_int),value,intent(in) :: index,default,length
      character(kind=c_char)          :: value(length)

558 559 560 561 562
      class (type_property),pointer   :: property

      property => model%root%parameters%get_property(index)
      select type (property)
      class is (type_string_property)
563
         if (int2logical(default)) then
564
            call copy_to_c_string(property%default, value)
565
         else
566
            call copy_to_c_string(property%value, value)
567 568 569 570
         end if
      class default
         call driver%fatal_error('get_string_parameter','not a string variable')
      end select
Jorn Bruggeman's avatar
Jorn Bruggeman committed
571
   end subroutine get_string_parameter
Jorn Bruggeman's avatar
Jorn Bruggeman committed
572

573
   subroutine python_driver_fatal_error(self,location,message)
574 575 576 577 578
      class (type_python_driver),intent(inout) :: self
      character(len=*),          intent(in)    :: location,message

      write (*,*) trim(location)//': '//trim(message)
      stop 1
Jorn Bruggeman's avatar
Jorn Bruggeman committed
579
   end subroutine python_driver_fatal_error
580

581
   subroutine python_driver_log_message(self,message)
582 583 584 585
      class (type_python_driver),intent(inout) :: self
      character(len=*),          intent(in)    :: message

      !write (*,*) trim(message)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
586
   end subroutine python_driver_log_message
587

Jorn Bruggeman's avatar
Jorn Bruggeman committed
588 589 590 591 592
   end module fabm_python

!-----------------------------------------------------------------------
! Copyright by the GOTM-team under the GNU Public License - www.gnu.org
!-----------------------------------------------------------------------