YAC 3.12.0
Yet Another Coupler
Loading...
Searching...
No Matches
toy_multi_common.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// #define VERBOSE
6
7#include <mpi.h>
8#include <stdlib.h>
9#include <stdio.h>
10#include <unistd.h>
11#include <math.h>
12#include <string.h>
13#include <stdint.h>
14#include "yac.h"
15#include "yac_core.h"
16#include "yac_utils.h"
17
18#include "toy_multi_common.h"
19
20/* ------------------------------------------------- */
21
22struct {
23 char const * name;
24 int const * location;
26 {
27 {.name = "conserv",
28 .location = &YAC_LOCATION_CELL},
29 {.name = "2nd_conserv",
30 .location = &YAC_LOCATION_CELL},
31 {.name = "avg",
32 .location = &YAC_LOCATION_CORNER},
33 {.name = "hcsbb",
34 .location = &YAC_LOCATION_CELL},
35 {.name = "rbf",
36 .location = &YAC_LOCATION_CELL},
37 };
38size_t nbr_interpolations = sizeof(interpolations) / sizeof(interpolations[0]);
39
41 char const * comp_name, int comp_id, int grid_id,
42 int cell_point_id, int corner_point_id,
43 double * cell_point_data, double * corner_point_data,
44 YAC_VTK_FILE * vtk_file) {
45
46 int info, err;
47 double tic;
48
49 int rank;
50 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
51
52 MPI_Barrier(MPI_COMM_WORLD);
53 tic=MPI_Wtime();
54
55 // get list of all registered components
56 int nbr_comps = yac_cget_nbr_comps();
57 char const ** comp_names = malloc((size_t)nbr_comps * sizeof(*comp_names));
58 yac_cget_comp_names(nbr_comps, comp_names);
59 size_t local_comp_idx = SIZE_MAX;
60 for (int i = 0; i < nbr_comps; ++i) comp_names[i] = strdup(comp_names[i]);
61
62 // register fields
63 int * field_ids =
64 malloc((size_t)nbr_comps * nbr_interpolations * sizeof(*field_ids));
65 for (int comp_idx = 0, i = 0; comp_idx < nbr_comps; ++comp_idx) {
66 if (!strcmp(comp_name, comp_names[comp_idx]))
67 local_comp_idx = (size_t)comp_idx;
68 for (size_t interp_idx = 0; interp_idx < nbr_interpolations;
69 ++interp_idx, ++i) {
70 char field_name[256];
71 sprintf(
72 field_name, "%s_%s_field_out", interpolations[interp_idx].name,
73 comp_names[comp_idx]);
74 int * point_id_ptr;
76 (*(interpolations[interp_idx].location) == YAC_LOCATION_CELL) ||
78 "invalid location");
79 if (*(interpolations[interp_idx].location) == YAC_LOCATION_CELL)
80 point_id_ptr = &cell_point_id;
81 else
82 point_id_ptr = &corner_point_id;
84 field_name, comp_id, point_id_ptr, 1, 1, "2",
85 YAC_TIME_UNIT_SECOND, &field_ids[i]);
86 }
87 }
88
89 // define couples
90 if (rank == 0) {
91 for (size_t interp_idx = 0; interp_idx < nbr_interpolations;
92 ++interp_idx) {
93
94 int interp_config;
95 yac_cget_interp_stack_config(&interp_config);
96 char const * interp_name = interpolations[interp_idx].name;
97 if (!strcmp(interp_name, "conserv")) {
99 interp_config, 1, 0, 0, YAC_CONSERV_DESTAREA);
100 } else if (!strcmp(interp_name, "2nd_conserv")) {
102 interp_config, 2, 0, 0, YAC_CONSERV_DESTAREA);
103 } else if (!strcmp(interp_name, "avg")) {
105 interp_config, YAC_AVG_BARY, 1);
106 } else if (!strcmp(interp_name, "hcsbb")) {
108 } else if (!strcmp(interp_name, "rbf")) {
110 interp_config, YAC_NNN_RBF, 9,
112 } else {
113 fprintf(
114 stderr, "unsupported interpolation type: \"%s\"\n", interp_name);
115 exit(EXIT_FAILURE);
116 }
117 yac_cadd_interp_stack_config_fixed(interp_config, -1.0);
118
119 for (int src_comp_idx = 0; src_comp_idx < nbr_comps; ++src_comp_idx) {
120
121 char src_grid_name[256];
122 sprintf(src_grid_name, "%s_grid", comp_names[src_comp_idx]);
123
124 for (int tgt_comp_idx = 0; tgt_comp_idx < nbr_comps; ++tgt_comp_idx) {
125
126 if (src_comp_idx == tgt_comp_idx) continue;
127
128 char tgt_grid_name[256];
129 sprintf(tgt_grid_name, "%s_grid", comp_names[tgt_comp_idx]);
130
131 char field_name[256];
132 sprintf(
133 field_name, "%s_%s_field_out", interp_name,
134 comp_names[src_comp_idx]);
135
137 comp_names[src_comp_idx], src_grid_name, field_name,
138 comp_names[tgt_comp_idx], tgt_grid_name, field_name,
140 interp_config, 0, 0);
141 }
142 }
143
144 yac_cfree_interp_stack_config(interp_config);
145 }
146 }
147
148 MPI_Barrier(MPI_COMM_WORLD);
149 if (rank == 0)
150 printf(
151 "toy_multi_common(%s): Time for field/interpolation definition %f\n",
152 comp_name, MPI_Wtime()-tic);
153
154 // finalize initialisation phase
155
156 MPI_Barrier(MPI_COMM_WORLD);
157 tic=MPI_Wtime();
158
159 yac_cenddef ( );
160
161 MPI_Barrier(MPI_COMM_WORLD);
162 if (rank == 0)
163 printf(
164 "toy_multi_common(%s): Time for search %f\n",
165 comp_name, MPI_Wtime()-tic);
166
167 // initialise data
168
169 double ** field_buffers =
170 malloc((size_t)nbr_comps * nbr_interpolations * sizeof(*field_buffers));
171 for (int comp_idx = 0, i = 0; comp_idx < nbr_comps; ++comp_idx) {
172 for (size_t interp_idx = 0; interp_idx < nbr_interpolations;
173 ++interp_idx, ++i) {
174
175 if ((size_t)comp_idx == local_comp_idx) {
176 field_buffers[i] =
177 (*(interpolations[interp_idx].location) == YAC_LOCATION_CELL)?
178 cell_point_data:corner_point_data;
179 } else {
180 int point_id =
181 (*(interpolations[interp_idx].location) == YAC_LOCATION_CELL)?
182 cell_point_id:corner_point_id;
183 size_t data_size = yac_cget_points_size(point_id);
184 field_buffers[i] = malloc(data_size * sizeof(**field_buffers));
185 for (size_t j = 0; j < data_size; ++j)
186 field_buffers[i][j] = -10.0;
187 }
188 }
189 }
190
191 // exchange/interpolate fields
192
193 MPI_Barrier(MPI_COMM_WORLD);
194 tic=MPI_Wtime();
195
196 for (size_t interp_idx = 0, offset = local_comp_idx * nbr_interpolations;
197 interp_idx < nbr_interpolations; ++interp_idx) {
198
199 double *point_set_data[1] = {field_buffers[offset + interp_idx]};
200 double **collection_data[1] = {point_set_data};
201
202 yac_cput(field_ids[offset + interp_idx], 1, collection_data, &info, &err);
203 }
204
205 for (int comp_idx = 0, i = 0; comp_idx < nbr_comps; ++comp_idx) {
206 for (size_t interp_idx = 0; interp_idx < nbr_interpolations;
207 ++interp_idx, ++i) {
208
209 if ((size_t)comp_idx == local_comp_idx) continue;
210
211 double *collection_data[1];
212
213 collection_data[0] = field_buffers[i];
214 yac_cget(field_ids[i], 1, collection_data, &info, &err);
215 }
216 }
217
218 MPI_Barrier(MPI_COMM_WORLD);
219 if (rank == 0)
220 printf(
221 "toy_multi_common(%s): Time for exchange %f\n",
222 comp_name, MPI_Wtime()-tic);
223
224 // write fields to vtk output file
225
227 vtk_file, cell_point_data,
230 vtk_file, corner_point_data,
232
233 for (int comp_idx = 0, i = 0; comp_idx < nbr_comps; ++comp_idx) {
234 for (size_t interp_idx = 0; interp_idx < nbr_interpolations;
235 ++interp_idx, ++i) {
236
237 if ((size_t)comp_idx != local_comp_idx) {
238
239 char field_name[256];
240 sprintf(
241 field_name, "%s_in_%s", interpolations[interp_idx].name,
242 comp_names[comp_idx]);
243
244 if (*(interpolations[interp_idx].location) == YAC_LOCATION_CELL)
246 vtk_file, field_buffers[i],
248 *(interpolations[interp_idx].location), grid_id), field_name);
249 else
251 vtk_file, field_buffers[i],
253 *(interpolations[interp_idx].location), grid_id), field_name);
254 }
255 }
256 }
257
258 // finalize
259
260 yac_vtk_close(vtk_file);
262 MPI_Finalize();
263
264 // clean up
265
266 for (int comp_idx = 0, i = 0; comp_idx < nbr_comps; ++comp_idx)
267 for (size_t interp_idx = 0; interp_idx < nbr_interpolations;
268 ++interp_idx, ++i)
269 if ((size_t)comp_idx != local_comp_idx) free(field_buffers[i]);
270 free(field_buffers);
271
272 return EXIT_SUCCESS;
273}
#define YAC_ASSERT(exp, msg)
#define YAC_INTERP_NNN_MAX_SEARCH_DISTANCE_DEFAULT
char const src_grid_name[]
char const tgt_grid_name[]
int info
int grid_id
int cell_point_id
int comp_id
size_t nbr_interpolations
char const * name
int const * location
int run_toy_multi_common(char const *comp_name, int comp_id, int grid_id, int cell_point_id, int corner_point_id, double *cell_point_data, double *corner_point_data, YAC_VTK_FILE *vtk_file)
struct @6 interpolations[]
void yac_vtk_write_cell_scalars_double(YAC_VTK_FILE *vtk_file, double *scalars, unsigned num_cells, char *name)
Definition vtk_output.c:264
void yac_vtk_write_point_scalars_double(YAC_VTK_FILE *vtk_file, double *scalars, unsigned num_points, char *name)
Definition vtk_output.c:292
void yac_vtk_close(YAC_VTK_FILE *vtk_file)
Definition vtk_output.c:403
void yac_cenddef(void)
Definition yac.c:4363
int const YAC_NNN_RBF
Definition yac.c:79
int const YAC_LOCATION_CELL
Definition yac.c:31
void yac_cfinalize()
Finalises YAC.
Definition yac.c:717
int const YAC_LOCATION_CORNER
Definition yac.c:32
int const YAC_CONSERV_DESTAREA
Definition yac.c:82
void yac_cget(int const field_id, int collection_size, double **recv_field, int *info, int *ierr)
Definition yac.c:2729
int const YAC_TIME_UNIT_SECOND
Definition yac.c:56
void yac_cadd_interp_stack_config_conservative(int interp_stack_config_id, int order, int enforced_conserv, int partial_coverage, int normalisation)
Definition yac.c:5204
void yac_cput(int const field_id, int const collection_size, double ***const send_field, int *info, int *ierr)
Definition yac.c:3684
void yac_cget_comp_names(int nbr_comps, const char **comp_names)
Definition yac.c:4524
void yac_cadd_interp_stack_config_hcsbb(int interp_stack_config_id)
Definition yac.c:5741
void yac_cadd_interp_stack_config_fixed(int interp_stack_config_id, double value)
Definition yac.c:5785
void yac_cfree_interp_stack_config(int interp_stack_config_id)
Definition yac.c:5114
void yac_cadd_interp_stack_config_nnn(int interp_stack_config_id, int type, size_t n, double max_search_distance, double scale)
Definition yac.c:5171
size_t yac_cget_grid_size(int located, int grid_id)
Definition yac.c:5068
void yac_cget_interp_stack_config(int *interp_stack_config_id)
Definition yac.c:5100
void yac_cadd_interp_stack_config_average(int interp_stack_config_id, int reduction_type, int partial_coverage)
Definition yac.c:5134
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:1373
size_t yac_cget_points_size(int points_id)
Definition yac.c:5088
void yac_cdef_couple(char const *src_comp_name, char const *src_grid_name, char const *src_field_name, char const *tgt_comp_name, char const *tgt_grid_name, char const *tgt_field_name, char const *coupling_timestep, int time_unit, int time_reduction, int interp_stack_config_id, int src_lag, int tgt_lag)
Definition yac.c:1928
int yac_cget_nbr_comps(void)
Definition yac.c:4433
int const YAC_AVG_BARY
Definition yac.c:71
int const YAC_REDUCTION_TIME_NONE
Definition yac.c:49