YAC 3.13.2
Yet Another Coupler
Loading...
Searching...
No Matches
toy_multi_cube.c
Go to the documentation of this file.
1// Copyright (C) 2013 DKRZ, MPI-M
2// Copyright (c) 2024 The YAC Authors
3//
4// SPDX-License-Identifier: BSD-3-Clause
5
6// #define VERBOSE
7
8#include <mpi.h>
9#include <stdlib.h>
10#include <stdio.h>
11#include <unistd.h>
12#include "yac.h"
13#include "yac_utils.h"
14
15#include "toy_multi_common.h"
16
17// redefine YAC assert macros
18#undef YAC_ASSERT
19#define STR_USAGE "Usage: %s -n cube edge length\n"
20#define YAC_ASSERT(exp, msg) \
21 { \
22 if(!((exp))) { \
23 fprintf(stderr, "ERROR: %s\n" STR_USAGE, msg, argv[0]); \
24 exit(EXIT_FAILURE); \
25 } \
26 }
27
28static void parse_arguments( int argc, char ** argv, size_t * cube_n);
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 size_t cube_n = 50; // default cube edge length
42 parse_arguments(argc, argv, &cube_n);
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 = "CUBE";
49
50 yac_cdef_comp(comp_name, &comp_id);
51
52 MPI_Comm comp_comm;
53 yac_cget_comp_comm(comp_id, &comp_comm);
54
55 int rank, comp_rank, comp_size;
56 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
57 MPI_Comm_rank(comp_comm, &comp_rank);
58 MPI_Comm_size(comp_comm, &comp_size);
59 MPI_Comm_free(&comp_comm);
60
61 unsigned nbr_vertices;
62 unsigned nbr_cells;
63 unsigned * num_vertices_per_cell;
64 unsigned * cell_to_vertex;
65 double * x_vertices;
66 double * y_vertices;
67 double * x_cells;
68 double * y_cells;
69
70 int * cell_core_mask;
71 int * corner_core_mask;
72 int * global_cell_id;
73 int * global_corner_id;
74
75 yac_generate_part_cube_grid_information(cube_n, &nbr_vertices, &nbr_cells,
76 &num_vertices_per_cell, &cell_to_vertex,
77 &x_vertices, &y_vertices, &x_cells,
78 &y_cells, &global_cell_id,
79 &cell_core_mask, &global_corner_id,
80 &corner_core_mask, comp_rank, comp_size);
81
82 int grid_id;
83 char grid_name[256];
84 sprintf(grid_name, "%s_grid", comp_name);
85
87 grid_name, nbr_vertices, nbr_cells, (int*)num_vertices_per_cell,
88 x_vertices, y_vertices, (int*)cell_to_vertex, &grid_id );
89
94
95 int cell_point_id;
96 int corner_point_id;
97
99 grid_id, nbr_cells, YAC_LOCATION_CELL, x_cells, y_cells, &cell_point_id);
101 grid_id, nbr_vertices, YAC_LOCATION_CORNER, x_vertices, y_vertices,
102 &corner_point_id);
103
104 double * cell_point_data =
105 malloc(nbr_cells * sizeof(*cell_point_data));
106 double * corner_point_data =
107 malloc(nbr_vertices * sizeof(*corner_point_data));
108
109 for (unsigned i = 0; i < nbr_cells; ++i)
110 cell_point_data[i] = yac_test_func(x_cells[i], y_cells[i]);
111
112 for (unsigned i = 0; i < nbr_vertices; ++i)
113 corner_point_data[i] = yac_test_func(x_vertices[i], y_vertices[i]);
114
115 MPI_Barrier(MPI_COMM_WORLD);
116 if (rank == 0)
117 printf(
118 "toy_multi_common(%s): Time for initialisation %f\n",
119 comp_name, MPI_Wtime()-tic);
120
121 // initialize VTK file
122 char vtk_filename[32];
123 sprintf(vtk_filename, "%s_out_%d.vtk", comp_name, comp_rank);
124
125 yac_coordinate_pointer point_data =
126 malloc(nbr_vertices * sizeof(*point_data));
127 for (unsigned i = 0; i < nbr_vertices; ++i)
128 LLtoXYZ(x_vertices[i], y_vertices[i], point_data[i]);
129
130 YAC_VTK_FILE *vtk_file = yac_vtk_open(vtk_filename, comp_name);
131 yac_vtk_write_point_data(vtk_file, (double *)point_data, nbr_vertices);
133 vtk_file, (unsigned *)cell_to_vertex,
134 (unsigned*)num_vertices_per_cell, nbr_cells);
136 vtk_file, corner_core_mask, nbr_vertices, "corner_core_mask");
138 vtk_file, global_corner_id, nbr_vertices, "global_corner_id");
140 vtk_file, cell_core_mask, nbr_cells, "cell_core_mask");
142 vtk_file, global_cell_id, nbr_cells, "global_cell_id");
143
144 int ret =
146 comp_name, comp_id, grid_id, cell_point_id, corner_point_id,
147 cell_point_data, corner_point_data, vtk_file);
148
149 free(point_data);
150 free(corner_point_data);
151 free(cell_point_data);
152 free(cell_core_mask);
153 free(corner_core_mask);
154 free(global_cell_id);
155 free(global_corner_id);
156 free(x_vertices);
157 free(y_vertices);
158 free(x_cells);
159 free(y_cells);
160 free(num_vertices_per_cell);
161 free(cell_to_vertex);
162
163 return ret;
164}
165
166static void parse_arguments(int argc, char ** argv, size_t * cube_n) {
167
168 int opt;
169 while ((opt = getopt(argc, argv, "n:")) != -1) {
170 YAC_ASSERT((opt == 'c') || (opt == 'n'), "invalid command argument")
171 switch (opt) {
172 default:
173 case 'n':
174 *cube_n = atoi(optarg);
175 YAC_ASSERT(*cube_n > 0, "invalid cube edge length");
176 break;
177 }
178 }
179}
void yac_generate_part_cube_grid_information(unsigned n, unsigned *nbr_vertices, unsigned *nbr_cells, unsigned **num_vertices_per_cell, unsigned **cell_to_vertex, double **x_vertices, double **y_vertices, double **x_cells, double **y_cells, int **global_cell_id, int **cell_core_mask, int **global_corner_id, int **corner_core_mask, int rank, int size)
int * cell_to_vertex
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, size_t *cube_n)
#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_cdef_comp(char const *comp_name, int *comp_id)
Definition yac.c:1013
double(* yac_coordinate_pointer)[3]
Definition yac_types.h:21