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_data* data, char* qid);
25int hdf5_plasma_read_1Dt(hid_t f, plasma_1Dt_data* data, char* qid);
26int hdf5_plasma_read_1DS(hid_t f, plasma_1DS_data* data, char* qid);
36int hdf5_plasma_init(hid_t f, plasma_data* data, char* qid) {
37
38 char path[256];
39 int err = 1;
40
41 /* Read data the QID corresponds to */
42 hdf5_gen_path("/plasma/plasma_1D_XXXXXXXXXX", qid, path);
43 if(hdf5_find_group(f, path) == 0) {
44 data->type = plasma_type_1D;
45 err = hdf5_plasma_read_1D(f, &data->plasma_1D, qid);
46 }
47 hdf5_gen_path("/plasma/plasma_1Dt_XXXXXXXXXX", qid, path);
48 if(hdf5_find_group(f, path) == 0) {
49 data->type = plasma_type_1Dt;
50 err = hdf5_plasma_read_1Dt(f, &data->plasma_1Dt, qid);
51 }
52 hdf5_gen_path("/plasma/plasma_1DS_XXXXXXXXXX", qid, path);
53 if(hdf5_find_group(f, path) == 0) {
54 data->type = plasma_type_1DS;
55 err = hdf5_plasma_read_1DS(f, &data->plasma_1DS, qid);
56 }
57 return err;
58}
59
69int hdf5_plasma_read_1D(hid_t f, plasma_1D_data* data, char* qid) {
70 #undef PLSPATH
71 #define PLSPATH "/plasma/plasma_1D_XXXXXXXXXX/"
72
73 int n_rho, n_ions;
74 if( hdf5_read_int(PLSPATH "nion", &n_ions,
75 f, qid, __FILE__, __LINE__) ) {return 1;}
76 if( hdf5_read_int(PLSPATH "nrho", &n_rho,
77 f, qid, __FILE__, __LINE__) ) {return 1;}
78
79 int* q = (int*) malloc( n_ions * sizeof(int) );
80 int* znum = (int*) malloc( n_ions * sizeof(int) );
81 int* anum = (int*) malloc( n_ions * sizeof(int) );
82 real* mass = (real*) malloc( ( n_ions + 1 ) * sizeof(real) );
83 real* charge = (real*) malloc( ( n_ions + 1 ) * sizeof(real) );
84 if( hdf5_read_int(PLSPATH "znum", znum,
85 f, qid, __FILE__, __LINE__) ) {return 1;}
86 if( hdf5_read_int(PLSPATH "anum", anum,
87 f, qid, __FILE__, __LINE__) ) {return 1;}
88 if( hdf5_read_int(PLSPATH "charge", q,
89 f, qid, __FILE__, __LINE__) ) {return 1;}
90 if( hdf5_read_double(PLSPATH "mass", &mass[1],
91 f, qid, __FILE__, __LINE__) ) {return 1;}
92 charge[0] = -1 * CONST_E;
93 for(int i = 0; i < n_ions; i++) {
94 charge[i+1] = q[i] * CONST_E;
95 }
96 mass[0] = CONST_M_E;
97 for(int i = 0; i < n_ions; i++) {
98 mass[i+1] *= CONST_U;
99 }
100 free(q);
101
102 real* Te = (real*) malloc( n_rho*sizeof(real) );
103 real* Ti = (real*) malloc( n_rho*sizeof(real) );
104 real* ne = (real*) malloc( n_rho*sizeof(real) );
105 real* ni = (real*) malloc( n_rho*n_ions*sizeof(real) );
106 real* rho = (real*) malloc( n_rho*sizeof(real) );
107 if( hdf5_read_double(PLSPATH "rho", rho,
108 f, qid, __FILE__, __LINE__) ) {return 1;}
109 if( hdf5_read_double(PLSPATH "etemperature", Te,
110 f, qid, __FILE__, __LINE__) ) {return 1;}
111 if( hdf5_read_double(PLSPATH "edensity", ne,
112 f, qid, __FILE__, __LINE__) ) {return 1;}
113 if( hdf5_read_double(PLSPATH "itemperature", Ti,
114 f, qid, __FILE__, __LINE__) ) {return 1;}
115 if( hdf5_read_double(PLSPATH "idensity", ni,
116 f, qid, __FILE__, __LINE__) ) {return 1;}
117
118 for(int i = 0; i < n_rho; i++) {
119 Te[i] = Te[i] * CONST_E;
120 Ti[i] = Ti[i] * CONST_E;
121 }
122
123 int err = plasma_1D_init(data, n_rho, n_ions, rho, anum, znum, mass, charge,
124 Te, Ti, ne, ni);
125 free(Te);
126 free(Ti);
127 free(ne);
128 free(ni);
129 free(znum);
130 free(anum);
131 free(mass);
132 free(charge);
133 return err;
134}
135
145int hdf5_plasma_read_1Dt(hid_t f, plasma_1Dt_data* data, char* qid) {
146 #undef PLSPATH
147 #define PLSPATH "/plasma/plasma_1Dt_XXXXXXXXXX/"
148
149 /* Read rhogrid size and number of species */
150 int n_rho, n_time, n_ions;
151 if( hdf5_read_int(PLSPATH "nion", &n_ions,
152 f, qid, __FILE__, __LINE__) ) {return 1;}
153 if( hdf5_read_int(PLSPATH "nrho", &n_rho,
154 f, qid, __FILE__, __LINE__) ) {return 1;}
155 if( hdf5_read_int(PLSPATH "ntime", &n_time,
156 f, qid, __FILE__, __LINE__) ) {return 1;}
157
158 int* q = (int*) malloc( n_ions * sizeof(int) );
159 int* znum = (int*) malloc( n_ions * sizeof(int) );
160 int* anum = (int*) malloc( n_ions * sizeof(int) );
161 real* mass = (real*) malloc( ( n_ions + 1 ) * sizeof(real) );
162 real* charge = (real*) malloc( ( n_ions + 1 ) * sizeof(real) );
163 if( hdf5_read_int(PLSPATH "znum", znum,
164 f, qid, __FILE__, __LINE__) ) {return 1;}
165 if( hdf5_read_int(PLSPATH "anum", anum,
166 f, qid, __FILE__, __LINE__) ) {return 1;}
167 if( hdf5_read_int(PLSPATH "charge", q,
168 f, qid, __FILE__, __LINE__) ) {return 1;}
169 if( hdf5_read_double(PLSPATH "mass", &mass[1],
170 f, qid, __FILE__, __LINE__) ) {return 1;}
171 charge[0] = -1 * CONST_E;
172 for(int i = 0; i < n_ions; i++) {
173 charge[i+1] = q[i] * CONST_E;
174 }
175 mass[0] = CONST_M_E;
176 for(int i = 0; i < n_ions; i++) {
177 mass[i+1] *= CONST_U;
178 }
179 free(q);
180
181 real* rho = (real*) malloc( n_rho * sizeof(real) );
182 real* time = (real*) malloc( n_time * sizeof(real) );
183 if( hdf5_read_double(PLSPATH "rho", rho,
184 f, qid, __FILE__, __LINE__) ) {return 1;}
185 if( hdf5_read_double(PLSPATH "time", time,
186 f, qid, __FILE__, __LINE__) ) {return 1;}
187
188 real* Te = (real*) malloc( n_time*n_rho*sizeof(real) );
189 real* Ti = (real*) malloc( n_time*n_rho*sizeof(real) );
190 real* ne = (real*) malloc( n_time*n_rho*sizeof(real) );
191 real* ni = (real*) malloc( n_time*n_ions*n_rho*sizeof(real) );
192 if( hdf5_read_double(PLSPATH "etemperature", Te,
193 f, qid, __FILE__, __LINE__) ) {return 1;}
194 if( hdf5_read_double(PLSPATH "itemperature", Ti,
195 f, qid, __FILE__, __LINE__) ) {return 1;}
196
197 for(int i_time = 0; i_time < n_time; i_time++) {
198 for(int i_rho = 0; i_rho < n_rho; i_rho++) {
199 Te[i_time*n_rho+i_rho] *= CONST_E;
200 Ti[i_time*n_rho+i_rho] *= CONST_E;
201 }
202 }
203
204 if( hdf5_read_double(PLSPATH "edensity", ne,
205 f, qid, __FILE__, __LINE__) ) {return 1;}
206 if( hdf5_read_double(PLSPATH "idensity", ni,
207 f, qid, __FILE__, __LINE__) ) {return 1;}
208 int err = plasma_1Dt_init(data, n_rho, n_time, n_ions, rho, time, anum,
209 znum, mass, charge, Te, Ti, ne, ni);
210 free(Te);
211 free(Ti);
212 free(ne);
213 free(ni);
214 free(znum);
215 free(anum);
216 free(mass);
217 free(charge);
218 return err;
219}
220
230int hdf5_plasma_read_1DS(hid_t f, plasma_1DS_data* data, char* qid) {
231
232 #undef PLSPATH
233 #define PLSPATH "/plasma/plasma_1DS_XXXXXXXXXX/"
234
235 /* Read rhogrid and number of species */
236 int n_rho, n_ions;
237 real rhomin, rhomax;
238 if( hdf5_read_int(PLSPATH "nion", &n_ions,
239 f, qid, __FILE__, __LINE__) ) {return 1;}
240 if( hdf5_read_int(PLSPATH "nrho", &n_rho,
241 f, qid, __FILE__, __LINE__) ) {return 1;}
242 if( hdf5_read_double(PLSPATH "rhomin", &rhomin,
243 f, qid, __FILE__, __LINE__) ) {return 1;}
244 if( hdf5_read_double(PLSPATH "rhomax", &rhomax,
245 f, qid, __FILE__, __LINE__) ) {return 1;}
246
247 int* q = (int*) malloc( n_ions * sizeof(int) );
248 int* znum = (int*) malloc( n_ions * sizeof(int) );
249 int* anum = (int*) malloc( n_ions * sizeof(int) );
250 real* mass = (real*) malloc( ( n_ions + 1 ) * sizeof(real) );
251 real* charge = (real*) malloc( ( n_ions + 1 ) * sizeof(real) );
252 if( hdf5_read_int(PLSPATH "znum", znum,
253 f, qid, __FILE__, __LINE__) ) {return 1;}
254 if( hdf5_read_int(PLSPATH "anum", anum,
255 f, qid, __FILE__, __LINE__) ) {return 1;}
256 if( hdf5_read_int(PLSPATH "charge", q,
257 f, qid, __FILE__, __LINE__) ) {return 1;}
258 if( hdf5_read_double(PLSPATH "mass", &mass[1],
259 f, qid, __FILE__, __LINE__) ) {return 1;}
260 charge[0] = -1 * CONST_E;
261 for(int i = 0; i < n_ions; i++) {
262 charge[i+1] = q[i] * CONST_E;
263 }
264 mass[0] = CONST_M_E;
265 for(int i = 0; i < n_ions; i++) {
266 mass[i+1] *= CONST_U;
267 }
268 free(q);
269
270 real* Te = (real*) malloc( n_rho*sizeof(real) );
271 real* Ti = (real*) malloc( n_rho*sizeof(real) );
272 real* ne = (real*) malloc( n_rho*sizeof(real) );
273 real* ni = (real*) malloc( n_rho*n_ions*sizeof(real) );
274 if( hdf5_read_double(PLSPATH "etemperature", Te,
275 f, qid, __FILE__, __LINE__) ) {return 1;}
276 if( hdf5_read_double(PLSPATH "edensity", ne,
277 f, qid, __FILE__, __LINE__) ) {return 1;}
278 if( hdf5_read_double(PLSPATH "itemperature", Ti,
279 f, qid, __FILE__, __LINE__) ) {return 1;}
280 if( hdf5_read_double(PLSPATH "idensity", ni,
281 f, qid, __FILE__, __LINE__) ) {return 1;}
282
283 for(int i = 0; i < n_rho; i++) {
284 Te[i] = Te[i] * CONST_E;
285 Ti[i] = Ti[i] * CONST_E;
286 }
287
288 int err = plasma_1DS_init(data, n_rho, rhomin, rhomax, n_ions, anum, znum,
289 mass, charge, Te, Ti, ne, ni);
290
291 free(Te);
292 free(Ti);
293 free(ne);
294 free(ni);
295 free(znum);
296 free(anum);
297 free(mass);
298 free(charge);
299 return err;
300}
Main header file for ASCOT5.
double real
Definition ascot5.h:85
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_data *data, char *qid)
Read 1Dt plasma data from HDF5 file.
#define PLSPATH
Definition hdf5_plasma.c:22
int hdf5_plasma_read_1D(hid_t f, plasma_1D_data *data, char *qid)
Read 1D plasma data from HDF5 file.
Definition hdf5_plasma.c:69
int hdf5_plasma_init(hid_t f, plasma_data *data, char *qid)
Read plasma data from HDF5 file.
Definition hdf5_plasma.c:36
int hdf5_plasma_read_1DS(hid_t f, plasma_1DS_data *data, char *qid)
Load plasma data from HDF5 file and prepare parameters.
Header file for hdf5_plasma.c.
Header file for plasma.c.
@ plasma_type_1D
Definition plasma.h:21
@ plasma_type_1Dt
Definition plasma.h:22
@ plasma_type_1DS
Definition plasma.h:23
int plasma_1DS_init(plasma_1DS_data *data, int nrho, real rhomin, real rhomax, int nion, int *anum, int *znum, real *mass, real *charge, real *Te, real *Ti, real *ne, real *ni)
Initialize 1DS plasma data and check inputs.
Definition plasma_1DS.c:43
Header file for plasma_1DS.c.
int plasma_1D_init(plasma_1D_data *data, int nrho, int nion, real *rho, int *anum, int *znum, real *mass, real *charge, real *Te, real *Ti, real *ne, real *ni)
Initialize 1D plasma data and check inputs.
Definition plasma_1D.c:25
Header file for plasma_1D.c.
int plasma_1Dt_init(plasma_1Dt_data *data, int nrho, int ntime, int nion, real *rho, real *time, int *anum, int *znum, real *mass, real *charge, real *Te, real *Ti, real *ne, real *ni)
Initialize 1Dt plasma data and check inputs.
Definition plasma_1Dt.c:22
Header file for plasma_1Dt.c.
1D spline plasma parameters on the target
Definition plasma_1DS.h:15
1D plasma parameters on the target
Definition plasma_1D.h:14
1D plasma parameters on the target
Definition plasma_1Dt.h:14
Plasma simulation data.
Definition plasma.h:32
plasma_1D_data plasma_1D
Definition plasma.h:34
plasma_1Dt_data plasma_1Dt
Definition plasma.h:35
plasma_1DS_data plasma_1DS
Definition plasma.h:36
plasma_type type
Definition plasma.h:33