YAC 3.14.0
Yet Another Coupler
Loading...
Searching...
No Matches
dummy_atmosphere_c.c
Go to the documentation of this file.
1// Copyright (c) 2024 The YAC Authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#include <mpi.h>
6
7#include <stdlib.h>
8#include <stdio.h>
9#include <unistd.h>
10#include <string.h>
11#include "yac.h"
12
17#define NBR_CELLS 2
18#define NBR_VERTICES 4
19#define YAC_RAD (0.01745329251994329576923690768489) // M_PI / 180
20
21#define STR_USAGE "Usage: %s -c configFilename\n"
22#define YAC_ASSERT_ARGS(exp, msg) \
23 { \
24 if(!((exp))) { \
25 fprintf(stderr, "ERROR: %s\n" STR_USAGE, msg, argv[0]); \
26 exit(EXIT_FAILURE); \
27 } \
28 }
29
30static void parse_arguments(
31 int argc, char ** argv, char const ** configFilename);
32
33int main (int argc, char *argv[]) {
34
35 const int no_of_fields = 14;
36
37 const char * fieldName[] = { "surface_downward_eastward_stress", // bundled field containing two components
38 "surface_downward_northward_stress", // bundled field containing two components
39 "surface_fresh_water_flux", // bundled field containing three components
40 "surface_temperature",
41 "total_heat_flux", // bundled field containing four components
42 "atmosphere_sea_ice_bundle", // bundled field containing four components
43 "sea_surface_temperature",
44 "eastward_sea_water_velocity",
45 "northward_sea_water_velocity",
46 "ocean_sea_ice_bundle",
47 "atmos_out1", // output field
48 "atmos_out2", // output field
49 "atmos_out3", // output field
50 "atmos_out4"}; // output field
51 int field_collection_size[] = {
52 2,
53 2,
54 3,
55 1,
56 4,
57 4,
58 1,
59 1,
60 1,
61 5,
62 1,
63 1,
64 1,
65 1};
66
67 char * comp_name = "dummy_atmosphere";
68 char * grid_name = "dummy_atmosphere_grid";
69
70 const int nbr_cells = NBR_CELLS;
71 const int nbr_vertices = NBR_VERTICES;
73
74 int i, info, ierror;
75 int comp_id;
76
77 int cell_point_ids[1];
78
79 int grid_id;
80
83
84 double * buffer;
85 double * buffer_lon;
86 double * buffer_lat;
87 int * cell_to_vertex;
88
89 int * cell_mask;
90 int * field_id;
91
92 MPI_Comm local_comm;
93
94 int size, rank;
95
96 MPI_Init (0, NULL);
97
98 // Initialise the coupler
99 char const * configFilename = "toy_dummy.yaml"; // default configuration file
100 parse_arguments(argc, argv, &configFilename);
101 yac_cinit ();
102 yac_cread_config_yaml(configFilename);
103
104 // Inform the coupler about what we are
105 yac_cdef_comp ( comp_name, &comp_id );
106
107 printf ( "YAC Version %s\n", yac_cget_version() );
108
109 yac_cget_comp_comm ( comp_id, &local_comm );
110
111 MPI_Comm_rank ( local_comm, &rank );
112 MPI_Comm_size ( local_comm, &size );
113
114 printf (" %s rank %d : local size %d \n", comp_name, rank, size );
115
116 buffer_lon = malloc ( nbr_vertices * sizeof(*buffer_lon) );
117 buffer_lat = malloc ( nbr_vertices * sizeof(*buffer_lat) );
118 cell_to_vertex = malloc ( 3 * nbr_cells * sizeof(*cell_to_vertex) );
119
120 /* Define vertices
121
122 0
123 / \
124 / o \
125 / \
126 1-------2 Eq.
127 \ /
128 \ o /
129 \ /
130 3
131
132 */
133
134 buffer_lon[0] = 0.0 * YAC_RAD; buffer_lat[0] = 1.0 * YAC_RAD;
135 buffer_lon[1] = -1.0 * YAC_RAD; buffer_lat[1] = 0.0 * YAC_RAD;
136 buffer_lon[2] = 1.0 * YAC_RAD; buffer_lat[2] = 0.0 * YAC_RAD;
137 buffer_lon[3] = 0.0 * YAC_RAD; buffer_lat[3] = -1.0 * YAC_RAD;
138
139 // Connectivity
140
141 cell_to_vertex[0] = 0; cell_to_vertex[1] = 1; cell_to_vertex[2] = 2; // cell 1
142 cell_to_vertex[3] = 1; cell_to_vertex[4] = 3; cell_to_vertex[5] = 2; // cell 2
143
144 for ( i = 0; i < nbr_cells; ++i ) nbr_vertices_per_cell[i] = 3;
145
146 // Description of elements, here as unstructured grid
147
148 yac_cdef_grid_unstruct ( grid_name,
149 nbr_vertices,
150 nbr_cells,
155 &grid_id );
156
157 // Decomposition information
158
159 for ( i = 0; i < nbr_cells; ++i ) {
160 global_index[i] = i;
161 cell_core_mask[i] = 1;
162 }
163
166
167 // Center points in cells (needed e.g. for nearest neighbour)
168
169 buffer_lon[0] = 0.0 * YAC_RAD; buffer_lat[0] = 0.5 * YAC_RAD;
170 buffer_lon[1] = 0.0 * YAC_RAD; buffer_lat[1] = -0.5 * YAC_RAD;
171
173 nbr_cells,
177 &cell_point_ids[0] );
178
179 free ( buffer_lon );
180 free ( buffer_lat );
181
182 // Mask generation
183
184 cell_mask = malloc (nbr_cells * sizeof(*cell_mask));
185
186 for ( i = 0; i < nbr_cells; ++i )
187 cell_mask[i] = 1;
188
190
191 free (cell_mask);
192
193 field_id = malloc ( no_of_fields * sizeof(*field_id));
194
195 for ( i = 0; i < no_of_fields; ++i )
197 comp_id,
199 1,field_collection_size[i],
201 &field_id[i] );
202
203 yac_cenddef ( );
204
205 // these queries can only be fulfilled once the search has been completed.
206
207 for ( i = 0; i < no_of_fields; ++i ) {
208 const char* timestep_string = yac_cget_timestep_from_field_id ( field_id[i] );
209 printf ( "Field ID %d: model step %s \n", field_id[i], timestep_string);
210 }
211
212 // Data exchange
213
214 buffer = malloc (5 * nbr_cells * sizeof (*buffer));
215
216 /* field_id[0] represents "TAUX" wind stress component
217 field_id[1] represents "TAUY" wind stress component
218 field_id[2] represents "SFWFLX" surface fresh water flux
219 field_id[3] represents "SFTEMP" surface temperature
220 field_id[4] represents "THFLX" total heat flux
221 field_id[5] represents "ICEATM" ice temperatures and melt potential
222
223 field_id[6] represents "SST" sea surface temperature
224 field_id[7] represents "OCEANU" u component of ocean surface current
225 field_id[8] represents "OCEANV" v component of ocean surface current
226 field_id[9]represents "ICEOCE" ice thickness, concentration and temperatures
227 */
228
229 // Send fields from atmosphere to ocean
230 // ------------------------------------
231
232 {
233 double *point_set_data[5];
234 double **collection_data[5];
235
236 for (int j = 0; j < 5; ++j) {
237 point_set_data[j] = buffer + j * nbr_cells;
238 collection_data[j] = &(point_set_data[j]);
239 }
240
241 // meridional wind stress
242 for ( i = 0; i < nbr_cells; ++i ) buffer[0*nbr_cells+i] = 10.1;
243 for ( i = 0; i < nbr_cells; ++i ) buffer[1*nbr_cells+i] = 10.2;
244
245 yac_cput ( field_id[0], 2, collection_data, &info, &ierror );
246 if ( info > 0 )
247 printf ( "atmosphere CPL TAUX 1 %f \n"
248 "atmosphere CPL TAUX 2 %f \n",
249 buffer[0*nbr_cells+0], buffer[1*nbr_cells+0]);
250
251 // zonal wind stress
252 for ( i = 0; i < nbr_cells; ++i ) buffer[0*nbr_cells+i] = 20.1;
253 for ( i = 0; i < nbr_cells; ++i ) buffer[1*nbr_cells+i] = 20.2;
254
255 yac_cput ( field_id[1], 2, collection_data, &info, &ierror );
256 if ( info > 0 )
257 printf ( "atmosphere CPL TAUY 1 %f \n"
258 "atmosphere CPL TAUY 2 %f \n",
259 buffer[0*nbr_cells+0], buffer[1*nbr_cells+0]);
260
261 // surface fresh water flux
262 for ( i = 0; i < nbr_cells; ++i ) buffer[0*nbr_cells+i] = 30.1;
263 for ( i = 0; i < nbr_cells; ++i ) buffer[1*nbr_cells+i] = 30.2;
264 for ( i = 0; i < nbr_cells; ++i ) buffer[2*nbr_cells+i] = 30.3;
265
266 yac_cput ( field_id[2], 3, collection_data, &info, &ierror );
267
268 // surface temperature
269
270 for ( i = 0; i < nbr_cells; ++i ) buffer[0*nbr_cells+i] = 40.1;
271
272 yac_cput ( field_id[3], 1, collection_data, &info, &ierror );
273
274 // total heat flux
275
276 for ( i = 0; i < nbr_cells; ++i ) buffer[0*nbr_cells+i] = 50.1;
277 for ( i = 0; i < nbr_cells; ++i ) buffer[1*nbr_cells+i] = 50.2;
278 for ( i = 0; i < nbr_cells; ++i ) buffer[2*nbr_cells+i] = 50.3;
279 for ( i = 0; i < nbr_cells; ++i ) buffer[3*nbr_cells+i] = 50.4;
280
281 yac_cput ( field_id[4], 4, collection_data, &info, &ierror );
282
283 // ice temperatures and melt potential
284
285 for ( i = 0; i < nbr_cells; ++i ) buffer[0*nbr_cells+i] = 60.1;
286 for ( i = 0; i < nbr_cells; ++i ) buffer[1*nbr_cells+i] = 60.2;
287 for ( i = 0; i < nbr_cells; ++i ) buffer[2*nbr_cells+i] = 60.3;
288 for ( i = 0; i < nbr_cells; ++i ) buffer[3*nbr_cells+i] = 60.4;
289
290 yac_cput ( field_id[5], 4, collection_data, &info, &ierror );
291 }
292
293 // Receive fields from ocean
294 // -------------------------
295
296 {
297 double *collection_data[5];
298
299 for (int j = 0; j < 5; ++j) {
300 collection_data[j] = buffer + j * nbr_cells;
301 }
302
303 // SST
304
305 yac_cget ( field_id[6], 1, collection_data, &info, &ierror );
306 if ( info > 0 ) printf ( "atmosphere CPL SST %f \n", buffer[0] );
307
308 // zonal velocity
309
310 yac_cget ( field_id[7], 1, collection_data, &info, &ierror );
311 if ( info > 0 ) printf ( "atmosphere CPL OCEANU %f \n", buffer[0] );
312
313 // meridional velocity
314
315 yac_cget ( field_id[8], 1, collection_data, &info, &ierror );
316 if ( info > 0 ) printf ( "atmosphere CPL OCEANV %f \n", buffer[0] );
317
318 // Ice thickness, concentration, T1 and T2
319
320 yac_cget ( field_id[9], 5, collection_data, &info, &ierror );
321 if ( info > 0 ) {
322 printf ( "atmosphere CPL ice 1 %f \n", buffer[0*nbr_cells+0] );
323 printf ( "atmosphere CPL ice 2 %f \n", buffer[1*nbr_cells+0] );
324 printf ( "atmosphere CPL ice 3 %f \n", buffer[2*nbr_cells+0] );
325 printf ( "atmosphere CPL ice 4 %f \n", buffer[3*nbr_cells+0] );
326 printf ( "atmosphere CPL ice 5 %f \n", buffer[4*nbr_cells+0] );
327 }
328 }
329
330 free (buffer);
331 free (field_id);
332
333 yac_cfinalize ();
334
335 MPI_Finalize ();
336
337 return 0;
338}
339
340static void parse_arguments(
341 int argc, char ** argv, char const ** configFilename) {
342
343 int opt;
344 while ((opt = getopt(argc, argv, "c:")) != -1) {
345 YAC_ASSERT_ARGS((opt == 'c'), "invalid command argument")
346 switch (opt) {
347 default:
348 case 'c':
349 *configFilename = optarg;
350 break;
351 }
352 }
353}
#define NBR_CELLS
#define NBR_VERTICES
#define YAC_RAD
const char * fieldName[]
int nbr_vertices_per_cell[NBR_CELLS]
int * cell_to_vertex
int * cell_mask
double * buffer_lat
double * buffer_lon
int cell_point_ids[1]
int info
const int no_of_fields
int * cell_core_mask
int grid_id
double * buffer
int ierror
int * global_index
int * field_id
int comp_id
static void parse_arguments(int argc, char **argv, enum experiment_type *experiment)
Definition toy_scrip.c:490
#define YAC_ASSERT_ARGS(exp, msg)
Definition toy_scrip.c:145
void yac_cenddef(void)
Definition yac.c:4414
void yac_cset_global_index(int const *global_index, int location, int grid_id)
Definition yac.c:5063
int const YAC_LOCATION_CELL
Definition yac.c:34
const char * yac_cget_timestep_from_field_id(int field_id)
Definition yac.c:4722
void yac_cinit(void)
Definition yac.c:514
void yac_cfinalize()
Finalises YAC.
Definition yac.c:740
void yac_cget_comp_comm(int comp_id, MPI_Comm *comp_comm)
Definition yac.c:856
void yac_cget(int const field_id, int collection_size, double **recv_field, int *info, int *ierr)
Definition yac.c:2759
int const YAC_TIME_UNIT_SECOND
Definition yac.c:59
void yac_cdef_grid_unstruct(const char *grid_name, int nbr_vertices, int nbr_cells, int *num_vertices_per_cell, double *x_vertices, double *y_vertices, int *cell_to_vertex, int *grid_id)
Definition yac.c:4881
void yac_cdef_points_unstruct(int const grid_id, int const nbr_points, int const located, double const *x_points, double const *y_points, int *point_id)
Definition yac.c:1181
char * yac_cget_version(void)
Definition yac.c:826
void yac_cput(int const field_id, int const collection_size, double ***const send_field, int *info, int *ierr)
Definition yac.c:3721
void yac_cread_config_yaml(const char *yaml_filename)
Definition yac.c:595
void yac_cset_core_mask(int const *is_core, int location, int grid_id)
Definition yac.c:5079
void yac_cset_mask(int const *is_valid, int points_id)
Definition yac.c:1304
void yac_cdef_comp(char const *comp_name, int *comp_id)
Definition yac.c:1013
void yac_cdef_field(char const *name, int const comp_id, int const *point_ids, int const num_pointsets, int collection_size, const char *timestep, int time_unit, int *field_id)
Definition yac.c:1396