Skip to content

Commit

Permalink
added fabm_finalzie_library, general clean-up improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jornbr committed Mar 17, 2021
1 parent 59835b7 commit 5ccf737
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 256 deletions.
482 changes: 241 additions & 241 deletions include/standard_variable_assignments.h

Large diffs are not rendered by default.

23 changes: 21 additions & 2 deletions src/fabm.F90
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module fabm
public fabm_initialize_library
public fabm_get_version
public fabm_create_model
public fabm_finalize_library
public type_fabm_model

! Variable identifier types by external physical drivers.
Expand All @@ -65,6 +66,8 @@ module fabm
integer, parameter, public :: data_source_user = 3
integer, parameter, public :: data_source_default = data_source_host

logical, save :: default_driver = .false.

! --------------------------------------------------------------------------
! Derived typed for variable identifiers
! --------------------------------------------------------------------------
Expand Down Expand Up @@ -333,16 +336,32 @@ subroutine fabm_initialize_library()
if (associated(factory)) return

! If needed, create default object for communication (e.g., logging, error reporting) with host.
if (.not. associated(driver)) allocate(driver)
if (.not. associated(driver)) then
allocate(driver)
default_driver = .true.
end if

! Create all standard variable objects.
call initialize_standard_variables()
call fabm_standard_variables%initialize()

! Create the model factory.
factory => fabm_model_factory
call factory%initialize()
end subroutine fabm_initialize_library

! --------------------------------------------------------------------------
! fabm_finalize_library: finalize FABM library
! --------------------------------------------------------------------------
! This deallocates all global variables created by fabm_initialize_library
! --------------------------------------------------------------------------
subroutine fabm_finalize_library()
call fabm_standard_variables%finalize()

if (associated(driver) .and. default_driver) deallocate(driver)
if (associated(factory)) call factory%finalize()
factory => null()
end subroutine fabm_finalize_library

! --------------------------------------------------------------------------
! fabm_get_version: get FABM version string
! --------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions src/fabm_graph.F90
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,14 @@ recursive function graph_add_call(self, model, source, stack_top) result(node)
target_graph => self

! Check if this node is already in a graph of another job (recursive search from the root graph/very first job).
! NB owner_graph and node are nullified only to make it clear to valgrind that the code path does not depend
! on uninitialized values.
root_graph => target_graph
do while (associated(root_graph%previous%first))
root_graph => root_graph%previous%first%p
end do
owner_graph => null()
node => null()
call find_node(root_graph, model, source, owner_graph, node)

if (associated(node)) then
Expand Down
22 changes: 16 additions & 6 deletions src/fabm_job.F90
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ module fabm_job

type type_job_node
class (type_job), pointer :: p => null()
logical :: owner = .false.
type (type_job_node), pointer :: next => null()
end type

Expand Down Expand Up @@ -145,6 +144,7 @@ module fabm_job
procedure :: initialize => job_manager_initialize
procedure :: print => job_manager_print
procedure :: write_graph => job_manager_write_graph
procedure :: finalize => job_manager_finalize
end type

type type_variable_register
Expand Down Expand Up @@ -503,9 +503,9 @@ end subroutine job_print
subroutine job_finalize(self)
class (type_job), intent(inout) :: self

type (type_task), pointer :: task, next_task
type (type_task), pointer :: task, next_task
type (type_variable_request), pointer :: variable_request, next_variable_request
type (type_call_request), pointer :: call_request, next_call_request
type (type_call_request), pointer :: call_request, next_call_request

task => self%first_task
do while (associated(task))
Expand Down Expand Up @@ -1263,7 +1263,6 @@ subroutine job_set_finalize(self)
job_node => self%first
do while (associated(job_node))
next => job_node%next
if (job_node%owner) call job_node%p%finalize()
deallocate(job_node)
job_node => next
end do
Expand Down Expand Up @@ -1362,7 +1361,6 @@ subroutine job_manager_create(self, job, name, source, outsource_tasks, previous

allocate(node)
node%p => job
node%owner = .true.
node%next => self%first
self%first => node

Expand Down Expand Up @@ -1497,7 +1495,6 @@ recursive subroutine add_to_order(job)
node => first_ordered
end if
node%p => job
node%owner = .true.
end subroutine add_to_order

end subroutine job_manager_initialize
Expand All @@ -1516,6 +1513,19 @@ subroutine job_manager_print(self, unit, specific_variable)
end do
end subroutine job_manager_print

subroutine job_manager_finalize(self)
class (type_job_manager), intent(inout) :: self

type (type_job_node), pointer :: job_node

job_node => self%first
do while (associated(job_node))
call job_node%p%finalize()
job_node => job_node%next
end do
call self%type_job_set%finalize()
end subroutine

subroutine job_manager_write_graph(self, unit)
class (type_job_manager), intent(in) :: self
integer, intent(in) :: unit
Expand Down
30 changes: 26 additions & 4 deletions src/fabm_standard_variables.F90
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module fabm_standard_variables
public type_base_standard_variable
public type_universal_standard_variable, type_domain_specific_standard_variable, type_interior_standard_variable, type_horizontal_standard_variable, type_surface_standard_variable, type_bottom_standard_variable, type_global_standard_variable
public type_standard_variable_node, type_standard_variable_set
public standard_variables, initialize_standard_variables
public standard_variables

! ====================================================================================================
! Data types that contain all metadata needed to describe standard variables.
Expand Down Expand Up @@ -91,6 +91,7 @@ module fabm_standard_variables

type type_standard_variable_node
class (type_base_standard_variable), pointer :: p => null()
logical, private :: own = .false.
type (type_standard_variable_node), pointer :: next => null()
end type

Expand All @@ -108,6 +109,9 @@ module fabm_standard_variables
type type_standard_variable_collection
type (type_standard_variable_node), pointer :: first => null()
#include "standard_variables.h"
contains
procedure :: initialize => standard_variable_collection_initialize
procedure :: finalize => standard_variable_collection_finalize
end type

! Single instance of the collection that contains all standard variables.
Expand Down Expand Up @@ -136,7 +140,7 @@ recursive function base_standard_variable_resolve(self) result(p)
end do

allocate(p, source=self)
call add(p)
call add(p, own=.true.)

contains

Expand All @@ -153,8 +157,9 @@ logical function compare(variable1, variable2)

end function base_standard_variable_resolve

recursive subroutine add(standard_variable)
recursive subroutine add(standard_variable, own)
class (type_base_standard_variable), target, intent(inout) :: standard_variable
logical, optional, intent(in) :: own

type (type_standard_variable_node), pointer :: node
type (type_interior_standard_variable) :: in_interior
Expand All @@ -172,6 +177,7 @@ recursive subroutine add(standard_variable)

allocate(node)
node%p => standard_variable
if (present(own)) node%own = own
node%next => standard_variables%first
standard_variables%first => node
standard_variable%resolved = .true.
Expand Down Expand Up @@ -288,10 +294,26 @@ subroutine base_standard_variable_assert_resolved(self)
stop 1
end subroutine

subroutine initialize_standard_variables()
subroutine standard_variable_collection_initialize(self)
class (type_standard_variable_collection), intent(inout) :: self
#include "standard_variable_assignments.h"
end subroutine

subroutine standard_variable_collection_finalize(self)
class (type_standard_variable_collection), intent(inout) :: self

type (type_standard_variable_node), pointer :: node, next

node => self%first
do while (associated(node))
next => node%next
if (node%own) deallocate(node%p)
deallocate(node)
node => next
end do
self%first => null()
end subroutine standard_variable_collection_finalize

logical function standard_variable_set_contains_variable(self, standard_variable)
class (type_standard_variable_set), intent(in) :: self
class (type_base_standard_variable), target :: standard_variable
Expand Down
18 changes: 17 additions & 1 deletion src/fabm_types.F90
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module fabm_types
public standard_variables
public type_interior_standard_variable, type_horizontal_standard_variable, type_global_standard_variable, &
type_universal_standard_variable, type_bottom_standard_variable, type_surface_standard_variable, type_domain_specific_standard_variable, &
initialize_standard_variables, type_standard_variable_node, type_base_standard_variable, type_standard_variable_set
type_standard_variable_node, type_base_standard_variable, type_standard_variable_set

! Variable identifier types used by biogeochemical models
public type_variable_id
Expand Down Expand Up @@ -692,6 +692,7 @@ module fabm_types
procedure :: add => abstract_model_factory_add
procedure :: create => abstract_model_factory_create
procedure :: register_version => abstract_model_factory_register_version
procedure :: finalize => abstract_model_factory_finalize
end type

class (type_base_model_factory), pointer, save, public :: factory => null()
Expand Down Expand Up @@ -2929,6 +2930,21 @@ recursive subroutine abstract_model_factory_register_version(self, name, version
version%version_string = version_string
end subroutine abstract_model_factory_register_version

recursive subroutine abstract_model_factory_finalize(self)
class (type_base_model_factory), intent(inout) :: self

type (type_base_model_factory_node), pointer :: current, next

current => self%first_child
do while(associated(current))
next => current%next
call current%factory%finalize()
deallocate(current)
current => next
end do
self%first_child => null()
end subroutine abstract_model_factory_finalize

subroutine coupling_task_list_remove(self, task)
class (type_coupling_task_list), intent(inout) :: self
class (type_coupling_task), pointer :: task
Expand Down
2 changes: 2 additions & 0 deletions src/test/host.F90
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@ program test_host

deallocate(model)

call fabm_finalize_library()

contains

subroutine read_environment
Expand Down
4 changes: 2 additions & 2 deletions util/standard_variables/parse_standard_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ def CF2FABM(id):

# Assign variable attributes in Fortran.
for k, v in data:
fout_assignments.write('standard_variables%%%s%%%s = %s\n' % (item['name'],k,v))
fout_assignments.write('call add(standard_variables%%%s)\n\n' % item['name'])
fout_assignments.write('self%%%s%%%s = %s\n' % (item['name'],k,v))
fout_assignments.write('call add(self%%%s)\n\n' % item['name'])

# Create wiki entry for this variable.
fwiki.write('|%s|%s|%s|\n' % (item['name'],item['units'],item.get('cf_names',('',))[0]))
Expand Down

0 comments on commit 5ccf737

Please sign in to comment.