YetAnotherCoupler 3.5.2
Loading...
Searching...
No Matches
basic_grid_data.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 <stdio.h>
6#include <string.h>
7
8#include "utils_core.h"
9#include "area.h"
10#include "basic_grid_data.h"
11
13 struct yac_basic_grid_data grid, double * cell_areas) {
14
15 // determine maximum number of vertices per cell
16 int max_num_vertices_per_cell = 0;
17 for (size_t i = 0; i < grid.num_cells; ++i)
18 if (grid.num_vertices_per_cell[i] > max_num_vertices_per_cell)
19 max_num_vertices_per_cell = grid.num_vertices_per_cell[i];
20
21 struct yac_grid_cell cell =
23 xmalloc(
24 (size_t)max_num_vertices_per_cell * sizeof(*(cell.coordinates_xyz))),
25 .edge_type =
26 xmalloc(
27 (size_t)max_num_vertices_per_cell * sizeof(*(cell.edge_type))),
28 .num_corners = (size_t)max_num_vertices_per_cell,
29 .array_size = (size_t)max_num_vertices_per_cell};
30
31 // for all cells in the grid
32 for (size_t cell_idx = 0; cell_idx < grid.num_cells; ++cell_idx) {
33
34 // generate grid cell
35 size_t num_vertices = grid.num_vertices_per_cell[cell_idx];
36 size_t * cell_to_vertex =
37 grid.cell_to_vertex + grid.cell_to_vertex_offsets[cell_idx];
38 size_t * cell_to_edge =
39 grid.cell_to_edge + grid.cell_to_edge_offsets[cell_idx];
40 for (size_t i = 0; i < num_vertices; ++i) {
41
42 size_t vertex_idx = cell_to_vertex[i];
43 memcpy(
44 cell.coordinates_xyz[i], grid.vertex_coordinates[vertex_idx],
45 3 * sizeof(cell.coordinates_xyz[i][0]));
46 size_t edge_idx = cell_to_edge[i];
47 cell.edge_type[i] = grid.edge_type[edge_idx];
48 }
49 cell.num_corners = num_vertices;
50
51 cell_areas[cell_idx] = yac_huiliers_area(cell);
52 }
53
54 free(cell.edge_type);
55 free(cell.coordinates_xyz);
56}
57
59
60 free(grid.vertex_coordinates);
61 free(grid.cell_ids);
62 free(grid.vertex_ids);
63 free(grid.edge_ids);
64 free(grid.core_cell_mask);
65 free(grid.core_vertex_mask);
66 free(grid.core_edge_mask);
67 free(grid.num_vertices_per_cell);
68 free(grid.num_cells_per_vertex);
69 free(grid.cell_to_vertex);
70 free(grid.cell_to_vertex_offsets);
71 free(grid.cell_to_edge);
73 free(grid.cell_to_edge_offsets);
74 free(grid.vertex_to_cell);
75 free(grid.vertex_to_cell_offsets);
76 free(grid.edge_to_vertex);
77 free(grid.edge_type);
78}
double yac_huiliers_area(struct yac_grid_cell cell)
Area calculation on a unit sphere taken from ESMF based on L'Huilier's Theorem.
Definition area.c:396
Structs and interfaces for area calculations.
void yac_basic_grid_data_compute_cell_areas(struct yac_basic_grid_data grid, double *cell_areas)
void yac_basic_grid_data_free(struct yac_basic_grid_data grid)
#define xmalloc(size)
Definition ppm_xfuncs.h:66
yac_coordinate_pointer vertex_coordinates
size_t * vertex_to_cell_offsets
yac_size_t_2_pointer edge_to_vertex
enum yac_edge_type * edge_type
size_t * cell_to_vertex_offsets
size_t num_corners
Definition grid_cell.h:21
enum yac_edge_type * edge_type
Definition grid_cell.h:20
size_t array_size
Definition grid_cell.h:22
double(* coordinates_xyz)[3]
Definition grid_cell.h:19