YAC 3.13.2
Yet Another Coupler
Loading...
Searching...
No Matches
toy_multi_unstruct_2.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 "yac.h"
12#include "yac_utils.h"
13
14#include "toy_multi_common.h"
15
16// redefine YAC assert macros
17#undef YAC_ASSERT
18#define STR_USAGE "Usage: %s -g gridFilename\n"
19#define YAC_ASSERT(exp, msg) \
20 { \
21 if(!((exp))) { \
22 fprintf(stderr, "ERROR: %s\n" STR_USAGE, msg, argv[0]); \
23 exit(EXIT_FAILURE); \
24 } \
25 }
26
27static void parse_arguments(
28 int argc, char ** argv, char const ** gridFilename);
29
30int main (int argc, char *argv[]) {
31
32 double tic;
33
34 // Initialisation of MPI
35
36 MPI_Init (0, NULL);
37
38 MPI_Barrier(MPI_COMM_WORLD);
39 tic=MPI_Wtime();
40
41 char const * gridFilename = "GR30_lsm.nc"; // default grid file
42 parse_arguments(argc, argv, &gridFilename);
43 yac_cinit ();
45 yac_cdef_datetime("2008-03-09T16:05:07", "2008-03-10T16:05:07");
46
47 int comp_id;
48 char * comp_name = "MPIOM";
49
50 yac_cdef_comp(comp_name, &comp_id);
51
52 MPI_Comm comp_comm;
53
54 yac_cget_comp_comm(comp_id, &comp_comm);
55
56 int rank, comp_rank, comp_size;
57 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
58 MPI_Comm_rank(comp_comm,&comp_rank);
59 MPI_Comm_size(comp_comm,&comp_size);
60 MPI_Comm_free(&comp_comm);
61
62 int nbr_vertices;
63 int nbr_cells;
64 int * num_vertices_per_cell;
65 int * cell_to_vertex;
66
67 int * cell_mask;
68
69 double * x_vertices;
70 double * y_vertices;
71 double * x_cells;
72 double * y_cells;
73
74 int * cell_core_mask;
75 int * global_cell_id;
76 int * corner_core_mask;
77 int * global_corner_id;
78
79 yac_read_part_mpiom_grid_information(gridFilename, &nbr_vertices, &nbr_cells,
80 &num_vertices_per_cell, &cell_to_vertex,
81 &x_vertices, &y_vertices, &x_cells,
82 &y_cells, &global_cell_id,
83 &cell_mask,
84 &cell_core_mask, &global_corner_id,
85 &corner_core_mask, comp_rank, comp_size);
86
87 int grid_id;
88 char grid_name[256];
89 sprintf(grid_name, "%s_grid", comp_name);
90
92 grid_name, nbr_vertices, nbr_cells, num_vertices_per_cell,
93 x_vertices, y_vertices, cell_to_vertex, &grid_id);
94
99
100 int cell_point_id;
101 int corner_point_id;
102
104 grid_id, nbr_vertices, YAC_LOCATION_CORNER, x_vertices, y_vertices, &corner_point_id);
106 grid_id, nbr_cells, YAC_LOCATION_CELL, x_cells, y_cells, &cell_point_id);
107
109
110 double * cell_point_data = malloc(nbr_cells * sizeof(*cell_point_data));
111 double * corner_point_data = malloc(nbr_vertices * sizeof(*corner_point_data));
112
113 int cell_to_vertex_offset = 0;
114
115 for (int i = 0; i < nbr_cells; ++i) {
116
117 double middle_point[3] = {0, 0, 0};
118
119 for (int j = 0; j < num_vertices_per_cell[i]; ++j) {
120
121 double curr_point[3];
122
123 LLtoXYZ(x_vertices[cell_to_vertex[cell_to_vertex_offset + j]],
124 y_vertices[cell_to_vertex[cell_to_vertex_offset + j]],
125 curr_point);
126
127 middle_point[0] += curr_point[0];
128 middle_point[1] += curr_point[1];
129 middle_point[2] += curr_point[2];
130 }
131
132 double scale = 1.0 / sqrt(middle_point[0] * middle_point[0] +
133 middle_point[1] * middle_point[1] +
134 middle_point[2] * middle_point[2]);
135
136 middle_point[0] *= scale;
137 middle_point[1] *= scale;
138 middle_point[2] *= scale;
139
140 double lon, lat;
141
142 XYZtoLL(middle_point, &lon, &lat);
143
144 cell_to_vertex_offset += num_vertices_per_cell[i];
145
146 cell_point_data[i] = yac_test_func(lon, lat);
147 }
148
149 for (int i = 0; i < nbr_vertices; ++i)
150 corner_point_data[i] = yac_test_func(x_vertices[i], y_vertices[i]);
151
152 MPI_Barrier(MPI_COMM_WORLD);
153 if (rank == 0)
154 printf(
155 "toy_multi_common(%s): Time for initialisation %f\n",
156 comp_name, MPI_Wtime()-tic);
157
158 // initialize VTK file
159 char vtk_filename[32];
160 sprintf(vtk_filename, "%s_out_%d.vtk", comp_name, comp_rank);
161
162 yac_coordinate_pointer point_data =
163 malloc(nbr_vertices * sizeof(*point_data));
164 for (int i = 0; i < nbr_vertices; ++i)
165 LLtoXYZ(x_vertices[i], y_vertices[i], point_data[i]);
166
167 YAC_VTK_FILE *vtk_file = yac_vtk_open(vtk_filename, comp_name);
169 vtk_file, (double *)point_data, nbr_vertices);
171 vtk_file, (unsigned *)cell_to_vertex,
172 (unsigned*)num_vertices_per_cell, nbr_cells);
174 vtk_file, corner_core_mask, nbr_vertices, "corner_core_mask");
176 vtk_file, global_corner_id, nbr_vertices, "global_corner_id");
178 vtk_file, cell_core_mask, nbr_cells, "cell_core_mask");
180 vtk_file, global_cell_id, nbr_cells, "global_cell_id");
181
182 int ret =
184 comp_name, comp_id, grid_id, cell_point_id, corner_point_id,
185 cell_point_data, corner_point_data, vtk_file);
186
187 free(point_data);
188 free(corner_point_data);
189 free(cell_point_data);
190 free(num_vertices_per_cell);
191 free(cell_to_vertex);
192 free(cell_mask);
193 free(x_vertices);
194 free(y_vertices);
195 free(x_cells);
196 free(y_cells);
197 free(cell_core_mask);
198 free(global_cell_id);
199 free(corner_core_mask);
200 free(global_corner_id);
201
202 return ret;
203}
204
205static void parse_arguments(
206 int argc, char ** argv, char const ** gridFilename) {
207
208 int opt;
209 while ((opt = getopt(argc, argv, "g:")) != -1) {
210 YAC_ASSERT((opt == 'c') || (opt == 'g'), "invalid command argument")
211 switch (opt) {
212 default:
213 case 'g':
214 *gridFilename = optarg;
215 break;
216 }
217 }
218}
static void XYZtoLL(double const p_in[], double *lon, double *lat)
void yac_read_part_mpiom_grid_information(const char *filename, int *num_vertices, int *num_cells, int **num_vertices_per_cell, int **cell_to_vertex, double **x_vertices, double **y_vertices, double **x_cells, double **y_cells, int **global_cell_id, int **cell_mask, int **cell_core_mask, int **global_corner_id, int **corner_core_mask, int rank, int size)
int * cell_to_vertex
int * cell_mask
double yac_test_func(double lon, double lat)
int * cell_core_mask
int grid_id
int cell_point_id
int comp_id
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)
static void parse_arguments(int argc, char **argv, char const **gridFilename)
#define YAC_ASSERT(exp, msg)
static void LLtoXYZ(double lon, double lat, double p_out[])
Definition toy_scrip.c:587
void yac_vtk_write_cell_scalars_int(YAC_VTK_FILE *vtk_file, int *scalars, unsigned num_cells, char *name)
Definition vtk_output.c:250
void yac_vtk_write_point_data(YAC_VTK_FILE *vtk_file, double *point_data, unsigned num_points)
Definition vtk_output.c:69
void yac_vtk_write_point_scalars_int(YAC_VTK_FILE *vtk_file, int *scalars, unsigned num_points, char *name)
Definition vtk_output.c:278
void yac_vtk_write_cell_data(YAC_VTK_FILE *vtk_file, unsigned *cell_corners, unsigned *num_points_per_cell, unsigned num_cells)
Definition vtk_output.c:88
YAC_VTK_FILE * yac_vtk_open(const char *filename, const char *title)
Definition vtk_output.c:45
void yac_cset_global_index(int const *global_index, int location, int grid_id)
Definition yac.c:5049
void yac_cdef_datetime(const char *start_datetime, const char *end_datetime)
Definition yac.c:761
int const YAC_LOCATION_CELL
Definition yac.c:34
void yac_cinit(void)
Definition yac.c:514
int const YAC_LOCATION_CORNER
Definition yac.c:35
void yac_cget_comp_comm(int comp_id, MPI_Comm *comp_comm)
Definition yac.c:856
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:4867
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
void yac_cdef_calendar(int calendar)
Definition yac.c:769
void yac_cset_core_mask(int const *is_core, int location, int grid_id)
Definition yac.c:5065
int const YAC_PROLEPTIC_GREGORIAN
Definition yac.c:68
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
double(* yac_coordinate_pointer)[3]
Definition yac_types.h:21