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
27int hdf5_nbi_init_offload(hid_t f, nbi_offload_data* offload_data,
28 real** offload_array, char* qid) {
29
30 char path[256]; // Storage array required for hdf5_gen_path() calls
31
32 /* Read data the QID corresponds to */
33
34 hdf5_generate_qid_path("/nbi/nbi_XXXXXXXXXX/", qid, path);
35 if( hdf5_find_group(f, path) ) {
36 return 1;
37 }
38
40 #undef NBIPATH
41 #define NBIPATH "/nbi/nbi_XXXXXXXXXX/"
43
44 if( hdf5_read_int(NBIPATH "ninj", &(offload_data->ninj),
45 f, qid, __FILE__, __LINE__) ) {return 1;}
46
47 /* Loop over injectors twice: first time we initialize offload data */
48 offload_data->offload_array_length = 0;
49 for(int i = 0; i < offload_data->ninj; i++) {
50 sprintf(path, NBIPATH "inj%d/%s", i+1, "ids");
51 if( hdf5_read_int(path, &(offload_data->id[i]),
52 f, qid, __FILE__, __LINE__) ) {return 1;}
53
54 sprintf(path, NBIPATH "inj%d/%s", i+1, "nbeamlet");
55 if( hdf5_read_int(path, &(offload_data->n_beamlet[i]),
56 f, qid, __FILE__, __LINE__) ) {return 1;}
57
58 offload_data->offload_array_length += 6*offload_data->n_beamlet[i];
59
60 sprintf(path, NBIPATH "inj%d/%s", i+1, "power");
61 if( hdf5_read_double(path, &(offload_data->power[i]),
62 f, qid, __FILE__, __LINE__) ) {return 1;}
63 sprintf(path, NBIPATH "inj%d/%s", i+1, "energy");
64 if( hdf5_read_double(path, &(offload_data->energy[i]),
65 f, qid, __FILE__, __LINE__) ) {return 1;}
66 sprintf(path, NBIPATH "inj%d/%s", i+1, "efrac");
67 if( hdf5_read_double(path, &(offload_data->efrac[i*3]),
68 f, qid, __FILE__, __LINE__) ) {return 1;}
69
70 sprintf(path, NBIPATH "inj%d/%s", i+1, "divh");
71 if( hdf5_read_double(path, &(offload_data->div_h[i]),
72 f, qid, __FILE__, __LINE__) ) {return 1;}
73 sprintf(path, NBIPATH "inj%d/%s", i+1, "divv");
74 if( hdf5_read_double(path, &(offload_data->div_v[i]),
75 f, qid, __FILE__, __LINE__) ) {return 1;}
76 sprintf(path, NBIPATH "inj%d/%s", i+1, "divhalofrac");
77 if( hdf5_read_double(path, &(offload_data->div_halo_frac[i]),
78 f, qid, __FILE__, __LINE__) ) {return 1;}
79 sprintf(path, NBIPATH "inj%d/%s", i+1, "divhaloh");
80 if( hdf5_read_double(path, &(offload_data->div_halo_h[i]),
81 f, qid, __FILE__, __LINE__) ) {return 1;}
82 sprintf(path, NBIPATH "inj%d/%s", i+1, "divhalov");
83 if( hdf5_read_double(path, &(offload_data->div_halo_v[i]),
84 f, qid, __FILE__, __LINE__) ) {return 1;}
85
86 sprintf(path, NBIPATH "inj%d/%s", i+1, "anum");
87 if( hdf5_read_int(path, &(offload_data->anum[i]),
88 f, qid, __FILE__, __LINE__) ) {return 1;}
89 sprintf(path, NBIPATH "inj%d/%s", i+1, "znum");
90 if( hdf5_read_int(path, &(offload_data->znum[i]),
91 f, qid, __FILE__, __LINE__) ) {return 1;}
92 sprintf(path, NBIPATH "inj%d/%s", i+1, "mass");
93 if( hdf5_read_double(path, &(offload_data->mass[i]),
94 f, qid, __FILE__, __LINE__) ) {return 1;}
95
96 /* Conver to SI */
97 offload_data->energy[i] *= CONST_E;
98 offload_data->mass[i] *= CONST_U;
99 }
100
101 /* In second loop we now initialize the offload array which we are
102 * able to allocate now */
103 *offload_array = (real*)malloc(
104 offload_data->offload_array_length * sizeof(real) );
105 int idx = 0;
106 for(int i = 0; i < offload_data->ninj; i++) {
107 int nbeamlet = offload_data->n_beamlet[i];
108 sprintf(path, NBIPATH "inj%d/%s", i+1, "beamletx");
109 if( hdf5_read_double(path, &(*offload_array)[idx + 0*nbeamlet],
110 f, qid, __FILE__, __LINE__) ) {return 1;}
111 sprintf(path, NBIPATH "inj%d/%s", i+1, "beamlety");
112 if( hdf5_read_double(path, &(*offload_array)[idx + 1*nbeamlet],
113 f, qid, __FILE__, __LINE__) ) {return 1;}
114 sprintf(path, NBIPATH "inj%d/%s", i+1, "beamletz");
115 if( hdf5_read_double(path, &(*offload_array)[idx + 2*nbeamlet],
116 f, qid, __FILE__, __LINE__) ) {return 1;}
117 sprintf(path, NBIPATH "inj%d/%s", i+1, "beamletdx");
118 if( hdf5_read_double(path, &(*offload_array)[idx + 3*nbeamlet],
119 f, qid, __FILE__, __LINE__) ) {return 1;}
120 sprintf(path, NBIPATH "inj%d/%s", i+1, "beamletdy");
121 if( hdf5_read_double(path, &(*offload_array)[idx + 4*nbeamlet],
122 f, qid, __FILE__, __LINE__) ) {return 1;}
123 sprintf(path, NBIPATH "inj%d/%s", i+1, "beamletdz");
124 if( hdf5_read_double(path, &(*offload_array)[idx + 5*nbeamlet],
125 f, qid, __FILE__, __LINE__) ) {return 1;}
126 idx += 6*nbeamlet;
127 }
128
129 /* Initialize the data */
130 if( nbi_init_offload(offload_data, offload_array) ) {
131 return 1;
132 }
133 return 0;
134}
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_offload(hid_t f, nbi_offload_data *offload_data, real **offload_array, char *qid)
Initialize NBI offload data from HDF5 file.
Definition hdf5_nbi.c:27
Header file for hdf5_nbi.c.
int nbi_init_offload(nbi_offload_data *offload_data, real **offload_array)
Load NBI data and prepare parameters for offload.
Definition nbi.c:23
Header file for nbi.c.
NBI parameters consisting of a bundle of injectors.
Definition nbi.h:40
real div_v[NBI_MAX_INJ]
Definition nbi.h:48
real div_halo_v[NBI_MAX_INJ]
Definition nbi.h:51
real div_halo_h[NBI_MAX_INJ]
Definition nbi.h:50
int znum[NBI_MAX_INJ]
Definition nbi.h:53
real div_h[NBI_MAX_INJ]
Definition nbi.h:47
int n_beamlet[NBI_MAX_INJ]
Definition nbi.h:43
int offload_array_length
Definition nbi.h:55
real mass[NBI_MAX_INJ]
Definition nbi.h:54
int anum[NBI_MAX_INJ]
Definition nbi.h:52
real energy[NBI_MAX_INJ]
Definition nbi.h:45
int id[NBI_MAX_INJ]
Definition nbi.h:42
real div_halo_frac[NBI_MAX_INJ]
Definition nbi.h:49
real power[NBI_MAX_INJ]
Definition nbi.h:44
real efrac[NBI_MAX_INJ *3]
Definition nbi.h:46