YAC 3.14.0
Yet Another Coupler
Loading...
Searching...
No Matches
dummy_ocean_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 "ocean_out1", // output field
48 "ocean_out2", // output field
49 "ocean_out3", // output field
50 "ocean_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_ocean";
68 char * grid_name = "dummy_ocean_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 // Define unstructured grid
147
149 grid_name, nbr_vertices, nbr_cells, nbr_vertices_per_cell,
151
152 // Decomposition information
153
154 for (int i = 0; i < nbr_cells; ++i ) {
155 global_index[i] = i;
156 cell_core_mask[i] = 1;
157 }
158
161
162 // Center points in cells (needed e.g. for nearest neighbour)
163
164 buffer_lon[0] = 0.0 * YAC_RAD; buffer_lat[0] = 0.5 * YAC_RAD;
165 buffer_lon[1] = 0.0 * YAC_RAD; buffer_lat[1] = -0.5 * YAC_RAD;
166
169 &cell_point_ids[0]);
170
171 free ( buffer_lon );
172 free ( buffer_lat );
173
174 // Mask generation
175
176 cell_mask = malloc (nbr_cells * sizeof(*cell_mask));
177
178 for (int i = 0; i < nbr_cells; ++i) cell_mask[i] = 1;
179
181
182 free (cell_mask);
183
184 field_id = malloc ( no_of_fields * sizeof(*field_id));
185
186 for ( i = 0; i < no_of_fields; ++i )
188 comp_id,
190 1,field_collection_size[i],
192 &field_id[i] );
193
194 yac_cenddef ( );
195
196 // these queries can only be fulfilled once the search has been completed.
197
198 for ( i = 0; i < no_of_fields; ++i ) {
199 const char* timestep_string = yac_cget_timestep_from_field_id ( field_id[i] );
200 printf ( "Field ID %d: model step %s \n", field_id[i], timestep_string);
201 }
202
203 // Data exchange
204
205 buffer = malloc (5 * nbr_cells * sizeof (*buffer));
206
207 /* field_id[0] represents "TAUX" wind stress component
208 field_id[1] represents "TAUY" wind stress component
209 field_id[2] represents "SFWFLX" surface fresh water flux
210 field_id[3] represents "SFTEMP" surface temperature
211 field_id[4] represents "THFLX" total heat flux
212 field_id[5] represents "ICEATM" ice temperatures and melt potential
213
214 field_id[6] represents "SST" sea surface temperature
215 field_id[7] represents "OCEANU" u component of ocean surface current
216 field_id[8] represents "OCEANV" v component of ocean surface current
217 field_id[9]represents "ICEOCE" ice thickness, concentration and temperatures
218 */
219
220 // Send fields from ocean to atmosphere
221 // ------------------------------------
222
223 {
224 double *point_set_data[5];
225 double **collection_data[5];
226
227 for (int j = 0; j < 5; ++j) {
228 point_set_data[j] = buffer + j * nbr_cells;
229 collection_data[j] = &(point_set_data[j]);
230 }
231
232 // SST
233 for ( i = 0; i < nbr_cells; ++i ) buffer[0*nbr_cells+i] = 110.1;
234
235 yac_cput ( field_id[6], 1, collection_data, &info, &ierror );
236 if ( info > 0 ) printf ( "ocean CPL SST %f \n", buffer[0*nbr_cells+0]);
237
238 // zonal velocity
239
240 for ( i = 0; i < nbr_cells; ++i ) buffer[0*nbr_cells+i] = 120.1;
241
242 yac_cput ( field_id[7], 1, collection_data, &info, &ierror );
243 if ( info > 0 ) printf ( "ocean CPL U %f \n", buffer[0*nbr_cells+0]);
244
245 // meridional velocity
246
247 for ( i = 0; i < nbr_cells; ++i ) buffer[0*nbr_cells+i] = 130.1;
248
249 yac_cput ( field_id[8], 1, collection_data, &info, &ierror );
250 if ( info > 0 ) printf ( "ocean CPL V %f \n", buffer[0*nbr_cells+0]);
251
252 // Ice thickness, concentration, T1 and T2
253
254 for ( i = 0; i < nbr_cells; ++i ) {
255 buffer[0*nbr_cells+i] = 140.1;
256 buffer[1*nbr_cells+i] = 140.2;
257 buffer[2*nbr_cells+i] = 140.3;
258 buffer[3*nbr_cells+i] = 140.4;
259 buffer[4*nbr_cells+i] = 140.5;
260 }
261 point_set_data[0] = buffer;
262
263 yac_cput ( field_id[9], 5, collection_data, &info, &ierror );
264 if ( info > 0 )
265 printf (
266 "ocean CPL ice 1 %f \n"
267 "ocean CPL ice 2 %f \n"
268 "ocean CPL ice 3 %f \n"
269 "ocean CPL ice 4 %f \n"
270 "ocean CPL ice 5 %f \n",
271 buffer[0*nbr_cells+0], buffer[1*nbr_cells+0], buffer[2*nbr_cells+0],
272 buffer[3*nbr_cells+0], buffer[4*nbr_cells+0]);
273 }
274
275 // Receive fields from atmosphere
276 // ------------------------------
277
278 {
279 double *collection_data[5];
280
281 for (int j = 0; j < 5; ++j) {
282 collection_data[j] = buffer + j * nbr_cells;
283 }
284
285 // zonal wind stress
286
287 yac_cget ( field_id[0], 2, collection_data, &info, &ierror );
288
289 // meridional wind stress
290
291 yac_cget ( field_id[1], 2, collection_data, &info, &ierror );
292
293 // freshwater flux
294
295 yac_cget ( field_id[2], 3, collection_data, &info, &ierror );
296
297 // surface air temperature
298 yac_cget ( field_id[3], 1, collection_data, &info, &ierror );
299
300 // total heat flux - 4 parts - record 5
301
302 yac_cget ( field_id[4], 4, collection_data, &info, &ierror );
303
304 // ice parameter
305
306 yac_cget ( field_id[5], 4, collection_data, &info, &ierror );
307 }
308
309 free (buffer);
310 free (field_id);
311
312 yac_cfinalize ();
313
314 MPI_Finalize ();
315
316 return 0;
317}
318
319static void parse_arguments(
320 int argc, char ** argv, char const ** configFilename) {
321
322 int opt;
323 while ((opt = getopt(argc, argv, "c:")) != -1) {
324 YAC_ASSERT_ARGS((opt == 'c'), "invalid command argument")
325 switch (opt) {
326 default:
327 case 'c':
328 *configFilename = optarg;
329 break;
330 }
331 }
332}
#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