This repository provides practical usage examples of an abstract data type designed for the repository pattern defined as repot.
In this example, the abstract data type base_repository_atype
is extended for reading an integer value and real value from repositories in 4 different formats:
- json
- namelist
- user-defined
- in-memory
In-memory here means that the data is not placed in an external file and is held in memory as components of an extended user-defined type.
This repository also provides examples of internal representations relating IDs (keys) to value. One is to use a select-case statement, and another is to use a hashmap.
From the main routine in app/app.f90
, we can see that being able to get data through the common interface regardless of the repository types, i.e., external file format or internal representation.
use :: repot
implicit none
class(base_repository_atype), allocatable :: config_repo
call configure_repository(config_repo)
block
use :: config_ids
integer(int32) :: num
real(real64) :: val
if (config_repo%find(Config_ID_Number_of_Iterations)) &
call config_repo%get(Config_ID_Number_of_Iterations, num)
if (config_repo%find(Config_ID_Initial_Value)) &
call config_repo%get(Config_ID_Initial_Value, val)
print '(A,":", g0)', Config_ID_Initial_Value, val
print '(A,":", g0)', Config_ID_Number_of_Iterations, num
end block
The examples require the following to build:
- Modern Fortran compiler
- gfortran 11.2 bundled with quickstart Fortran on Windows
- Intel Fortran Classic 2021.5.0 Build 20211109_000000
- NAG Fortran 7.1 Build 7117
- Fortran Package Manager (fpm) 0.7.0 alpha
- JSON-Fortran
- stdlib
- M_CLI2
To get the code, execute the following command:
git clone https://github.com/degawa/repot_examples.git
cd repot_examples
To build the example using fpm with gfortran, execute the following command:
fpm build
Execute the example with command line arguments. The example reads values from the data storage specified by the arguments.
>fpm run -- --format "in_memory"
initial value:4.000000000000000
number of iterations:100
>fpm run -- --format "in_memory_hashmap"
initial value:9.0000000000000000
number of iterations:25
>cat app/config.json
{
"unknown": {
"initial value": 1.0
},
"iterative solver": {
"number of iterations": 1
}
}
>fpm run -- --format "json" --filename "app/config.json"
initial value:1.0000000000000000
number of iterations:1
>cat app/config.nml
&simulation_configulation
config%initial_value = -100d0
config%number_of_iterations = 20
/
>fpm run -- --format "namelist" --filename "app/config.nml"
initial value:-100.00000000000000
number of iterations:20
>cat app/config
-1.0
1000
>fpm run -- --format "user_defined" --filename "app/config"
initial value:-1.0000000000000000
number of iterations:1000
To build the example using fpm with gfortran, execute the following command:
fpm build --compiler ifort
Execute the example with command line arguments. The example reads values from the data storage specified by the arguments.
>fpm run --compiler ifort -- --format "in_memory"
initial value:4.000000000000000
number of iterations:100
>fpm run --compiler ifort -- --format "in_memory_hashmap"
initial value:9.0000000000000000
number of iterations:25
>fpm run --compiler ifort -- --format "json" --filename "app/config.json"
initial value:1.0000000000000000
number of iterations:1
>fpm run --compiler ifort -- --format "namelist" --filename "app/config.nml"
initial value:-100.00000000000000
number of iterations:20
>fpm run --compiler ifort -- --format "user_defined" --filename "app/config"
initial value:-1.0000000000000000
number of iterations:1000
NAG Fortran compiler cannot build json-fortran because the preprocessor's behavior differs from gfortran and Intel Fortran. Some modifications are required to build json-fortran.
- Copy the
json_initialize_dummy_arguments.inc
tojson_initialize_dummy_arguments_nagfor.inc
and add one more&
to the final line ofjson_initialize_dummy_arguments_nagfor.inc
.
-strict_integer_type_checking &
+strict_integer_type_checking &&
- Define the preprocessor macro in the
#if
block added to the top ofjson_value_module.F90
andjson_file_module.F90
for choosing the appropriateinc
file.
+#if defined(NAGFOR)
+#define JSON_INIT_DUMMY_ARGS "json_initialize_dummy_arguments_nagfor.inc"
+#else
+#define JSON_INIT_DUMMY_ARGS "json_initialize_dummy_arguments.inc"
+#endif
- Replace the
#include
target to the macro injson_value_module.F90
andjson_file_module.F90
.
-#include "json_initialize_dummy_arguments.inc"
+#include JSON_INIT_DUMMY_ARGS
To build the example using fpm with gfortran, execute the following command:
fpm build --compiler nagfor --flag "-I build\dependencies\json-fortran\src"
Execute the example with command line arguments. The example reads values from the data storage specified by the arguments.
>fpm run --compiler nagfor --flag "-I build\dependencies\json-fortran\src" -- --format "in_memory"
initial value:4.000000000000000
number of iterations:100
>fpm run --compiler nagfor --flag "-I build\dependencies\json-fortran\src" -- --format "in_memory_hashmap"
initial value:9.000000000000000
number of iterations:25
>fpm run --compiler nagfor --flag "-I build\dependencies\json-fortran\src" -- --format "json" --filename "app/config.json"
initial value:1.000000000000000
number of iterations:1
>fpm run --compiler nagfor --flag "-I build\dependencies\json-fortran\src" -- --format "namelist" --filename "app/config.nml"
initial value:-100.0000000000000
number of iterations:20
>fpm run --compiler nagfor --flag "-I build\dependencies\json-fortran\src" -- --format "user_defined" --filename "app/config"
initial value:-1.000000000000000
number of iterations:1000