TUV-x
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
musica_config::config_t Type Reference

Model configuration data. More...

Public Member Functions

procedure empty
 Empties the configuration.
 
procedure from_file => construct_from_file
 Loads a configuration with data from a file.
 
procedure to_file
 Writes a configuration to a file.
 
procedure number_of_children
 Returns the number of child objects.
 
procedure get_iterator
 Gets an iterator for the configuration data.
 
procedure key
 Gets the key name for a key-value pair.
 
procedure merge_in
 Merges another config_t object into the config_t object.
 
procedure validate
 Validates the format of the configuration file.
 
procedure print => do_print
 Print the raw contents of the configuration.
 
procedure pack_size
 Returns the number of bytes required to pack the object onto a buffer.
 
procedure mpi_pack
 Packs the object onto a character buffer.
 
procedure mpi_unpack
 Unpacks an object from a character buffer.
 
final finalize
 Cleans up memory.
 
 finalize_1d_array
 

Private Member Functions

procedure, private find_by_prefix
 Find a JSON key by prefix.
 

Private Attributes

type(c_ptr) node_ = c_null_ptr
 Pointer to YAML node.
 

Gets some configuration data

Each function includes optional found and default arguments. If neither is included and the data are not found, execution is stopped with an error message.

If a default value is included and the data are not found, the returned argument is set to this default value, otherwise it is set to a standard default value.

If the found argument is included and the data are found, found is set to true, otherwise it is set to false.

generic get => get_config, get_string, get_string_string_default, get_int, get_float, get_double, get_logical, get_string_array, get_double_array, get_config_array, get_from_iterator, get_array_from_iterator
 
procedure, private get_config
 
procedure, private get_string_string_default
 
procedure, private get_string
 
procedure, private get_int
 
procedure, private get_float
 
procedure, private get_double
 
procedure, private get_logical
 
procedure, private get_string_array
 
procedure, private get_double_array
 
procedure, private get_config_array
 
procedure, private get_from_iterator
 
procedure, private get_array_from_iterator
 

Adds a named piece of configuration data

generic add => add_config, add_char_array, add_string, add_int, add_float, add_double, add_logical, add_string_array, add_double_array, add_config_array
 
procedure, private add_config
 
procedure, private add_char_array
 
procedure, private add_string
 
procedure, private add_int
 
procedure, private add_float
 
procedure, private add_double
 
procedure, private add_logical
 
procedure, private add_string_array
 
procedure, private add_double_array
 
procedure, private add_config_array
 

Assignment

generic assignment => config_assign_config, config_assign_string, config_assign_char, string_assign_config
 
procedure, private config_assign_config
 
procedure, private config_assign_string
 
procedure, private config_assign_char
 
procedure, pass(config), private string_assign_config
 

Detailed Description

Model configuration data.

Instances of type config_t can be used to access model configuration data in json format. If there is a need to use model configuration in another format (e.g., XML) in the future, an abstract config_t type could be set up, that this type and an XML-based type could extend. The rest of the model code would be unaffected.

It is assumed that most configuration datasets will be small enough that returned subsets of configuration data can just be a copy of the original data (instead of using a pointer to the start of the subset in the original dataset, or something like this). This avoids ownership problems with cleaning up the memory after a config_t object goes out of scope.

Only use config_t objects during initialization. They are not designed for efficiency.

IMPORTANT: The order of elements is arbitrary. No user of a config_t object can assume anything by the order of key-value pairs in the data. This dataset:

foo: 1
bar: 2
foobar: 3

... is the same as:

bar: 2
foobar: 3
foo: 1

There is no guarantee that an iterator over the elements of a config_t object will return them in the same order they exist in the original file or string.

Example of a config_t object generated from a file:

use musica_constants, only : musica_dk, musica_ik
character(len=*), parameter :: my_name = "config file example"
type(config_t) :: main_config, sub_config, sub_real_config
real(musica_dk) :: my_real
integer(musica_ik) :: my_int
type(string_t) :: my_string
class(iterator_t), pointer :: iter
logical :: found
call main_config%from_file( 'data/config_example.yaml' )
! this would fail with an error if 'a string' is not found
call main_config%get( "a string", my_string, my_name )
write(*,*) "a string value: ", my_string
! add the found argument to avoid failure if the pair is not found
call main_config%get( "my int", my_int, my_name, found = found )
if( found ) then
write(*,*) "my int value: ", my_int
else
write(*,*) "'my int' was not found"
end if
! when you get a subset of the properties, a new config_t object is
! created containing the subset data. The two config_t objects are
! independent of one another after this point.
call main_config%get( "other props", sub_config, my_name )
call sub_config%get( "an int", my_int, my_name )
write(*,*) "other props->an int value: ", my_int
! you can iterate over a set of key-value pairs. but remember that
! the order is always arbitrary. you also must provide the right type
! of variable for the values.
call main_config%get( "real props", sub_real_config, my_name )
iter => sub_real_config%get_iterator( )
do while( iter%next( ) )
my_string = sub_real_config%key( iter )
call sub_real_config%get( iter, my_real, my_name )
write(*,*) my_string, " value: ", my_real
end do
! you can also get the number of child objects before iterating over
! them, if you want to allocate an array or something first
write(*,*) "number of children: ", sub_real_config%number_of_children( )
! you can add key-value pairs with the add function
call main_config%add( "my new int", 43, my_name )
call main_config%get( "my new int", my_int, my_name )
write(*,*) "my new int value: ", my_int
! clean up memory
deallocate( iter )
The config_t type and related functions.
Definition: config.F90:8
Common physical constants.
Definition: constants.F90:8
integer, parameter musica_dk
Kind of a double-precision real number.
Definition: constants.F90:20
The abstract iterator_t type and related functions.
Definition: iterator.F90:8
The string_t type and related functions.
Definition: string.F90:8
Model configuration data.
Definition: config.F90:144
An abstract iterator.
Definition: iterator.F90:40
Generic string type.
Definition: string.F90:21

data/config_example.json:

my int: 12
other props:
some time [min]: 12
a pressure [bar]: 103.4
an int: 45
real props:
foo: 14.2
bar: 64.2
foobar: 920.4
a string: foo

Output:

a string value: foo
my int value: 12
other props->an int value: 45
other props->some time value: 720.00000000000000 s
other props->a pressure value: 10340000.000000000 Pa
foo value: 14.199999999999999
bar value: 64.200000000000003
foobar value: 920.39999999999998
number of children: 3
my new int value: 43

The documentation for this type was generated from the following file: