ICON Community Interface 0.4.0
Loading...
Searching...
No Matches
simple_c_plugin.c
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 FORTRAN, see the subdirectory
8 "simple".
9
10 @authors 11/2023 :: ICON Community Interface <comin@icon-model.org>
11
12 SPDX-License-Identifier: BSD-3-Clause
13
14 Please see the file LICENSE in the root of the source tree for this code.
15 Where software is supplied by third parties, it is indicated in the
16 headers of the routines.
17*/
18
19#include <comin.h>
20#include <stdbool.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25// points to the pointer to the buffer (buffer swapping)
27
29 unsigned int major, minor, patch;
30 comin_setup_get_version(&major, &minor, &patch);
31 comin_print_info_f("ComIn v%u.%u.%u simple_c_constructor called!", major,
32 minor, patch);
33
34 t_comin_var_descriptor desc;
35 for (void *it = comin_var_get_descr_list_head(); it != NULL;
36 it = comin_var_get_descr_list_next(it)) {
37 comin_var_get_descr_list_var_desc(it, &desc);
38 comin_print_info_f("found variable: %s on domain id %d", desc.name,
39 desc.id);
40 }
41
42 // test explicit free'ing of a var descriptor list iterator:
43 {
44 t_comin_var_descr_list_iterator *descr_it = comin_var_get_descr_list_head();
45 descr_it = comin_var_get_descr_list_next(descr_it);
46 comin_var_descr_list_iterator_delete(&descr_it);
47 }
48
49 t_comin_var_descriptor pres_d = {.name = "pres", .id = 1};
50 t_comin_entry_point before_output = EP_ATM_WRITE_OUTPUT_BEFORE;
51 pres = comin_var_get(1, &before_output, pres_d, COMIN_FLAG_READ);
52 if (pres == NULL) {
53 comin_plugin_finish("simple_c_plugin", "Internal error!");
54 }
55
56 t_comin_var_descriptor var_d = {.name = "simple_c_var", .id = 1};
57 simple_c_var = comin_var_get(1, &before_output, var_d, COMIN_FLAG_WRITE);
58 if (simple_c_var == NULL) {
59 comin_plugin_finish("simple_c_plugin", "Internal error!");
60 }
61
62 t_comin_var_descriptor tracer_d = {.name = "simple_c_tracer", .id = 1};
64 comin_var_get(1, &before_output, tracer_d, COMIN_FLAG_WRITE);
65 if (simple_c_tracer == NULL) {
66 comin_plugin_finish("simple_c_plugin", "Internal error!");
67 }
68
69 comin_print_info("Metadata:");
70 // iterator over the metadata:
71 void *end_it = comin_metadata_get_iterator_end(pres_d);
72 void *it = comin_metadata_get_iterator_begin(pres_d);
73 for (; !comin_metadata_iterator_compare(it, end_it);
74 comin_metadata_iterator_next(it)) {
75 const char *key = comin_metadata_iterator_get_key(it);
76 int type = comin_metadata_get_typeid(pres_d, key);
77 comin_print_info_f("%s type:%i", key, type);
78 if (type == COMIN_METADATA_TYPEID_INTEGER) {
79 int val = -999;
80 comin_metadata_get_integer(pres_d, key, &val);
81 comin_print_info_f(" value: %i", val);
82 }
83 }
84 comin_metadata_iterator_delete(it);
85 comin_metadata_iterator_delete(end_it);
86
87 // test "dim_semantics" auxiliary function, used for
88 // interpretation of array dimensions:
89 int dim_semantics[5];
90 comin_var_get_dim_semantics(pres, dim_semantics);
91 if (dim_semantics[0] != COMIN_DIM_SEMANTICS_NPROMA) {
92 comin_plugin_finish("simple_c_plugin", "Internal error!");
93 }
94 if (dim_semantics[1] != COMIN_DIM_SEMANTICS_LEVEL) {
95 comin_plugin_finish("simple_c_plugin", "Internal error!");
96 }
97 if (dim_semantics[2] != COMIN_DIM_SEMANTICS_BLOCK) {
98 comin_plugin_finish("simple_c_plugin", "Internal error!");
99 }
100}
101
103 int jg;
104 comin_print_info("simple_c_diagfct called!");
105 jg = comin_current_get_domain_id();
106 comin_print_info_f("currently on domain %i", jg);
107 int pres_shape[5], tracer_shape[5];
108 comin_var_get_shape(pres, pres_shape);
109 double *simple_c_var_data = comin_var_get_ptr_double(simple_c_var);
110 double *pres_data = comin_var_get_ptr_double(pres);
111 for (int i = 0; i < pres_shape[0] * pres_shape[1] * pres_shape[2] *
112 pres_shape[3] * pres_shape[4];
113 ++i) {
114 simple_c_var_data[i] = pres_data[i] + 42.;
115 }
116 comin_var_get_shape(simple_c_tracer, tracer_shape);
117 double *simple_c_tracer_data = comin_var_get_ptr_double(simple_c_tracer);
118 for (int i = 0; i < tracer_shape[0] * tracer_shape[1] * tracer_shape[2];
119 ++i) {
120 simple_c_tracer_data[i] /= 1337.;
121 }
122}
123
124void simple_c_destructor() { comin_print_info("simple_c_destructor called!"); }
125
127 int ilen = -1;
128 const char *plugin_name = NULL;
129 comin_current_get_plugin_name(&plugin_name, &ilen);
130
131 int plugin_id = comin_current_get_plugin_id();
132 comin_print_info_f("plugin %.*s has id %d", ilen, plugin_name, plugin_id);
133 t_comin_var_descriptor simple_var_d = {.name = "simple_c_var", .id = 1};
134 comin_var_request_add(simple_var_d, true);
135
136 comin_metadata_set_integer(simple_var_d, "zaxis_id", COMIN_ZAXIS_3D);
137 comin_metadata_set_logical(simple_var_d, "restart", false);
138 comin_metadata_set_logical(simple_var_d, "tracer", false);
139 comin_metadata_set_integer(simple_var_d, "tracer_vlimit", 0);
140 comin_metadata_set_integer(simple_var_d, "tracer_hlimit", 0);
141
142 t_comin_var_descriptor simple_tracer_d = {.name = "simple_c_tracer",
143 .id = -1};
144 comin_var_request_add(simple_tracer_d, false);
145 comin_metadata_set_integer(simple_tracer_d, "zaxis_id", COMIN_ZAXIS_3D);
146 comin_metadata_set_logical(simple_tracer_d, "restart", false);
147 comin_metadata_set_logical(simple_tracer_d, "tracer", true);
148 comin_metadata_set_integer(simple_tracer_d, "tracer_vlimit", 0);
149 comin_metadata_set_integer(simple_tracer_d, "tracer_hlimit", 0);
150
151 comin_callback_register(EP_SECONDARY_CONSTRUCTOR, &simple_c_constructor);
152 comin_callback_register(EP_ATM_WRITE_OUTPUT_BEFORE, &simple_c_diagfct);
153 comin_callback_register(EP_DESTRUCTOR, &simple_c_destructor);
154
155 char ep_name[COMIN_MAX_LEN_EP_NAME + 1];
156 comin_callback_get_ep_name(EP_DESTRUCTOR, ep_name);
157 if (strncmp(ep_name, "EP_DESTRUCTOR", (size_t)13)) {
158 char output_text[255];
159 sprintf(output_text, "Expected EP_DESTRUCTOR; got |%s|\n", ep_name);
160 comin_plugin_finish("simple_c: comin_main", output_text);
161 }
162 // TODO: Access descriptive data structures
163
164 /* access to comin descriptive global data (exemplary)*/
166 comin_print_info_f("n_dom: %d", n_dom);
168 comin_print_info_f("max_dom: %d", max_dom);
170 comin_print_info_f("nproma: %d", nproma);
171 int min_rlcell_int = comin_descrdata_get_global_min_rlcell_int();
172 comin_print_info_f("min_rlcell_int: %d", min_rlcell_int);
173}
C interface for the ICON Community Interface.
void comin_print_info_f(const char *fmt,...)
integer(c_int) function comin_descrdata_get_global_n_dom()
integer(c_int) function comin_descrdata_get_global_min_rlcell_int()
integer(c_int) function comin_descrdata_get_global_nproma()
integer(c_int) function comin_descrdata_get_global_max_dom()
void simple_c_constructor()
void * simple_c_tracer
void simple_c_diagfct()
void simple_c_destructor()
void * pres
void * simple_c_var
void comin_main()
char name[COMIN_MAX_LEN_VAR_NAME+1]
Definition comin.h:37
const int jg