ASCOT5
Loading...
Searching...
No Matches
bbnbi5_main.c
Go to the documentation of this file.
1
5#include <getopt.h>
6#include <math.h>
7#ifdef MPI
8 #include <mpi.h>
9#endif
10#include <omp.h>
11#include <stdlib.h>
12#include <string.h>
13#include <time.h>
14#include "ascot5.h"
15#include "gitver.h"
16#include "math.h"
17#include "hdf5_interface.h"
18#include "hdf5io/hdf5_helpers.h"
19#include "print.h"
20#include "simulate.h"
21#include "particle.h"
22#include "B_field.h"
23#include "plasma.h"
24#include "neutral.h"
25#include "wall.h"
26#include "asigma.h"
27#include "nbi.h"
28#include "diag.h"
29#include "bbnbi5.h"
30
31int bbnbi_read_arguments(int argc, char** argv, sim_offload_data* sim,
32 int* nprt, real* t1, real* t2);
33
42int main(int argc, char** argv) {
43
44 /* Read and parse command line arguments */
45 int nprt; /* Number of markers to be generated in total */
46 real t1, t2; /* Markers are initialized in this time-spawn */
48 if(bbnbi_read_arguments(argc, argv, &sim, &nprt, &t1, &t2) != 0) {
49 abort();
50 return 1;
51 }
52
53 /* Init MPI (but BBNBI 5 does not yet support MPI) */
54 sim.mpi_rank = 0;
55 sim.mpi_root = 0;
56 print_out0(VERBOSE_MINIMAL, sim.mpi_rank, sim.mpi_root, "BBNBI5\n");
58 "Tag %s\nBranch %s\n\n", GIT_VERSION, GIT_BRANCH);
59
60 /* Read data needed for bbnbi simulation */
61 real* nbi_offload_array;
62 real* B_offload_array;
63 real* plasma_offload_array;
64 real* neutral_offload_array;
65 real* wall_offload_array;
66 int* wall_int_offload_array;
67 real* asigma_offload_array;
68 real* diag_offload_array;
73 &B_offload_array, NULL, &plasma_offload_array,
74 &neutral_offload_array, &wall_offload_array,
75 &wall_int_offload_array, NULL, NULL,
76 &asigma_offload_array, &nbi_offload_array,
77 NULL, NULL) ) {
79 "Input initialization failed\n");
80 abort();
81 return 1;
82 }
83
84 /* Disable diagnostics that are not supported */
87
88 /* Initialize diagnostics */
89 if( diag_init_offload(&sim.diag_offload_data, &diag_offload_array, 0) ) {
91 "\nFailed to initialize diagnostics.\n"
92 "See stderr for details.\n");
93 abort();
94 return 1;
95 }
96 real diag_offload_array_size = sim.diag_offload_data.offload_array_length
97 * sizeof(real) / (1024.0*1024.0);
99 "Initialized diagnostics, %.1f MB.\n", diag_offload_array_size);
101
102 /* QID for this run */
103 char qid[11];
105
106 /* Write bbnbi run group to HDF5 */
107 if(sim.mpi_rank == sim.mpi_root) {
109 "\nPreparing output.\n");
110 if( hdf5_interface_init_results(&sim, qid, "bbnbi") ) {
112 "\nInitializing output failed.\n"
113 "See stderr for details.\n");
114 /* Free offload data and terminate */
115 abort();
116 return 1;
117 }
118 strcpy(sim.qid, qid);
119 }
120
121 /* Inject markers from the injectors and trace them */
124 &sim, nprt, t1, t2, B_offload_array, plasma_offload_array,
125 neutral_offload_array, wall_offload_array, wall_int_offload_array,
126 asigma_offload_array, nbi_offload_array, &p, diag_offload_array);
127
128 /* Write output */
129 if(sim.mpi_rank == sim.mpi_root) {
130 if( hdf5_interface_write_state(sim.hdf5_out, "state", nprt, p) ) {
132 "\n"
133 "Writing marker state failed.\n"
134 "See stderr for details.\n"
135 "\n");
136 }
137 free(p);
139 "\nMarker state written.\n");
140
142 &sim, diag_offload_array, sim.hdf5_out);
143 }
144 print_out0(VERBOSE_MINIMAL, sim.mpi_rank, sim.mpi_root, "\nDone\n");
145
146 return 0;
147}
148
168int bbnbi_read_arguments(int argc, char** argv, sim_offload_data* sim,
169 int* nprt, real* t1, real* t2) {
170 struct option longopts[] = {
171 {"in", required_argument, 0, 1},
172 {"out", required_argument, 0, 2},
173 {"mpi_size", required_argument, 0, 3},
174 {"mpi_rank", required_argument, 0, 4},
175 {"d", required_argument, 0, 5},
176 {"bfield", required_argument, 0, 6},
177 {"wall", required_argument, 0, 7},
178 {"plasma", required_argument, 0, 8},
179 {"nbi", required_argument, 0, 9},
180 {"n", required_argument, 0, 10},
181 {"t1", required_argument, 0, 11},
182 {"t2", required_argument, 0, 12},
183 {0, 0, 0, 0}
184 };
185
186 /* Initialize default values */
187 sim->hdf5_in[0] = '\0';
188 sim->hdf5_out[0] = '\0';
189 sim->qid_options[0] = '\0';
190 sim->qid_bfield[0] = '\0';
191 sim->qid_efield[0] = '\0';
192 sim->qid_marker[0] = '\0';
193 sim->qid_wall[0] = '\0';
194 sim->qid_plasma[0] = '\0';
195 sim->qid_neutral[0] = '\0';
196 sim->qid_boozer[0] = '\0';
197 sim->qid_mhd[0] = '\0';
198 sim->qid_asigma[0] = '\0';
199 sim->qid_nbi[0] = '\0';
200 sim->mpi_rank = 0;
201 sim->mpi_size = 0;
202 *nprt = 10000;
203 *t1 = 0.0;
204 *t2 = 0.0;
205 strcpy(sim->description, "TAG");
206
207 /* Read user input */
208 int c;
209 int slen;
210 while((c = getopt_long(argc, argv, "", longopts, NULL)) != -1) {
211 switch(c) {
212 case 1:
213 /* The .hdf5 filename can be specified with or without the
214 * trailing .h5 */
215 slen = strlen(optarg);
216 if ( slen > 3 && !strcmp(optarg+slen-3, ".h5") ) {
217 strcpy(sim->hdf5_in, optarg);
218 (sim->hdf5_in)[slen-3]='\0';
219 }
220 else {
221 strcpy(sim->hdf5_in, optarg);
222 }
223 break;
224 case 2:
225 /* The .hdf5 filename can be specified with or without the
226 * trailing .h5 */
227 slen = strlen(optarg);
228 if ( slen > 3 && !strcmp(optarg+slen-3, ".h5") ) {
229 strcpy(sim->hdf5_out, optarg);
230 (sim->hdf5_out)[slen-3]='\0';
231 }
232 else {
233 strcpy(sim->hdf5_out, optarg);
234 }
235 break;
236 case 3:
237 sim->mpi_size = atoi(optarg);
238 break;
239 case 4:
240 sim->mpi_rank = atoi(optarg);
241 break;
242 case 5:
243 strcpy(sim->description, optarg);
244 break;
245 case 6:
246 strcpy(sim->qid_bfield, optarg);
247 break;
248 case 7:
249 strcpy(sim->qid_wall, optarg);
250 break;
251 case 8:
252 strcpy(sim->qid_plasma, optarg);
253 break;
254 case 9:
255 strcpy(sim->qid_nbi, optarg);
256 break;
257 case 10:
258 *nprt = atoi(optarg);
259 break;
260 case 11:
261 *t1 = atof(optarg);
262 break;
263 case 12:
264 *t2 = atof(optarg);
265 break;
266 default:
267 // Unregonizable argument(s). Tell user how to run ascot5_main
269 "\nUnrecognized argument. The valid arguments are:\n");
271 "--in input file (default: ascot.h5)\n");
273 "--out output file (default: same as input)\n");
275 "--mpi_size number of independent processes\n");
277 "--mpi_rank rank of independent process\n");
279 "--d run description maximum of 250 characters\n");
281 "--n number of markers to generate (default: 10000)\n");
283 "--t1 time when injectors are turned on (default: 0.0 s)\n");
285 "--t2 time when injectors are turned off (default: 0.0 s)\n");
286 return 1;
287 }
288 }
289
290 /* Default value for input file is ascot.h5, and for output same as input
291 * file. Adujust hdf5_in and hdf5_out accordingly. For output file, we don't
292 * add the .h5 extension here. */
293 if(sim->hdf5_in[0] == '\0' && sim->hdf5_out[0] == '\0') {
294 /* No input, use default values for both */
295 strcpy(sim->hdf5_in, "ascot.h5");
296 strcpy(sim->hdf5_out, "ascot.h5");
297 }
298 else if(sim->hdf5_in[0] == '\0' && sim->hdf5_out[0] != '\0') {
299 /* Output file is given but the input file is not */
300 strcpy(sim->hdf5_in, "ascot.h5");
301 strcat(sim->hdf5_out, ".h5");
302 }
303 else if(sim->hdf5_in[0] != '\0' && sim->hdf5_out[0] == '\0') {
304 /* Input file is given but the output is not */
305 strcat(sim->hdf5_in, ".h5");
306 strcpy(sim->hdf5_out, sim->hdf5_in);
307 }
308 else {
309 /* Both input and output files are given */
310 strcat(sim->hdf5_in, ".h5");
311 strcat(sim->hdf5_out, ".h5");
312 }
313 return 0;
314}
Header file for B_field.c.
Main header file for ASCOT5.
double real
Definition ascot5.h:85
Header file for asigma.c.
void bbnbi_simulate(sim_offload_data *sim, int nprt, real t1, real t2, real *B_offload_array, real *plasma_offload_array, real *neutral_offload_array, real *wall_offload_array, int *wall_int_offload_array, real *asigma_offload_array, real *nbi_offload_array, particle_state **p, real *diag_offload_array)
Simulate NBI injection.
Definition bbnbi5.c:64
Functions to execute bbnbi externally.
int bbnbi_read_arguments(int argc, char **argv, sim_offload_data *sim, int *nprt, real *t1, real *t2)
Read command line arguments.
int main(int argc, char **argv)
Main function for BBNBI5.
Definition bbnbi5_main.c:42
int diag_init_offload(diag_offload_data *data, real **offload_array, int Nmrk)
Initializes offload array from offload data.
Definition diag.c:40
Header file for diag.c.
Header file for hdf5_helpers.h.
int hdf5_interface_init_results(sim_offload_data *sim, char *qid, char *run)
Initialize run group.
int hdf5_interface_read_input(sim_offload_data *sim, int input_active, real **B_offload_array, real **E_offload_array, real **plasma_offload_array, real **neutral_offload_array, real **wall_offload_array, int **wall_int_offload_array, real **boozer_offload_array, real **mhd_offload_array, real **asigma_offload_array, real **nbi_offload_array, input_particle **p, int *n_markers)
Read and initialize input data.
int hdf5_interface_write_state(char *fn, char *state, integer n, particle_state *p)
Write marker state to HDF5 output.
int hdf5_interface_write_diagnostics(sim_offload_data *sim, real *diag_offload_array, char *out)
Write diagnostics to HDF5 output.
void hdf5_generate_qid(char *qid)
Generate an identification number for a run.
Header file for hdf5_interface.c.
@ hdf5_input_plasma
@ hdf5_input_options
@ hdf5_input_wall
@ hdf5_input_neutral
@ hdf5_input_bfield
@ hdf5_input_nbi
@ hdf5_input_asigma
Header file for math.c.
Header file for nbi.c.
Header file for neutral.c.
Header file for particle.c.
Header file for plasma.c.
Macros for printing console output.
#define print_out(v,...)
Print to standard output.
Definition print.h:31
@ VERBOSE_NORMAL
Definition print.h:18
@ VERBOSE_IO
Definition print.h:20
@ VERBOSE_MINIMAL
Definition print.h:19
#define print_out0(v, rank, root,...)
Print to standard output only for root process.
Definition print.h:36
void simulate_init_offload(sim_offload_data *sim)
Initializes simulation settings.
Definition simulate.c:349
Header file for simulate.c.
int diagtrcof_collect
Definition diag.h:28
int diagorb_collect
Definition diag.h:22
size_t offload_array_length
Definition diag.h:47
General representation of a marker.
Definition particle.h:40
Simulation offload struct.
Definition simulate.h:55
char qid_wall[256]
Definition simulate.h:134
char qid_options[256]
Definition simulate.h:130
char qid_boozer[256]
Definition simulate.h:137
char hdf5_in[256]
Definition simulate.h:120
char qid[256]
Definition simulate.h:122
char qid_nbi[256]
Definition simulate.h:140
char qid_marker[256]
Definition simulate.h:133
char qid_mhd[256]
Definition simulate.h:138
char qid_neutral[256]
Definition simulate.h:136
char qid_bfield[256]
Definition simulate.h:131
char qid_plasma[256]
Definition simulate.h:135
char qid_efield[256]
Definition simulate.h:132
char qid_asigma[256]
Definition simulate.h:139
char hdf5_out[256]
Definition simulate.h:121
char description[256]
Definition simulate.h:123
diag_offload_data diag_offload_data
Definition simulate.h:66
Header file for wall.c.