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_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 */
47 sim_data sim;
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 */
65 NULL, NULL) ) {
67 "Input initialization failed\n");
68 abort();
69 return 1;
70 }
71
72 /* Disable diagnostics that are not supported */
75
76 /* Initialize diagnostics */
77 if( diag_init(&sim.diag_data, 0) ) {
79 "\nFailed to initialize diagnostics.\n"
80 "See stderr for details.\n");
81 abort();
82 return 1;
83 }
84 simulate_init(&sim);
85
86 /* QID for this run */
87 char qid[11];
89
90 /* Write bbnbi run group to HDF5 */
91 if(sim.mpi_rank == sim.mpi_root) {
93 "\nPreparing output.\n");
94 if( hdf5_interface_init_results(&sim, qid, "bbnbi") ) {
96 "\nInitializing output failed.\n"
97 "See stderr for details.\n");
98 /* Free data and terminate */
99 abort();
100 return 1;
101 }
102 strcpy(sim.qid, qid);
103 }
104
105 /* Inject markers from the injectors and trace them */
107 bbnbi_simulate(&sim, nprt, t1, t2, &p);
108
109 /* Write output */
110 if(sim.mpi_rank == sim.mpi_root) {
111 if( hdf5_interface_write_state(sim.hdf5_out, "state", nprt, p) ) {
113 "\n"
114 "Writing marker state failed.\n"
115 "See stderr for details.\n"
116 "\n");
117 }
118 free(p);
120 "\nMarker state written.\n");
121
123 }
124 print_out0(VERBOSE_MINIMAL, sim.mpi_rank, sim.mpi_root, "\nDone\n");
125
126 return 0;
127}
128
148int bbnbi_read_arguments(int argc, char** argv, sim_data* sim,
149 int* nprt, real* t1, real* t2) {
150 struct option longopts[] = {
151 {"in", required_argument, 0, 1},
152 {"out", required_argument, 0, 2},
153 {"mpi_size", required_argument, 0, 3},
154 {"mpi_rank", required_argument, 0, 4},
155 {"d", required_argument, 0, 5},
156 {"bfield", required_argument, 0, 6},
157 {"wall", required_argument, 0, 7},
158 {"plasma", required_argument, 0, 8},
159 {"nbi", required_argument, 0, 9},
160 {"n", required_argument, 0, 10},
161 {"t1", required_argument, 0, 11},
162 {"t2", required_argument, 0, 12},
163 {0, 0, 0, 0}
164 };
165
166 /* Initialize default values */
167 sim->hdf5_in[0] = '\0';
168 sim->hdf5_out[0] = '\0';
169 sim->qid_options[0] = '\0';
170 sim->qid_bfield[0] = '\0';
171 sim->qid_efield[0] = '\0';
172 sim->qid_marker[0] = '\0';
173 sim->qid_wall[0] = '\0';
174 sim->qid_plasma[0] = '\0';
175 sim->qid_neutral[0] = '\0';
176 sim->qid_boozer[0] = '\0';
177 sim->qid_mhd[0] = '\0';
178 sim->qid_asigma[0] = '\0';
179 sim->qid_nbi[0] = '\0';
180 sim->mpi_rank = 0;
181 sim->mpi_size = 0;
182 *nprt = 10000;
183 *t1 = 0.0;
184 *t2 = 0.0;
185 strcpy(sim->description, "TAG");
186
187 /* Read user input */
188 int c;
189 int slen;
190 while((c = getopt_long(argc, argv, "", longopts, NULL)) != -1) {
191 switch(c) {
192 case 1:
193 /* The .hdf5 filename can be specified with or without the
194 * trailing .h5 */
195 slen = strlen(optarg);
196 if ( slen > 3 && !strcmp(optarg+slen-3, ".h5") ) {
197 strcpy(sim->hdf5_in, optarg);
198 (sim->hdf5_in)[slen-3]='\0';
199 }
200 else {
201 strcpy(sim->hdf5_in, optarg);
202 }
203 break;
204 case 2:
205 /* The .hdf5 filename can be specified with or without the
206 * trailing .h5 */
207 slen = strlen(optarg);
208 if ( slen > 3 && !strcmp(optarg+slen-3, ".h5") ) {
209 strcpy(sim->hdf5_out, optarg);
210 (sim->hdf5_out)[slen-3]='\0';
211 }
212 else {
213 strcpy(sim->hdf5_out, optarg);
214 }
215 break;
216 case 3:
217 sim->mpi_size = atoi(optarg);
218 break;
219 case 4:
220 sim->mpi_rank = atoi(optarg);
221 break;
222 case 5:
223 strcpy(sim->description, optarg);
224 break;
225 case 6:
226 strcpy(sim->qid_bfield, optarg);
227 break;
228 case 7:
229 strcpy(sim->qid_wall, optarg);
230 break;
231 case 8:
232 strcpy(sim->qid_plasma, optarg);
233 break;
234 case 9:
235 strcpy(sim->qid_nbi, optarg);
236 break;
237 case 10:
238 *nprt = atoi(optarg);
239 break;
240 case 11:
241 *t1 = atof(optarg);
242 break;
243 case 12:
244 *t2 = atof(optarg);
245 break;
246 default:
247 // Unregonizable argument(s). Tell user how to run ascot5_main
249 "\nUnrecognized argument. The valid arguments are:\n");
251 "--in input file (default: ascot.h5)\n");
253 "--out output file (default: same as input)\n");
255 "--mpi_size number of independent processes\n");
257 "--mpi_rank rank of independent process\n");
259 "--d run description maximum of 250 characters\n");
261 "--n number of markers to generate (default: 10000)\n");
263 "--t1 time when injectors are turned on (default: 0.0 s)\n");
265 "--t2 time when injectors are turned off (default: 0.0 s)\n");
266 return 1;
267 }
268 }
269
270 /* Default value for input file is ascot.h5, and for output same as input
271 * file. Adujust hdf5_in and hdf5_out accordingly. For output file, we don't
272 * add the .h5 extension here. */
273 if(sim->hdf5_in[0] == '\0' && sim->hdf5_out[0] == '\0') {
274 /* No input, use default values for both */
275 strcpy(sim->hdf5_in, "ascot.h5");
276 strcpy(sim->hdf5_out, "ascot.h5");
277 }
278 else if(sim->hdf5_in[0] == '\0' && sim->hdf5_out[0] != '\0') {
279 /* Output file is given but the input file is not */
280 strcpy(sim->hdf5_in, "ascot.h5");
281 strcat(sim->hdf5_out, ".h5");
282 }
283 else if(sim->hdf5_in[0] != '\0' && sim->hdf5_out[0] == '\0') {
284 /* Input file is given but the output is not */
285 strcat(sim->hdf5_in, ".h5");
286 strcpy(sim->hdf5_out, sim->hdf5_in);
287 }
288 else {
289 /* Both input and output files are given */
290 strcat(sim->hdf5_in, ".h5");
291 strcat(sim->hdf5_out, ".h5");
292 }
293 return 0;
294}
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_data *sim, int nprt, real t1, real t2, particle_state **p)
Simulate NBI injection.
Definition bbnbi5.c:56
Functions to execute bbnbi externally.
int bbnbi_read_arguments(int argc, char **argv, sim_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(diag_data *data, int Nmrk)
Initializes diagnostics data.
Definition diag.c:37
Header file for diag.c.
Header file for hdf5_helpers.h.
int hdf5_interface_write_diagnostics(sim_data *sim)
Write diagnostics to HDF5 output.
int hdf5_interface_write_state(char *fn, char *state, integer n, particle_state *p)
Write marker state to HDF5 output.
int hdf5_interface_init_results(sim_data *sim, char *qid, char *run)
Initialize run group.
int hdf5_interface_read_input(sim_data *sim, int input_active, input_particle **p, int *n_markers)
Read and initialize input data.
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(sim_data *sim)
Initialize simulation data struct.
Definition simulate.c:306
Header file for simulate.c.
int diagtrcof_collect
Definition diag.h:28
int diagorb_collect
Definition diag.h:22
General representation of a marker.
Definition particle.h:40
Simulation data struct.
Definition simulate.h:57
int mpi_size
Definition simulate.h:133
char qid[256]
Definition simulate.h:128
char qid_wall[256]
Definition simulate.h:140
char qid_asigma[256]
Definition simulate.h:145
char hdf5_in[256]
Definition simulate.h:126
char qid_plasma[256]
Definition simulate.h:141
char qid_marker[256]
Definition simulate.h:139
char qid_nbi[256]
Definition simulate.h:146
int mpi_rank
Definition simulate.h:132
char qid_bfield[256]
Definition simulate.h:137
char qid_options[256]
Definition simulate.h:136
char qid_boozer[256]
Definition simulate.h:143
char description[256]
Definition simulate.h:129
char qid_mhd[256]
Definition simulate.h:144
char hdf5_out[256]
Definition simulate.h:127
char qid_efield[256]
Definition simulate.h:138
int mpi_root
Definition simulate.h:131
char qid_neutral[256]
Definition simulate.h:142
diag_data diag_data
Definition simulate.h:68
Header file for wall.c.