python_fabm.F90 23.9 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
20
   use fabm_types, only:rk,attribute_length,type_model_list_node,type_base_model,factory,type_link,type_link_list,type_internal_variable
21
   use fabm_driver, only: type_base_driver, driver
22
   use fabm_properties
Jorn Bruggeman's avatar
Jorn Bruggeman committed
23 24 25 26 27 28 29
   use fabm_python_helper

   implicit none
!
! !PUBLIC MEMBER FUNCTIONS:
   public

30 31 32 33 34 35 36
   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

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

42
   type (type_property_dictionary),save,private :: forced_parameters,forced_couplings
43 44 45

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

   contains

!-----------------------------------------------------------------------
!BOP
!
! !IROUTINE: Initialise the model
!
! !INTERFACE:
60
   subroutine initialize(path) bind(c)
61
!DIR$ ATTRIBUTES DLLEXPORT :: initialize
62
      character(kind=c_char),target,intent(in) :: path(*)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
63 64 65 66 67 68 69 70
!
! !DESCRIPTION:
!
! !REVISION HISTORY:
!  Original author(s): Jorn Bruggeman
!
!EOP
!
71
      character(len=attribute_length),pointer :: ppath
72
      class (type_property),          pointer :: property
Jorn Bruggeman's avatar
Jorn Bruggeman committed
73 74
!-----------------------------------------------------------------------
!BOC
75 76
      call c_f_pointer(c_loc(path), ppath)

77
      if (associated(model)) call finalize()
Jorn Bruggeman's avatar
Jorn Bruggeman committed
78

79 80
      if (.not.associated(driver)) allocate(type_python_driver::driver)

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

85
      ! Get a list of all parameters that had an explicit value specified.
86 87
      property => model%root%parameters%first
      do while (associated(property))
88
         if (.not.model%root%parameters%missing%contains(property%name)) &
89 90 91
            call forced_parameters%set_property(property)
         property => property%next
      end do
Jorn Bruggeman's avatar
Jorn Bruggeman committed
92

93 94 95 96 97 98 99 100
      ! 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
101 102 103
      ! Send information on spatial domain to FABM (this also allocates memory for diagnostics)
      call fabm_set_domain(model)

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

107
      call get_couplings(model,coupling_link_list)
108 109 110
   end subroutine initialize
!EOC

111 112 113 114 115 116 117 118 119 120
   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)
121
      call newmodel%root%couplings%update(forced_couplings)
122 123 124 125

      ! Re-create original models
      node => model%root%children%first
      do while (associated(node))
126
         if (node%model%user_created) then
127
            call factory%create(node%model%type_name,childmodel)
128
            childmodel%user_created = .true.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
            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))
144
         if (.not. model%root%parameters%retrieved%contains(property%name)) then
145 146 147 148 149 150 151 152 153 154 155 156 157
            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)
158 159

      call get_couplings(model,coupling_link_list)
160 161
   end subroutine

162 163 164 165 166
   subroutine check_ready()
      !DIR$ ATTRIBUTES DLLEXPORT :: check_ready
      call fabm_check_ready(model)
   end subroutine

167
   integer(c_int) function model_count() bind(c)
168 169 170 171 172 173 174 175 176
      !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
177
      end do
178 179
   end function

180
   subroutine get_variable_counts(nstate_bulk,nstate_surface,nstate_bottom,ndiagnostic_bulk,ndiagnostic_horizontal,nconserved, &
181
      ndependencies,nparameters,ncouplings) bind(c)
182 183 184
      !DIR$ ATTRIBUTES DLLEXPORT :: get_variable_counts
      integer(c_int),intent(out) :: nstate_bulk,nstate_surface,nstate_bottom
      integer(c_int),intent(out) :: ndiagnostic_bulk,ndiagnostic_horizontal
185
      integer(c_int),intent(out) :: nconserved,ndependencies,nparameters,ncouplings
186 187 188 189 190 191 192 193
      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()
194
      ncouplings = coupling_link_list%count()
195 196
   end subroutine

197
   subroutine get_variable_metadata(category,index,length,name,units,long_name,path) bind(c)
198 199
      !DIR$ ATTRIBUTES DLLEXPORT :: get_variable_metadata
      integer(c_int),intent(in), value             :: category,index,length
200
      character(kind=c_char),intent(out),dimension(length) :: name,units,long_name,path
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

      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
219 220 221 222
      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)
223
   end subroutine
Jorn Bruggeman's avatar
Jorn Bruggeman committed
224

225
   subroutine get_parameter_metadata(index,length,name,units,long_name,typecode,has_default) bind(c)
226
      !DIR$ ATTRIBUTES DLLEXPORT :: get_parameter_metadata
227
      integer(c_int),        intent(in), value             :: index,length
228
      character(kind=c_char),intent(out),dimension(length) :: name,units,long_name
229
      integer(c_int),        intent(out)                   :: typecode,has_default
230 231 232 233 234 235 236 237 238 239

      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
240 241
      end do

242 243 244 245
      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()
246
      has_default = logical2int(property%has_default)
247
   end subroutine
Jorn Bruggeman's avatar
Jorn Bruggeman committed
248

249 250 251
   subroutine get_dependency_metadata(index,length,name,units) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: get_dependency_metadata
      integer(c_int),intent(in), value             :: index,length
252
      character(kind=c_char),intent(out),dimension(length) :: name,units
Jorn Bruggeman's avatar
Jorn Bruggeman committed
253

254 255 256 257
      call copy_to_c_string(environment_names(index),name)
      call copy_to_c_string(environment_units(index),units)
   end subroutine

258 259 260 261 262 263 264
   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
265

266 267 268 269 270 271 272 273 274 275 276 277 278
      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)
   end subroutine

   function get_suitable_masters_for_ptr(pvariable) result(plist) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: get_suitable_masters_for_ptr
      type (c_ptr),          intent(in), value :: pvariable
      type (c_ptr)                             :: plist

279
      type (type_internal_variable),pointer :: variable
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
      type (type_link_list),         pointer :: list

      call c_f_pointer(pvariable, variable)
      list => get_suitable_masters(model,variable)
      plist = c_loc(list)
   end function

   function link_list_count(plist) bind(c) result(value)
      !DIR$ ATTRIBUTES DLLEXPORT :: link_list_count
      type (c_ptr),          intent(in), value :: plist
      integer(c_int)                           :: value

      type (type_link_list),pointer :: list

      call c_f_pointer(plist, list)
      value = list%count()
   end function

   function link_list_index(plist,index) bind(c) result(pvariable)
      !DIR$ ATTRIBUTES DLLEXPORT :: link_list_index
      type (c_ptr),  intent(in), value :: plist
      integer(c_int),intent(in), value :: index
      type (c_ptr)                     :: pvariable

      type (type_link_list),pointer :: list
      type (type_link),     pointer :: link
      integer                       :: i
Knut's avatar
Knut committed
307

308 309 310 311 312 313 314 315
      call c_f_pointer(plist, list)
      link => list%first
      do i=2,index
         link => link%next
      end do
      pvariable = c_loc(link%target)
   end function

316 317 318 319 320
   subroutine link_list_finalize(plist) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: link_list_finalize
      type (c_ptr),  intent(in), value :: plist

      type (type_link_list),pointer :: list
Knut's avatar
Knut committed
321

322 323 324 325 326
      call c_f_pointer(plist, list)
      call list%finalize()
      deallocate(list)
   end subroutine

327 328 329 330 331 332
   subroutine get_variable_long_path(pvariable,length,long_name) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: get_variable_long_path
      type (c_ptr),          intent(in), value  :: pvariable
      integer(c_int),        intent(in), value  :: length
      character(kind=c_char),intent(out),dimension(length) ::long_name

333
      type (type_internal_variable),pointer :: variable
334 335
      class (type_base_model),       pointer :: owner
      character(len=attribute_length)        :: long_name_
Knut's avatar
Knut committed
336

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
      call c_f_pointer(pvariable, variable)
      long_name_ = variable%long_name
      owner => variable%owner
      do while (associated(owner%parent))
         long_name_ = trim(owner%long_name)//'/'//trim(long_name_)
         owner => owner%parent
      end do
      call copy_to_c_string(long_name_,long_name)
   end subroutine get_variable_long_path

   subroutine get_variable_metadata_ptr(pvariable,length,name,units,long_name) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: get_variable_metadata_ptr
      type (c_ptr),          intent(in), value  :: pvariable
      integer(c_int),        intent(in), value  :: length
      character(kind=c_char),intent(out),dimension(length) :: name,units,long_name

353
      type (type_internal_variable),pointer :: variable
354 355 356 357 358 359 360

      call c_f_pointer(pvariable, variable)
      call copy_to_c_string(variable%name,     name)
      call copy_to_c_string(variable%units,    units)
      call copy_to_c_string(variable%long_name,long_name)
   end subroutine get_variable_metadata_ptr

361
   subroutine get_model_metadata(name,length,long_name,user_created) bind(c)
362
      !DIR$ ATTRIBUTES DLLEXPORT :: get_model_metadata
363 364 365
      character(kind=c_char),intent(in), target :: name(*)
      integer(c_int),        intent(in), value  :: length
      character(kind=c_char),intent(out)        :: long_name(length)
366
      integer(c_int),        intent(out)        :: user_created
367

368 369
      character(len=attribute_length),pointer   :: pname
      class (type_base_model),        pointer   :: found_model
370 371 372 373 374

      call c_f_pointer(c_loc(name), pname)
      found_model => model%root%find_model(pname(:index(pname,C_NULL_CHAR)-1))
      if (.not.associated(found_model)) call driver%fatal_error('get_model_metadata','model "'//pname(:index(pname,C_NULL_CHAR)-1)//'" not found.')
      call copy_to_c_string(found_model%long_name,long_name)
375
      user_created = logical2int(found_model%user_created)
376 377
   end subroutine

378 379 380 381 382 383 384 385 386 387 388 389 390 391
   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)
   end subroutine

   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
392

393 394 395
      value = model%state_variables(index)%initial_value
      call fabm_link_bulk_state_data(model,index,value)
   end subroutine
Jorn Bruggeman's avatar
Jorn Bruggeman committed
396

397 398 399 400
   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
401

402 403 404
      value = model%surface_state_variables(index)%initial_value
      call fabm_link_surface_state_data(model,index,value)
   end subroutine
Knut's avatar
Knut committed
405

406 407 408 409 410 411 412 413 414 415 416
   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)
   end subroutine

   subroutine get_rates(pelagic_rates_) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: get_rates
417
      real(c_double),target,intent(in) :: pelagic_rates_(*)
418 419

      real(c_double),pointer :: pelagic_rates(:)
420
      real(rk)               :: ext
421

422 423
      call fabm_get_light_extinction(model,ext)
      call fabm_get_light(model)
424 425
      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
426 427 428 429
      pelagic_rates = 0.0_rk
      call fabm_do(model,pelagic_rates)

      ! Compute rate of change in conserved quantities
430
      !call fabm_state_to_conserved_quantities(model,pelagic_rates,conserved_rates)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
431 432

      ! Normalize rate of change in conserved quantities to sum of absolute rates of change.
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
      !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
   end subroutine

   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))
   end subroutine

   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
449 450
   end subroutine

451
   subroutine finalize() bind(c)
Jorn Bruggeman's avatar
Jorn Bruggeman committed
452 453 454 455 456
      call fabm_finalize(model)
      if (allocated(environment_names)) deallocate(environment_names)
      if (allocated(environment_units)) deallocate(environment_units)
   end subroutine

457
   subroutine reset_parameter(index) bind(c)
458
      !DIR$ ATTRIBUTES DLLEXPORT :: reset_parameter
459 460
      integer(c_int),value,intent(in) :: index
      class (type_property),pointer   :: property
461

462 463 464
      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
465 466

      ! Re-initialize the model using updated parameter values
467
      call reinitialize()
Jorn Bruggeman's avatar
Jorn Bruggeman committed
468 469
   end subroutine

470 471
   subroutine set_real_parameter(name,value) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: set_real_parameter
472
      character(kind=c_char),target,    intent(in) :: name(*)
473 474 475 476 477 478
      real(c_double),value,intent(in) :: value

      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
479 480

      ! Re-initialize the model using updated parameter values
481
      call reinitialize()
Jorn Bruggeman's avatar
Jorn Bruggeman committed
482 483
   end subroutine

484
   function get_real_parameter(index,default) bind(c) result(value)
485
      !DIR$ ATTRIBUTES DLLEXPORT :: get_real_parameter
486
      integer(c_int),value,intent(in) :: index,default
487
      real(c_double)                  :: value
488 489 490 491 492
      class (type_property),pointer   :: property

      property => model%root%parameters%get_property(index)
      select type (property)
      class is (type_real_property)
493
         if (int2logical(default)) then
494 495 496 497 498 499 500
            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
501 502
   end function

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

      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
512 513

      ! Re-initialize the model using updated parameter values
514
      call reinitialize()
Jorn Bruggeman's avatar
Jorn Bruggeman committed
515 516
   end subroutine

517
   function get_integer_parameter(index,default) bind(c) result(value)
518
      !DIR$ ATTRIBUTES DLLEXPORT :: get_integer_parameter
519
      integer(c_int),value,intent(in) :: index,default
520
      integer(c_int)                  :: value
521 522 523 524 525
      class (type_property),pointer   :: property

      property => model%root%parameters%get_property(index)
      select type (property)
      class is (type_integer_property)
526
         if (int2logical(default)) then
527 528 529 530 531 532 533
            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
534 535
   end function

536 537
   subroutine set_logical_parameter(name,value) bind(c)
      !DIR$ ATTRIBUTES DLLEXPORT :: set_logical_parameter
538
      character(kind=c_char),target,    intent(in) :: name(*)
539 540 541 542 543
      integer(c_int),value,intent(in) :: value

      character(len=attribute_length),pointer :: pname

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

      ! Re-initialize the model using updated parameter values
547
      call reinitialize()
Jorn Bruggeman's avatar
Jorn Bruggeman committed
548 549
   end subroutine

550
   function get_logical_parameter(index,default) bind(c) result(value)
551
      !DIR$ ATTRIBUTES DLLEXPORT :: get_logical_parameter
552
      integer(c_int),value,intent(in) :: index,default
553
      integer(c_int)                  :: value
554 555 556 557 558
      class (type_property),pointer   :: property

      property => model%root%parameters%get_property(index)
      select type (property)
      class is (type_logical_property)
559 560
         if (int2logical(default)) then
            value = logical2int(property%default)
561
         else
562
            value = logical2int(property%value)
563 564 565 566
         end if
      class default
         call driver%fatal_error('get_logical_parameter','not a logical variable')
      end select
Jorn Bruggeman's avatar
Jorn Bruggeman committed
567 568
   end function

569 570 571 572 573
   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
574

575 576 577
      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
578 579

      ! Re-initialize the model using updated parameter values
580
      call reinitialize()
Jorn Bruggeman's avatar
Jorn Bruggeman committed
581 582
   end subroutine

583 584 585 586 587
   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)

588 589 590 591 592
      class (type_property),pointer   :: property

      property => model%root%parameters%get_property(index)
      select type (property)
      class is (type_string_property)
593
         if (int2logical(default)) then
594
            call copy_to_c_string(property%default, value)
595
         else
596
            call copy_to_c_string(property%value, value)
597 598 599 600
         end if
      class default
         call driver%fatal_error('get_string_parameter','not a string variable')
      end select
601
   end subroutine
Jorn Bruggeman's avatar
Jorn Bruggeman committed
602

603
   subroutine python_driver_fatal_error(self,location,message)
604 605 606 607 608
      class (type_python_driver),intent(inout) :: self
      character(len=*),          intent(in)    :: location,message

      write (*,*) trim(location)//': '//trim(message)
      stop 1
609
   end subroutine
610

611
   subroutine python_driver_log_message(self,message)
612 613 614 615
      class (type_python_driver),intent(inout) :: self
      character(len=*),          intent(in)    :: message

      !write (*,*) trim(message)
616
   end subroutine
617

618 619 620 621 622 623 624 625
   subroutine copy_to_c_string(string,cstring)
      character(len=*),intent(in)  :: string
      character,       intent(out) :: cstring(:)
      integer i,n
      n = min(len_trim(string),size(cstring)-1)
      do i=1,n
         cstring(i) = string(i:i)
      end do
Knut's avatar
Knut committed
626
      cstring(n+1) = C_NULL_CHAR
627 628
   end subroutine

629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
   function logical2int(value) result(ivalue)
      logical,intent(in) :: value
      integer            :: ivalue
      if (value) then
         ivalue = 1
      else
         ivalue = 0
      end if
   end function

   function int2logical(ivalue) result(value)
      integer,intent(in) :: ivalue
      logical            :: value
      value = ivalue/=0
   end function

Jorn Bruggeman's avatar
Jorn Bruggeman committed
645 646 647 648 649
   end module fabm_python

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