ASCOT5
Loading...
Searching...
No Matches
hdf5_nbi.c
Go to the documentation of this file.
1
5#include <stdio.h>
6#include <stdlib.h>
7#include <hdf5.h>
8#include <hdf5_hl.h>
9#include "../ascot5.h"
10#include "../nbi.h"
11#include "../consts.h"
12#include "hdf5_helpers.h"
13#include "hdf5_nbi.h"
14
25int hdf5_nbi_init(hid_t f, nbi_data* data, char* qid) {
26
27 char path[256]; // Storage array required for hdf5_gen_path() calls
28
29 /* Read data the QID corresponds to */
30
31 hdf5_generate_qid_path("/nbi/nbi_XXXXXXXXXX/", qid, path);
32 if( hdf5_find_group(f, path) ) {
33 return 1;
34 }
35
37 #undef NBIPATH
38 #define NBIPATH "/nbi/nbi_XXXXXXXXXX/"
40
41 int ninj;
42 if( hdf5_read_int(NBIPATH "ninj", &ninj,
43 f, qid, __FILE__, __LINE__) ) {return 1;}
44 data->ninj = ninj;
45
46 /* Loop over injectors twice: first time we initialize offload data */
47 int nbeamlet_total = 0;
48 int* id = (int*) malloc( ninj*sizeof(int) );
49 int* anum = (int*) malloc( ninj*sizeof(int) );
50 int* znum = (int*) malloc( ninj*sizeof(int) );
51 int* nbeamlet = (int*) malloc( ninj*sizeof(int) );
52 real* mass = (real*) malloc( ninj*sizeof(real) );
53 real* power = (real*) malloc( ninj*sizeof(real) );
54 real* efrac = (real*) malloc( 3*ninj*sizeof(real) );
55 real* energy = (real*) malloc( ninj*sizeof(real) );
56 real* div_h = (real*) malloc( ninj*sizeof(real) );
57 real* div_v = (real*) malloc( ninj*sizeof(real) );
58 real* div_halo_h = (real*) malloc( ninj*sizeof(real) );
59 real* div_halo_v = (real*) malloc( ninj*sizeof(real) );
60 real* div_halo_frac = (real*) malloc( ninj*sizeof(real) );
61 for(int i = 0; i < ninj; i++) {
62 sprintf(path, NBIPATH "inj%d/%s", i+1, "ids");
63 if( hdf5_read_int(path, &id[i],
64 f, qid, __FILE__, __LINE__) ) {return 1;}
65
66 sprintf(path, NBIPATH "inj%d/%s", i+1, "nbeamlet");
67 if( hdf5_read_int(path, &nbeamlet[i],
68 f, qid, __FILE__, __LINE__) ) {return 1;}
69
70 sprintf(path, NBIPATH "inj%d/%s", i+1, "power");
71 if( hdf5_read_double(path, &power[i],
72 f, qid, __FILE__, __LINE__) ) {return 1;}
73 sprintf(path, NBIPATH "inj%d/%s", i+1, "energy");
74 if( hdf5_read_double(path, &energy[i],
75 f, qid, __FILE__, __LINE__) ) {return 1;}
76 sprintf(path, NBIPATH "inj%d/%s", i+1, "efrac");
77 if( hdf5_read_double(path, &efrac[i*3],
78 f, qid, __FILE__, __LINE__) ) {return 1;}
79
80 sprintf(path, NBIPATH "inj%d/%s", i+1, "divh");
81 if( hdf5_read_double(path, &div_h[i],
82 f, qid, __FILE__, __LINE__) ) {return 1;}
83 sprintf(path, NBIPATH "inj%d/%s", i+1, "divv");
84 if( hdf5_read_double(path, &div_v[i],
85 f, qid, __FILE__, __LINE__) ) {return 1;}
86 sprintf(path, NBIPATH "inj%d/%s", i+1, "divhalofrac");
87 if( hdf5_read_double(path, &div_halo_frac[i],
88 f, qid, __FILE__, __LINE__) ) {return 1;}
89 sprintf(path, NBIPATH "inj%d/%s", i+1, "divhaloh");
90 if( hdf5_read_double(path, &div_halo_h[i],
91 f, qid, __FILE__, __LINE__) ) {return 1;}
92 sprintf(path, NBIPATH "inj%d/%s", i+1, "divhalov");
93 if( hdf5_read_double(path, &div_halo_v[i],
94 f, qid, __FILE__, __LINE__) ) {return 1;}
95
96 sprintf(path, NBIPATH "inj%d/%s", i+1, "anum");
97 if( hdf5_read_int(path, &anum[i],
98 f, qid, __FILE__, __LINE__) ) {return 1;}
99 sprintf(path, NBIPATH "inj%d/%s", i+1, "znum");
100 if( hdf5_read_int(path, &znum[i],
101 f, qid, __FILE__, __LINE__) ) {return 1;}
102 sprintf(path, NBIPATH "inj%d/%s", i+1, "mass");
103 if( hdf5_read_double(path, &mass[i],
104 f, qid, __FILE__, __LINE__) ) {return 1;}
105
106 /* Conver to SI */
107 energy[i] *= CONST_E;
108 mass[i] *= CONST_U;
109
110 nbeamlet_total += 6 * nbeamlet[i];
111 }
112
113 /* In second loop we now initialize the offload array which we are
114 * able to allocate now */
115 real* beamlet_xyz = (real*) malloc( nbeamlet_total * sizeof(real) );
116 int idx = 0;
117 for(int i = 0; i < data->ninj; i++) {
118 int n = nbeamlet[i];
119 sprintf(path, NBIPATH "inj%d/%s", i+1, "beamletx");
120 if( hdf5_read_double(path, &beamlet_xyz[idx + 0*n],
121 f, qid, __FILE__, __LINE__) ) {return 1;}
122 sprintf(path, NBIPATH "inj%d/%s", i+1, "beamlety");
123 if( hdf5_read_double(path, &beamlet_xyz[idx + 1*n],
124 f, qid, __FILE__, __LINE__) ) {return 1;}
125 sprintf(path, NBIPATH "inj%d/%s", i+1, "beamletz");
126 if( hdf5_read_double(path, &beamlet_xyz[idx + 2*n],
127 f, qid, __FILE__, __LINE__) ) {return 1;}
128 sprintf(path, NBIPATH "inj%d/%s", i+1, "beamletdx");
129 if( hdf5_read_double(path, &beamlet_xyz[idx + 3*n],
130 f, qid, __FILE__, __LINE__) ) {return 1;}
131 sprintf(path, NBIPATH "inj%d/%s", i+1, "beamletdy");
132 if( hdf5_read_double(path, &beamlet_xyz[idx + 4*n],
133 f, qid, __FILE__, __LINE__) ) {return 1;}
134 sprintf(path, NBIPATH "inj%d/%s", i+1, "beamletdz");
135 if( hdf5_read_double(path, &beamlet_xyz[idx + 5*n],
136 f, qid, __FILE__, __LINE__) ) {return 1;}
137 idx += 6*n;
138 }
139
140 /* Initialize the data */
141 int err = nbi_init(data, ninj, id, anum, znum, mass, power, efrac, energy,
142 div_h, div_v, div_halo_v, div_halo_h, div_halo_frac,
143 nbeamlet, beamlet_xyz);
144 return err;
145}
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_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.
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.
char * hdf5_generate_qid_path(const char *original, char *qid, char *path)
Generate a valid path from a given template and qid.
Header file for hdf5_helpers.h.
int hdf5_nbi_init(hid_t f, nbi_data *data, char *qid)
Initialize NBI offload data from HDF5 file.
Definition hdf5_nbi.c:25
Header file for hdf5_nbi.c.
int nbi_init(nbi_data *data, int ninj, int *id, int *anum, int *znum, real *mass, real *power, real *efrac, real *energy, real *div_h, real *div_v, real *div_halo_v, real *div_halo_h, real *div_halo_frac, int *nbeamlet, real *beamlet_xyz)
Initialize NBI data struct on target.
Definition nbi.c:22
Header file for nbi.c.
NBI data consisting of ninj injectors.
Definition nbi.h:40
int ninj
Definition nbi.h:41