ComIn 1.0.0
ICON Community Interface
Loading...
Searching...
No Matches
simple_fortran_plugin.F90
Go to the documentation of this file.
1! --------------------------------------------------------------------
2!> Example plugin for the ICON Community Interface (ComIn)
3! with basic (not MPI-parallel) callbacks and accessing variables and
4! descriptive data structures.
5!
6! Note that in order to demonstrate ComIn's language interoperability,
7! a similary plugin has been implemented in C, see the subdirectory
8! "simple_c".
9!
10!
11! @authors 01/2023 :: ICON Community INTERFACE <comin@icon-model.org>
12!
13! SPDX-License-Identifier: BSD-3-Clause
14!
15! Please see the file LICENSE in the root of the source tree for this code.
16! Where software is supplied by third parties, it is indicated in the
17! headers of the routines.
18! --------------------------------------------------------------------
21 & t_comin_var_descriptor, t_comin_var_handle, &
23 & comin_descrdata_get_global, t_comin_descrdata_global, &
24 & t_comin_setup_version_info, comin_setup_get_version, &
25 & ep_secondary_constructor, ep_atm_write_output_before, &
26 & comin_flag_read, comin_flag_write, &
28 & comin_domain_outside_loop, &
30 & t_comin_metadata_rw, comin_var_request_get_metadata, &
32 & comin_dim_semantics_level, comin_dim_semantics_nproma, &
33 & comin_dim_semantics_block
34
35 IMPLICIT NONE
36
37 !> working precision
38 INTEGER, PARAMETER :: wp = selected_real_kind(12,307)
39
40 !> handles for custom ICON variables
41 TYPE(t_comin_var_handle) :: pres, example_fortran_var, example_fortran_tracer
42
43 !> access descriptive data structures
44 TYPE(t_comin_descrdata_global) :: p_global
45
46 !> text buffer for printing
47 CHARACTER(LEN=200) :: text
48
49CONTAINS
50
51 SUBROUTINE assert(cond, message)
52 LOGICAL, INTENT(IN) :: cond
53 CHARACTER(len=*), INTENT(IN) :: message
54 IF (.NOT. cond) CALL comin_plugin_finish('simple_fortran_constructor', 'Assertion failed: ' // message)
55 END SUBROUTINE assert
56
57 ! --------------------------------------------------------------------
58 ! ComIn primary constructor.
59 ! The ICON model is not yet fully initialized, but the plugin can already
60 ! register its own variables and callbacks.
61 ! --------------------------------------------------------------------
62 SUBROUTINE comin_main() BIND(C)
63 !
64 TYPE(t_comin_var_descriptor) :: example_fortran_d, example_fortran_tracer_d
65 TYPE(t_comin_setup_version_info) :: version
66 TYPE(t_comin_metadata_rw) :: meta
67 INTEGER :: jg
68
69 version = comin_setup_get_version()
70 IF (version%version_no_major > 1) &
71 CALL comin_plugin_finish("comin_main (simple_fortran_plugin", "incompatible version!")
72
73 ! get descriptive data structures
74 p_global = comin_descrdata_get_global()
75
76 ! add requests for additional ICON variables
77 ! ... a scalar field (3D array)
78 example_fortran_d = t_comin_var_descriptor(id = 1, name = "example_fortran_var")
79 CALL comin_var_request_add(example_fortran_d, lmodexclusive = .false.)
80 CALL comin_var_request_get_metadata(example_fortran_d, meta)
81 CALL meta%set("tracer", .false.)
82 CALL meta%set("restart", .false.)
83 CALL meta%set("groups", [ "mygroup" ])
84
85 ! ... a tracer (4D array), must be requested on all domains.
86 DO jg = 1, p_global%get_n_dom()
87 example_fortran_tracer_d = t_comin_var_descriptor( id = jg, name = "example_fortran_tracer" )
88 CALL comin_var_request_add(example_fortran_tracer_d, lmodexclusive = .false.)
89 CALL comin_var_request_get_metadata(example_fortran_tracer_d, meta)
90 CALL meta%set("tracer", .true.)
91 CALL meta%set("restart", .false.)
92 END DO
93
94 ! register callbacks
95 CALL comin_callback_register(ep_secondary_constructor, simple_fortran_constructor)
96 CALL comin_callback_register(ep_atm_write_output_before, simple_fortran_diagfct)
97 END SUBROUTINE comin_main
98
99 ! --------------------------------------------------------------------
100 ! ComIn secondary constructor.
101 ! All variables have been registered by ICON (including our custom variables)
102 ! and we can get handles for them.
103 ! --------------------------------------------------------------------
104 SUBROUTINE simple_fortran_constructor() BIND(C)
105 INTEGER :: dim_semantics(5)
106 LOGICAL :: tracer
107 TYPE(t_comin_metadata) :: meta
108
109 CALL comin_print_info("third party callback: secondary constructor.")
110
111 ! ... get handle for the pressure field (3D array) from ICON
112 CALL comin_var_get([ep_atm_write_output_before], &
113 & t_comin_var_descriptor('pres', 1), comin_flag_read, pres)
114 ! the handle is valid or ComIn would have aborted with an error.
115 CALL assert(pres%valid(), "handle for 'pres' is valid")
116
117 ! ... get handles for our own variables (which we registered in comin_main)
118 CALL comin_var_get([ep_atm_write_output_before], &
119 & t_comin_var_descriptor(name="example_fortran_var", id=1), &
120 & comin_flag_write, example_fortran_var)
121
122 CALL comin_var_get([ep_atm_write_output_before], &
123 & t_comin_var_descriptor(name="example_fortran_tracer", id=1), &
124 & comin_flag_write, example_fortran_tracer)
125
126 ! check some meta-data:
127 ! ... check the meaning of the dimensions:
128 dim_semantics = example_fortran_var%dim_semantics()
129 CALL assert( &
130 & all(dim_semantics(1:3) == [comin_dim_semantics_nproma, comin_dim_semantics_level, comin_dim_semantics_block]), &
131 & "'example_fortran_var' dimension semantics are nproma, level, block")
132 ! ... check "tracer" flag:
133 CALL comin_var_get_metadata(t_comin_var_descriptor(name="example_fortran_tracer", id=1), meta)
134 CALL meta%get("tracer", tracer)
135 CALL assert(tracer, "'example_fortran_tracer' is a tracer")
136 END SUBROUTINE simple_fortran_constructor
137
138 ! --------------------------------------------------------------------
139 ! ComIn callback function.
140 ! This function is called by ICON before writing output files.
141 ! --------------------------------------------------------------------
142 SUBROUTINE simple_fortran_diagfct() BIND(C)
143 INTEGER :: domain_id
144 REAL(wp), POINTER :: &
145 & example_fortran_ptr(:,:,:,:,:), pres_ptr(:,:,:,:,:), tracer_slice(:,:,:)
146
147 CALL comin_print_info("third party callback: before output.")
148
149 ! get some context information from ComIn:
150 domain_id = comin_current_get_domain_id()
151 IF (domain_id == comin_domain_outside_loop) THEN
152 CALL comin_print_info("currently not in domain loop")
153 ELSE
154 WRITE(text,'(a,i0)') " currently on domain ", domain_id
156 END IF
157
158 ! access the scalar field "example_fortran_var":
159 CALL example_fortran_var%get_ptr(example_fortran_ptr)
160 CALL pres%get_ptr(pres_ptr)
161 example_fortran_ptr = pres_ptr + 42
162
163 ! access the "example_fortran_tracer" via a 3D array pointer:
164 CALL example_fortran_tracer%to_3d(tracer_slice)
165 tracer_slice(1,9,1) = 1.0
166 END SUBROUTINE simple_fortran_diagfct
167
168END MODULE simple_fortran_plugin
void comin_plugin_finish(const char *routine, const char *text)
Opaque data type for a metadata handle.
void comin_print_info(const char *msg)
void comin_setup_get_version(unsigned int *major, unsigned int *minor, unsigned int *patch)
t_comin_metadata * comin_var_request_get_metadata(const t_comin_var_descriptor *desc)
void comin_var_request_add(const t_comin_var_descriptor *var_desc, bool lmodexclusive)
t_comin_var_handle comin_var_get(int context_len, const t_comin_entry_point context[], const t_comin_var_descriptor *var_descriptor, int flag)
Get a variable handle for the given entry points.
const t_comin_metadata * comin_var_get_metadata(const t_comin_var_descriptor *desc)
int comin_current_get_domain_id()
void comin_callback_register(t_comin_entry_point entry_point, t_comin_callback_function fct_ptr)
Example plugin for the ICON Community Interface (ComIn).
type(t_comin_var_handle) example_fortran_var
type(t_comin_var_handle) example_fortran_tracer
integer, parameter wp
working precision
subroutine assert(cond, message)
character(len=200) text
text buffer for printing
type(t_comin_descrdata_global) p_global
access descriptive data structures
type(t_comin_var_handle) pres
handles for custom ICON variables