Neko 1.99.1
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
startup_and_json.f90
Go to the documentation of this file.
1! This module demonstrates how to define user-specific routines in Neko. It
2! provides an example of how to interact with the JSON parameter dictionary
3! used for simulation configuration. The key points covered in this tutorial
4! are:
5!
6! - Registering user-defined functions using the `user_setup` routine.
7! - Using the `user_startup` routine to inspect and manipulate the JSON
8! parameter dictionary before the simulation starts.
9! - Extracting parameters from the JSON file using `json_get` and
10! `json_get_or_default`, and `json_extract_object`.
11! - Printing JSON objects using the `print` method.
12! - Adding or modifying parameters in the JSON file using the `add` method.
13! - Saving the JSON parameter dictionary for later use in the module.
14!
15! This tutorial highlights the flexibility of the JSON-based configuration
16! system in Neko and demonstrates how to customize simulations by interacting
17! with the JSON parameter dictionary.
18
19
20! The user module always needs to be named "user"!
21module user
22 ! This use statement populates the scope of the module with all the types and
23 ! public procedures defined in Neko. So, this is convenient, but your scope
24 ! does get very polluted. It is generallys a good idea to `use` only the
25 ! modules you need, and moreover specify what exactly you want from those
26 ! modules using `use ..., only :`.
27 use neko
28 implicit none
29
30 ! A module-scope variable to hold a copy of the case file JSON parameter
31 ! dictionary.
32 type(json_file) :: case_params
33
34 ! Some variable that we want to use in the user code.
35 real(kind=rp) :: some_variable
36
37contains
38
39 ! This is a special routine that registers the user-defined functions with
40 ! Neko. Based on the inerface defined in user_intf.f90, we can register our
41 ! user-defined implementations of the various routines. You do this by
42 ! assigning procedure pointers to subroutines in the user module. Here, we
43 ! register only the startup routine.
44 subroutine user_setup(user)
45 type(user_t), intent(inout) :: user
46
47 user%startup => startup
48
49 end subroutine user_setup
50
51 ! The startup routine, provides the possibility to inspect and manipulate
52 ! the JSON parameter dictionary before the simulation starts. The routine is
53 ! called very early, before any of the solvers or simulation components are
54 ! initialized. This is also a good place to set up some constants that are
55 ! needed in the user code.
56 subroutine startup(params)
57 type(json_file), intent(inout) :: params
58
59 ! Some auxillary variables to extract various type of parameters from the
60 ! JSON
61 real(kind=rp) :: some_real
62 ! Note that we need an allocatable when we extract strings or arrays.
63 ! The json_fortran library will allocate the string or array for us.
64 character(len=:), allocatable :: some_string
65 real(kind=rp), allocatable :: some_real_array(:)
66 ! We can extract an entire JSON object into a new json_file.
67 type(json_file) :: some_json_object
68
69 ! Assign our constant to something. Can be based on JSON parameters
70 ! extracted below. A common use case is to set material properties based
71 ! on custom JSON entries in the case file, e.g. some non-dimensional number.
72 some_variable = 1.0_rp
73
74 ! Neko relies on json_fortran to parse and manipulate the JSON file.
75 ! In addition, there are some convenience subroutines in the json_utils
76 ! module.
77
78 ! Extract a string parameter. This will through an error if
79 ! case.fluid.scheme does not exist in the JSON file.
80 call json_get(params, "case.fluid.scheme", some_string)
81
82
83 ! Extract a real parameter, providing a default if it does not exist.
84 call json_get_or_default(params, "case.fluid.Re", some_real, 100.0_rp)
85
86 ! Extract the object with the velocity initial conditions.
87 call json_extract_object(params, "case.fluid.initial_condition", &
88 some_json_object)
89
90 ! We can print the contents to the console.
91 call some_json_object%print()
92
93 ! We can use the json_fotran methods to manipulate the JSON.
94 ! Here we add an additional parameter.
95 call params%add("case.fluid.initial_condition.my_param", 3.0_rp)
96
97 ! Add can also be used to modify existing parameters.
98 some_real_array = [1.0_rp, 2.0_rp, 3.0_rp]
99 call params%add("case.fluid.initial_condition.value", some_real_array)
100 call params%add("case.end_time", 0.0_rp)
101
102 ! Show the updated part of the JSON file.
103 call json_extract_object(params, "case.fluid.initial_condition", &
104 some_json_object)
105 call some_json_object%print()
106
107 ! There are a few other json utilities in neko, as well as more methods in
108 ! the json_fortran library. So, you can pretty much do anything you want
109 ! with the case file, if you so need.
110
111 ! We can save the params into our module-scope variable for later use.
112 case_params = params
113
114 end subroutine startup
115
116
117end module user
Master module.
Definition neko.f90:34
real(kind=rp) some_variable
subroutine user_setup(user)
type(json_file) case_params
subroutine startup(params)