ASCOT5
Loading...
Searching...
No Matches
hdf5_plasma.c
Go to the documentation of this file.
1
9#include <stdio.h>
10#include <stdlib.h>
11#include <hdf5.h>
12#include <hdf5_hl.h>
13#include "../ascot5.h"
14#include "../plasma.h"
15#include "../plasma/plasma_1D.h"
18#include "../consts.h"
19#include "hdf5_helpers.h"
20#include "hdf5_plasma.h"
21
22#define PLSPATH
24int hdf5_plasma_read_1D(hid_t f, plasma_1D_offload_data* offload_data,
25 real** offload_array, char* qid);
26int hdf5_plasma_read_1Dt(hid_t f, plasma_1Dt_offload_data* offload_data,
27 real** offload_array, char* qid);
28int hdf5_plasma_read_1DS(hid_t f, plasma_1DS_offload_data* offload_data,
29 real** offload_array, char* qid);
45 real** offload_array, char* qid) {
46
47 char path[256];
48 int err = 1;
49
50 /* Read data the QID corresponds to */
51
52 hdf5_gen_path("/plasma/plasma_1D_XXXXXXXXXX", qid, path);
53 if(hdf5_find_group(f, path) == 0) {
54 offload_data->type = plasma_type_1D;
55 err = hdf5_plasma_read_1D(f, &(offload_data->plasma_1D),
56 offload_array, qid);
57 }
58
59 hdf5_gen_path("/plasma/plasma_1Dt_XXXXXXXXXX", qid, path);
60 if(hdf5_find_group(f, path) == 0) {
61 offload_data->type = plasma_type_1Dt;
62 err = hdf5_plasma_read_1Dt(f, &(offload_data->plasma_1Dt),
63 offload_array, qid);
64 }
65
66 hdf5_gen_path("/plasma/plasma_1DS_XXXXXXXXXX", qid, path);
67 if(hdf5_find_group(f, path) == 0) {
68 offload_data->type = plasma_type_1DS;
69 err = hdf5_plasma_read_1DS(f, &(offload_data->plasma_1DS),
70 offload_array, qid);
71 }
72
73 /* Initialize if data was read succesfully */
74 if(!err) {
75 err = plasma_init_offload(offload_data, offload_array);
76 }
77
78 return err;
79}
80
92 real** offload_array, char* qid) {
93 #undef PLSPATH
94 #define PLSPATH "/plasma/plasma_1D_XXXXXXXXXX/"
95
96 /* Read rhogrid size and number of species */
97 int n_rho, n_ions;
98 if( hdf5_read_int(PLSPATH "nion", &n_ions,
99 f, qid, __FILE__, __LINE__) ) {return 1;}
100 if( hdf5_read_int(PLSPATH "nrho", &n_rho,
101 f, qid, __FILE__, __LINE__) ) {return 1;}
102
103 offload_data->n_species = n_ions + 1; /* Include electrons */
104 offload_data->n_rho = n_rho;
105
106 /* Electron charge and mass */
107 offload_data->charge[0] = -1 * CONST_E;
108 offload_data->mass[0] = CONST_M_E;
109
110 /* Read ion species information */
111 int temparr[MAX_SPECIES];
112
113 if( hdf5_read_int(PLSPATH "znum", offload_data->znum,
114 f, qid, __FILE__, __LINE__) ) {return 1;}
115 if( hdf5_read_int(PLSPATH "anum", offload_data->anum,
116 f, qid, __FILE__, __LINE__) ) {return 1;}
117
118 if( hdf5_read_int(PLSPATH "charge", temparr,
119 f, qid, __FILE__, __LINE__) ) {return 1;}
120 for(int i = 0; i < n_ions; i++) {
121 offload_data->charge[i+1] = temparr[i] * CONST_E;
122 }
123
124 if( hdf5_read_double(PLSPATH "mass", &(offload_data->mass[1]),
125 f, qid, __FILE__, __LINE__) ) {return 1;}
126 for(int i = 0; i < n_ions; i++) {
127 offload_data->mass[i+1] *= CONST_U;
128 }
129
130 /* Allocate space for rhogrid, density (for each species) and
131 temperature (for electrons and ions - all ions have same temperature) */
132 offload_data->offload_array_length =
133 3*n_rho + offload_data->n_species*n_rho;
134 *offload_array = (real*) malloc(sizeof(real)
135 * offload_data->offload_array_length);
136
137 /* Pointers to beginning of different data series to make code more
138 * readable */
139 real* rho = &(*offload_array)[0];
140 real* temp_e = &(*offload_array)[n_rho];
141 real* temp_i = &(*offload_array)[n_rho*2];
142 real* dens_e = &(*offload_array)[n_rho*3];
143 real* dens_i = &(*offload_array)[n_rho*4];
144
145 /* Read rhogrid, densities, and temperatures into allocated array */
146 if( hdf5_read_double(PLSPATH "rho", rho,
147 f, qid, __FILE__, __LINE__) ) {return 1;}
148 if( hdf5_read_double(PLSPATH "etemperature", temp_e,
149 f, qid, __FILE__, __LINE__) ) {return 1;}
150 if( hdf5_read_double(PLSPATH "edensity", dens_e,
151 f, qid, __FILE__, __LINE__) ) {return 1;}
152 if( hdf5_read_double(PLSPATH "itemperature", temp_i,
153 f, qid, __FILE__, __LINE__) ) {return 1;}
154 if( hdf5_read_double(PLSPATH "idensity", dens_i,
155 f, qid, __FILE__, __LINE__) ) {return 1;}
156
157 for(int i = 0; i < n_rho; i++) {
158 temp_e[i] = temp_e[i] * CONST_E;
159 temp_i[i] = temp_i[i] * CONST_E;
160 }
161
162 return 0;
163}
164
176 real** offload_array, char* qid) {
177 #undef PLSPATH
178 #define PLSPATH "/plasma/plasma_1Dt_XXXXXXXXXX/"
179
180 /* Read rhogrid size and number of species */
181 int n_rho, n_time, n_ions, n_species;
182 if( hdf5_read_int(PLSPATH "nion", &n_ions,
183 f, qid, __FILE__, __LINE__) ) {return 1;}
184 if( hdf5_read_int(PLSPATH "nrho", &n_rho,
185 f, qid, __FILE__, __LINE__) ) {return 1;}
186 if( hdf5_read_int(PLSPATH "ntime", &n_time,
187 f, qid, __FILE__, __LINE__) ) {return 1;}
188
189 n_species = n_ions + 1; /* Include electrons */
190 offload_data->n_species = n_species;
191 offload_data->n_rho = n_rho;
192 offload_data->n_time = n_time;
193
194 /* Electron charge and mass */
195 offload_data->charge[0] = -1 * CONST_E;
196 offload_data->mass[0] = CONST_M_E;
197
198 /* Read ion species information */
199 int temparr[MAX_SPECIES];
200
201 if( hdf5_read_int(PLSPATH "znum", offload_data->znum,
202 f, qid, __FILE__, __LINE__) ) {return 1;}
203 if( hdf5_read_int(PLSPATH "anum", offload_data->anum,
204 f, qid, __FILE__, __LINE__) ) {return 1;}
205
206 if( hdf5_read_int(PLSPATH "charge", temparr,
207 f, qid, __FILE__, __LINE__) ) {return 1;}
208 for(int i = 0; i < n_ions; i++) {
209 offload_data->charge[i+1] = temparr[i] * CONST_E;
210 }
211
212 if( hdf5_read_double(PLSPATH "mass", &(offload_data->mass[1]),
213 f, qid, __FILE__, __LINE__) ) {return 1;}
214 for(int i = 0; i < n_ions; i++) {
215 offload_data->mass[i+1] *= CONST_U;
216 }
217
218 /* Allocate space for rhogrid, density (for each species) and
219 temperature (for electrons and ions - all ions have same temperature) */
220 offload_data->offload_array_length =
221 n_rho + n_time + 2*n_time*n_rho + n_time*offload_data->n_species*n_rho;
222 *offload_array = (real*) malloc(sizeof(real)
223 * offload_data->offload_array_length);
224
225 /* Pointers to beginning of different data series to make code more
226 * readable */
227 real* rho = &(*offload_array)[0];
228 real* time = &(*offload_array)[n_rho];
229 real* temp = &(*offload_array)[n_rho+n_time];
230 real* dens = &(*offload_array)[n_rho+n_time+n_time*n_rho*2];
231
232 /* Read rho and time grids */
233 if( hdf5_read_double(PLSPATH "rho", rho,
234 f, qid, __FILE__, __LINE__) ) {return 1;}
235 if( hdf5_read_double(PLSPATH "time", time,
236 f, qid, __FILE__, __LINE__) ) {return 1;}
237
238 /* read electron and ion temperature data into temporary arrays and
239 * rearrange into offload array */
240 real* temp_e_in = (real*) malloc(n_time*n_rho*sizeof(real));
241 real* temp_i_in = (real*) malloc(n_time*n_rho*sizeof(real));
242
243 if( hdf5_read_double(PLSPATH "etemperature", temp_e_in,
244 f, qid, __FILE__, __LINE__) ) {return 1;}
245 if( hdf5_read_double(PLSPATH "itemperature", temp_i_in,
246 f, qid, __FILE__, __LINE__) ) {return 1;}
247
248 for(int i_time = 0; i_time < n_time; i_time++) {
249 for(int i_rho = 0; i_rho < n_rho; i_rho++) {
250 /* electrons */
251 temp[i_time*2*n_rho+i_rho]
252 = temp_e_in[i_time*n_rho + i_rho] * CONST_E; /* convert to J */
253
254 /* ions */
255 temp[i_time*2*n_rho+n_rho+i_rho]
256 = temp_i_in[i_time*n_rho + i_rho] * CONST_E; /* convert to J */
257 }
258 }
259
260 free(temp_e_in);
261 free(temp_i_in);
262
263 /* read electron and ion densities into temporary arrays and rearrange
264 * data into offload array */
265 real* dens_e_in = (real*) malloc(n_time*n_rho*sizeof(real));
266 real* dens_i_in = (real*) malloc(n_time*n_ions*n_rho*sizeof(real));
267
268 if( hdf5_read_double(PLSPATH "edensity", dens_e_in,
269 f, qid, __FILE__, __LINE__) ) {return 1;}
270 if( hdf5_read_double(PLSPATH "idensity", dens_i_in,
271 f, qid, __FILE__, __LINE__) ) {return 1;}
272
273 for(int i_time = 0; i_time < n_time; i_time++) {
274 for(int i_species = 0; i_species < (n_ions+1); i_species++) {
275 for(int i_rho = 0; i_rho < n_rho; i_rho++) {
276 if(i_species == 0) {
277 /* electrons */
278 dens[i_time*n_species*n_rho+i_species*n_rho+i_rho]
279 = dens_e_in[i_time*n_rho + i_rho];
280 }
281 else {
282 /* ions */
283 dens[i_time*n_species*n_rho+i_species*n_rho+i_rho]
284 = dens_i_in[i_time*(n_ions*n_rho)+(i_species-1)*n_rho
285 + i_rho];
286 }
287 }
288 }
289 }
290 free(dens_e_in);
291 free(dens_i_in);
292
293 return 0;
294}
295
310 real** offload_array, char* qid) {
311
312 #undef PLSPATH
313 #define PLSPATH "/plasma/plasma_1DS_XXXXXXXXXX/"
314
315 /* Read rhogrid and number of species */
316 int n_rho, n_ions;
317 if( hdf5_read_int(PLSPATH "nion", &n_ions,
318 f, qid, __FILE__, __LINE__) ) {return 1;}
319 if( hdf5_read_int(PLSPATH "nrho", &offload_data->n_rho,
320 f, qid, __FILE__, __LINE__) ) {return 1;}
321 if( hdf5_read_double(PLSPATH "rhomin", &offload_data->rho_min,
322 f, qid, __FILE__, __LINE__) ) {return 1;}
323 if( hdf5_read_double(PLSPATH "rhomax", &offload_data->rho_max,
324 f, qid, __FILE__, __LINE__) ) {return 1;}
325
326 offload_data->n_species = n_ions + 1; /* Include electrons */
327 n_rho = offload_data->n_rho;
328
329
330 /* Electron charge and mass */
331 offload_data->charge[0] = -1 * CONST_E;
332 offload_data->mass[0] = CONST_M_E;
333
334 /* Read ion species information */
335 int temparr[MAX_SPECIES];
336
337 if( hdf5_read_int(PLSPATH "znum", offload_data->znum,
338 f, qid, __FILE__, __LINE__) ) {return 1;}
339 if( hdf5_read_int(PLSPATH "anum", offload_data->anum,
340 f, qid, __FILE__, __LINE__) ) {return 1;}
341
342 if( hdf5_read_int(PLSPATH "charge", temparr,
343 f, qid, __FILE__, __LINE__) ) {return 1;}
344 for(int i = 0; i < n_ions; i++) {
345 offload_data->charge[i+1] = temparr[i] * CONST_E;
346 }
347
348 if( hdf5_read_double(PLSPATH "mass", &(offload_data->mass[1]),
349 f, qid, __FILE__, __LINE__) ) {return 1;}
350 for(int i = 0; i < n_ions; i++) {
351 offload_data->mass[i+1] *= CONST_U;
352 }
353
354 /* Allocate space for density (for each species) and
355 temperature (for electrons and ions - all ions have same temperature) */
356 offload_data->offload_array_length =
357 2*n_rho + offload_data->n_species*n_rho;
358 *offload_array = (real*) malloc(sizeof(real)
359 * offload_data->offload_array_length);
360
361 /* Pointers to beginning of different data series to make code more
362 * readable */
363 real* temp_e = &(*offload_array)[0];
364 real* temp_i = &(*offload_array)[n_rho*1];
365 real* dens_e = &(*offload_array)[n_rho*2];
366 real* dens_i = &(*offload_array)[n_rho*3];
367
368 /* Read densities, and temperatures into allocated array */
369 if( hdf5_read_double(PLSPATH "etemperature", temp_e,
370 f, qid, __FILE__, __LINE__) ) {return 1;}
371 if( hdf5_read_double(PLSPATH "edensity", dens_e,
372 f, qid, __FILE__, __LINE__) ) {return 1;}
373 if( hdf5_read_double(PLSPATH "itemperature", temp_i,
374 f, qid, __FILE__, __LINE__) ) {return 1;}
375 if( hdf5_read_double(PLSPATH "idensity", dens_i,
376 f, qid, __FILE__, __LINE__) ) {return 1;}
377
378 for(int i = 0; i < n_rho; i++) {
379 temp_e[i] = temp_e[i] * CONST_E;
380 temp_i[i] = temp_i[i] * CONST_E;
381 }
382
383 return 0;
384}
Main header file for ASCOT5.
double real
Definition ascot5.h:85
#define MAX_SPECIES
Maximum number of plasma species.
Definition ascot5.h:95
Header file containing physical and mathematical constants.
#define CONST_U
Atomic mass unit in kilograms [kg]
Definition consts.h:29
#define CONST_M_E
Electron mass [kg]
Definition consts.h:38
#define CONST_E
Elementary charge [C]
Definition consts.h:32
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_plasma_read_1Dt(hid_t f, plasma_1Dt_offload_data *offload_data, real **offload_array, char *qid)
Read 1Dt plasma data from HDF5 file.
int hdf5_plasma_read_1D(hid_t f, plasma_1D_offload_data *offload_data, real **offload_array, char *qid)
Read 1D plasma data from HDF5 file.
Definition hdf5_plasma.c:91
int hdf5_plasma_read_1DS(hid_t f, plasma_1DS_offload_data *offload_data, real **offload_array, char *qid)
Load plasma data from HDF5 file and prepare parameters.
#define PLSPATH
Definition hdf5_plasma.c:22
int hdf5_plasma_init_offload(hid_t f, plasma_offload_data *offload_data, real **offload_array, char *qid)
Read plasma data from HDF5 file.
Definition hdf5_plasma.c:44
Header file for hdf5_plasma.c.
int plasma_init_offload(plasma_offload_data *offload_data, real **offload_array)
Load plasma data and prepare parameters.
Definition plasma.c:50
Header file for plasma.c.
@ plasma_type_1D
Definition plasma.h:25
@ plasma_type_1Dt
Definition plasma.h:26
@ plasma_type_1DS
Definition plasma.h:27
Header file for plasma_1DS.c.
Header file for plasma_1D.c.
Header file for plasma_1Dt.c.
1D spline plasma parameters that will be offloaded to target
Definition plasma_1DS.h:14
int znum[MAX_SPECIES]
Definition plasma_1DS.h:23
real mass[MAX_SPECIES]
Definition plasma_1DS.h:20
int anum[MAX_SPECIES]
Definition plasma_1DS.h:22
real charge[MAX_SPECIES]
Definition plasma_1DS.h:21
1D plasma parameters that will be offloaded to target
Definition plasma_1D.h:13
int znum[MAX_SPECIES]
Definition plasma_1D.h:20
real charge[MAX_SPECIES]
Definition plasma_1D.h:18
real mass[MAX_SPECIES]
Definition plasma_1D.h:17
int anum[MAX_SPECIES]
Definition plasma_1D.h:19
1D plasma parameters that will be offloaded to target
Definition plasma_1Dt.h:13
int znum[MAX_SPECIES]
Definition plasma_1Dt.h:21
real mass[MAX_SPECIES]
Definition plasma_1Dt.h:18
real charge[MAX_SPECIES]
Definition plasma_1Dt.h:19
int anum[MAX_SPECIES]
Definition plasma_1Dt.h:20
Plasma offload data.
Definition plasma.h:40
plasma_1DS_offload_data plasma_1DS
Definition plasma.h:44
plasma_1D_offload_data plasma_1D
Definition plasma.h:42
plasma_1Dt_offload_data plasma_1Dt
Definition plasma.h:43
plasma_type type
Definition plasma.h:41