ASCOT5
Loading...
Searching...
No Matches
hdf5_wall.c
Go to the documentation of this file.
1
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <hdf5.h>
13#include <hdf5_hl.h>
14#include "../wall.h"
15#include "../wall/wall_2d.h"
16#include "../wall/wall_3d.h"
17#include "hdf5_wall.h"
18#include "hdf5_helpers.h"
19
20#define WPATH
22int hdf5_wall_read_2D(hid_t f, wall_2d_offload_data* offload_data,
23 real** offload_array, char* qid);
24int hdf5_wall_read_3D(hid_t f, wall_3d_offload_data* offload_data,
25 real** offload_array, char* qid);
26
42int hdf5_wall_init_offload(hid_t f, wall_offload_data* offload_data,
43 real** offload_array, int** int_offload_array,
44 char* qid) {
45
46 char path[256];
47 int err = 1;
48
49 /* Read data the QID corresponds to */
50
51 hdf5_gen_path("/wall/wall_2D_XXXXXXXXXX", qid, path);
52 if(hdf5_find_group(f, path) == 0) {
53 offload_data->type = wall_type_2D;
54 err = hdf5_wall_read_2D(f, &(offload_data->w2d),
55 offload_array, qid);
56 }
57
58 hdf5_gen_path("/wall/wall_3D_XXXXXXXXXX", qid, path);
59 if(hdf5_find_group(f, path) == 0) {
60 offload_data->type = wall_type_3D;
61 err = hdf5_wall_read_3D(f, &(offload_data->w3d),
62 offload_array, qid);
63 }
64
65 /* Initialize if data was read succesfully */
66 if(!err) {
67 err = wall_init_offload(offload_data, offload_array, int_offload_array);
68 }
69
70 return err;
71}
72
83int hdf5_wall_read_2D(hid_t f, wall_2d_offload_data* offload_data,
84 real** offload_array, char* qid) {
85 #undef WPATH
86 #define WPATH "/wall/wall_2D_XXXXXXXXXX/"
87
88 int nelements;
89
90 /* Read number of wall elements and allocate offload array */
91 if( hdf5_read_int(WPATH "nelements", &nelements,
92 f, qid, __FILE__, __LINE__) ) {return 1;}
93
94 offload_data->n = nelements;
95 offload_data->offload_array_length = 2 * offload_data->n;
96 *offload_array = (real*) malloc(2 * offload_data->n * sizeof(real));
97
98 /* Read the wall polygon */
99 if( hdf5_read_double(WPATH "r", &((*offload_array)[0]),
100 f, qid, __FILE__, __LINE__) ) {return 1;}
101 if( hdf5_read_double(WPATH "z", &((*offload_array)[nelements]),
102 f, qid, __FILE__, __LINE__) ) {return 1;}
103
104 return 0;
105}
106
117int hdf5_wall_read_3D(hid_t f, wall_3d_offload_data* offload_data,
118 real** offload_array, char* qid) {
119 #undef WPATH
120 #define WPATH "/wall/wall_3D_XXXXXXXXXX/"
121
122 int nelements;
123
124 /* Read number of wall elements and allocate offload array to
125 store n 3D triangles */
126 if( hdf5_read_int(WPATH "nelements", &nelements,
127 f, qid, __FILE__, __LINE__) ) {return 1;}
128 offload_data->n = nelements;
129
130 /* Allocate temporary arrays for x1x2x3, y1y2y3, z1z2z3 for each triangle */
131 real* x1x2x3 = (real*)malloc(3 * nelements * sizeof(real));
132 real* y1y2y3 = (real*)malloc(3 * nelements * sizeof(real));
133 real* z1z2z3 = (real*)malloc(3 * nelements * sizeof(real));
134
135 if( hdf5_read_double(WPATH "x1x2x3", x1x2x3,
136 f, qid, __FILE__, __LINE__) ) {return 1;}
137 if( hdf5_read_double(WPATH "y1y2y3", y1y2y3,
138 f, qid, __FILE__, __LINE__) ) {return 1;}
139 if( hdf5_read_double(WPATH "z1z2z3", z1z2z3,
140 f, qid, __FILE__, __LINE__) ) {return 1;}
141
142 /* The data in the offload array is to be in the format
143 * [x1 y1 z1 x2 y2 z2 x3 y3 z3; ... ]
144 */
145 *offload_array = (real*)malloc(9 * nelements * sizeof(real));
146 offload_data->offload_array_length = 9 * nelements;
147 for(int i = 0; i < nelements; i++) {
148 for(int j = 0; j < 3; j++) {
149 (*offload_array)[i*9 + j*3 + 0] = x1x2x3[3*i+j];
150 (*offload_array)[i*9 + j*3 + 1] = y1y2y3[3*i+j];
151 (*offload_array)[i*9 + j*3 + 2] = z1z2z3[3*i+j];
152 }
153 }
154 free(x1x2x3);
155 free(y1y2y3);
156 free(z1z2z3);
157 return 0;
158}
double real
Definition ascot5.h:85
herr_t hdf5_find_group(hid_t loc, const char *path)
Checks if given group exists within given hdf5 file. Negative value is returned if the group doesn't ...
int hdf5_read_double(const char *var, real *ptr, hid_t file, char *qid, const char *errfile, int errline)
Read double-valued data from ASCOT5 HDF5 file.
char * hdf5_gen_path(const char *original, char *qid, char *path)
Generate a valid path from a given template and qid.
int hdf5_read_int(const char *var, int *ptr, hid_t file, char *qid, const char *errfile, int errline)
Read int-valued data from ASCOT5 HDF5 file.
Header file for hdf5_helpers.h.
int hdf5_wall_init_offload(hid_t f, wall_offload_data *offload_data, real **offload_array, int **int_offload_array, char *qid)
Read wall data from HDF5 file.
Definition hdf5_wall.c:42
int hdf5_wall_read_3D(hid_t f, wall_3d_offload_data *offload_data, real **offload_array, char *qid)
Read 3D wall data from HDF5 file.
Definition hdf5_wall.c:117
#define WPATH
Definition hdf5_wall.c:20
int hdf5_wall_read_2D(hid_t f, wall_2d_offload_data *offload_data, real **offload_array, char *qid)
Read 2D wall data from HDF5 file.
Definition hdf5_wall.c:83
Header file for hdf5_wall.c.
2D wall offload data
Definition wall_2d.h:13
3D wall offload data
Definition wall_3d.h:16
Wall model offload data.
Definition wall.h:38
wall_3d_offload_data w3d
Definition wall.h:41
wall_type type
Definition wall.h:39
wall_2d_offload_data w2d
Definition wall.h:40
int wall_init_offload(wall_offload_data *offload_data, real **offload_array, int **int_offload_array)
Load wall data and prepare parameters.
Definition wall.c:42
Header file for wall.c.
@ wall_type_2D
Definition wall.h:25
@ wall_type_3D
Definition wall.h:26
Header file for wall_2d.c.
Header file for wall_3d.c.