ASCOT5
Loading...
Searching...
No Matches
plasma_1DS.c
Go to the documentation of this file.
1
5#include <stdlib.h>
6#include <math.h>
7#include <float.h>
8#include "../ascot5.h"
9#include "../print.h"
10#include "../error.h"
11#include "../consts.h"
12#include "../spline/interp.h"
13#include "plasma_1DS.h"
14
28#define PLASMA_1DS_NONEG 1
29
31#define PLASMA_1DS_LOG 1
32
34#define PLASMA_1DS_SQRT 2
35
57 real** offload_array) {
58
59 /* Spline initialization */
60 int err = 0;
61 int n_rho = offload_data->n_rho;
62 int n_species = offload_data->n_species;
63
64 /* Allocate enough space for two temperature and n_species density arrays */
65 real* coeff_array = (real*) malloc((2 + n_species) * NSIZE_COMP1D
66 * n_rho * sizeof(real));
67
68 for(int i=0; i< ((2 + n_species)*n_rho); i++) {
69#if PLASMA_1DS_NONEG == PLASMA_1DS_LOG
70 (*offload_array)[i] = log( (*offload_array)[i]);
71#elif PLASMA_1DS_NONEG == PLASMA_1DS_SQRT
72 (*offload_array)[i] = sqrt( (*offload_array)[i] );
73#endif
74 }
75
76 /* Evaluate spline coefficients */
77
78 /* Te */
80 coeff_array + 0*n_rho*NSIZE_COMP1D,
81 *offload_array + 0*n_rho,
82 offload_data->n_rho, NATURALBC,
83 offload_data->rho_min, offload_data->rho_max);
84
85 /* Ti */
87 coeff_array + 1*n_rho*NSIZE_COMP1D,
88 *offload_array + 1*n_rho,
89 offload_data->n_rho, NATURALBC,
90 offload_data->rho_min, offload_data->rho_max);
91
92 /* Densities */
93 for(int i=0; i < n_species; i++) {
95 coeff_array + (2 +i)*n_rho*NSIZE_COMP1D,
96 *offload_array + (2 + i)*n_rho,
97 offload_data->n_rho, NATURALBC,
98 offload_data->rho_min, offload_data->rho_max);
99 }
100
101 if(err) {
102 free(coeff_array);
103 return err;
104 }
105
106 free(*offload_array);
107 *offload_array = coeff_array;
108 offload_data->offload_array_length = (2 + n_species) * NSIZE_COMP1D * n_rho;
109
110 /* Initialize the data so that we can perform sanity checks */
111 real temp0, temp1, dens0, dens1;
112 plasma_1DS_data plsdata;
113 plasma_1DS_init(&plsdata, offload_data, *offload_array);
114
115 int n_ions = n_species - 1;
116 print_out(VERBOSE_IO, "\n1D plasma profiles (P_1DS)\n");
118 "Min rho = %1.2le, Max rho = %1.2le,"
119 " Number of rho grid points = %d,"
120 " Number of ion species = %d\n",
121 offload_data->rho_min, offload_data->rho_max, n_rho, n_ions);
123 "Species Z/A charge [e]/mass [amu] Density [m^-3] at Min/Max rho"
124 " Temperature [eV] at Min/Max rho\n");
125 for(int i=0; i < n_ions; i++) {
126 plasma_1DS_eval_temp(&temp0, offload_data->rho_min, i+1, &plsdata);
127 plasma_1DS_eval_temp(&temp1, offload_data->rho_max, i+1, &plsdata);
128 plasma_1DS_eval_dens(&dens0, offload_data->rho_min, i+1, &plsdata);
129 plasma_1DS_eval_dens(&dens1, offload_data->rho_max, i+1, &plsdata);
131 " %3d /%3d %3d /%7.3f %1.2le/%1.2le "
132 " %1.2le/%1.2le \n",
133 offload_data->znum[i], offload_data->anum[i],
134 (int)round(offload_data->charge[i+1]/CONST_E),
135 offload_data->mass[i+1]/CONST_U,
136 dens0, dens1, temp0 / CONST_E, temp1 / CONST_E);
137 }
138
139 plasma_1DS_eval_temp(&temp0, offload_data->rho_min, 0, &plsdata);
140 plasma_1DS_eval_temp(&temp1, offload_data->rho_max, 0, &plsdata);
141 plasma_1DS_eval_dens(&dens0, offload_data->rho_min, 0, &plsdata);
142 plasma_1DS_eval_dens(&dens1, offload_data->rho_max, 0, &plsdata);
144 "[electrons] %3d /%7.3f %1.2le/%1.2le "
145 " %1.2le/%1.2le \n", -1, CONST_M_E/CONST_U,
146 dens0, dens1, temp0 / CONST_E, temp1 / CONST_E);
147 real quasineutrality = 0;
148 for(int k = 0; k <n_rho; k++) {
149 real rho = offload_data->rho_min
150 + k * (offload_data->rho_max - offload_data->rho_min) / (n_rho - 1);
151
152 real ele_qdens;
153 plasma_1DS_eval_dens(&ele_qdens, rho, 0, &plsdata);
154 real ion_qdens = 0;
155 for(int i=0; i < n_ions; i++) {
156 plasma_1DS_eval_dens(&dens0, rho, i+1, &plsdata);
157 ion_qdens += dens0;
158 }
159 quasineutrality = fmax( quasineutrality,
160 fabs( 1 - ion_qdens / ele_qdens ) );
161 }
162 print_out(VERBOSE_IO, "Quasi-neutrality is (electron / ion charge density)"
163 " %.2f\n", 1+quasineutrality);
164
165 return 0;
166}
167
175 real** offload_array) {
176 free(*offload_array);
177 *offload_array = NULL;
178}
179
192 plasma_1DS_offload_data* offload_data,
193 real* offload_array) {
194 plasma_data->n_species = offload_data->n_species;
195
196 for(int i = 0; i < plasma_data->n_species; i++) {
197 plasma_data->mass[i] = offload_data->mass[i];
198 plasma_data->charge[i] = offload_data->charge[i];
199 plasma_data->znum[i] = offload_data->znum[i];
200 plasma_data->anum[i] = offload_data->anum[i];
201 }
202
203 int n_rho = offload_data->n_rho;
205 &(offload_array[0*n_rho]),
206 offload_data->n_rho, NATURALBC,
207 offload_data->rho_min,
208 offload_data->rho_max);
210 &(offload_array[1*n_rho*NSIZE_COMP1D]),
211 offload_data->n_rho, NATURALBC,
212 offload_data->rho_min,
213 offload_data->rho_max);
214
215 for(int i=0; i<offload_data->n_species; i++) {
217 &(offload_array[(2+i)*n_rho*NSIZE_COMP1D]),
218 offload_data->n_rho, NATURALBC,
219 offload_data->rho_min,
220 offload_data->rho_max);
221 }
222}
223
234a5err plasma_1DS_eval_temp(real* temp, real rho, int species,
236 int interperr = 0;
237 interperr += interp1Dcomp_eval_f(temp, &plasma_data->temp[species>0], rho);
238
239 a5err err = 0;
240 if(interperr) {
242 }
243
244#if PLASMA_1DS_NONEG == PLASMA_1DS_LOG
245 *temp = exp(*temp);
246#elif PLASMA_1DS_NONEG == PLASMA_1DS_SQRT
247 *temp = (*temp) * (*temp);
248#endif
249 if(!err && *temp < 0){
251 }
252 return err;
253}
254
265a5err plasma_1DS_eval_dens(real* dens, real rho, int species,
267
268 int interperr = 0;
269 interperr += interp1Dcomp_eval_f(dens, &plasma_data->dens[species], rho);
270
271 a5err err = 0;
272 if(interperr) {
274 }
275
276#if PLASMA_1DS_NONEG == PLASMA_1DS_LOG
277 *dens = exp(*dens);
278#elif PLASMA_1DS_NONEG == PLASMA_1DS_SQRT
279 *dens = (*dens) * (*dens);
280#endif
281 if(!err && *dens < 0){
283 }
284 return err;
285}
286
302 int interperr = 0;
303
304 /* Evaluate electron temperature and density */
305 interperr += interp1Dcomp_eval_f(&temp[0], &plasma_data->temp[0], rho);
306 interperr += interp1Dcomp_eval_f(&dens[0], &plasma_data->dens[0], rho);
307
308 /* Evaluate ion temperature (same for all ions) and densities */
309 interperr += interp1Dcomp_eval_f(&temp[1], &plasma_data->temp[1], rho);
310 for(int i=1; i<plasma_data->n_species; i++) {
311 temp[i] = temp[1];
312 interperr += interp1Dcomp_eval_f(&dens[i], &plasma_data->dens[i], rho);
313 }
314
315 a5err err = 0;
316 if(interperr) {
318 }
319
320#if PLASMA_1DS_NONEG == PLASMA_1DS_LOG
321 for(int i=0; i<plasma_data->n_species; i++) {
322 dens[i] = exp(dens[i]);
323 temp[i] = exp(temp[i]);
324 }
325#elif PLASMA_1DS_NONEG == PLASMA_1DS_SQRT
326 for(int i=0; i<plasma_data->n_species; i++) {
327 dens[i] = dens[i]*dens[i];
328 temp[i] = temp[i]*temp[i];
329 }
330#endif
331 for(int i=0; i<plasma_data->n_species; i++) {
332 if(!err && (dens[i] < 0 || temp[i] < 0) ) {
333 err = error_raise( ERR_INPUT_EVALUATION, __LINE__,
335 }
336 }
337 return err;
338}
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
Error module for ASCOT5.
unsigned long int a5err
Simulation error flag.
Definition error.h:17
@ EF_PLASMA_1DS
Definition error.h:41
@ ERR_INPUT_EVALUATION
Definition error.h:63
static DECLARE_TARGET_SIMD a5err error_raise(error_type type, int line, error_file file)
Raise a new error.
Definition error.h:86
Spline interpolation library.
@ NATURALBC
Definition interp.h:37
int interp1Dcomp_init_coeff(real *c, real *f, int n_x, int bc_x, real x_min, real x_max)
Calculate cubic spline interpolation coefficients for scalar 1D data.
a5err interp1Dcomp_eval_f(real *f, interp1D_data *str, real x)
Evaluate interpolated value of 1D scalar field.
void interp1Dcomp_init_spline(interp1D_data *str, real *c, int n_x, int bc_x, real x_min, real x_max)
Initialize a cubic spline.
Header file for math.c.
void plasma_1DS_free_offload(plasma_1DS_offload_data *offload_data, real **offload_array)
Free offload array and reset parameters.
Definition plasma_1DS.c:174
a5err plasma_1DS_eval_densandtemp(real *dens, real *temp, real rho, plasma_1DS_data *plasma_data)
Evaluate plasma density and temperature for all species.
Definition plasma_1DS.c:300
a5err plasma_1DS_eval_dens(real *dens, real rho, int species, plasma_1DS_data *plasma_data)
Evaluate plasma density.
Definition plasma_1DS.c:265
int plasma_1DS_init_offload(plasma_1DS_offload_data *offload_data, real **offload_array)
Initialize 1DS plasma data and check inputs.
Definition plasma_1DS.c:56
a5err plasma_1DS_eval_temp(real *temp, real rho, int species, plasma_1DS_data *plasma_data)
Evaluate plasma temperature.
Definition plasma_1DS.c:234
void plasma_1DS_init(plasma_1DS_data *plasma_data, plasma_1DS_offload_data *offload_data, real *offload_array)
Initialize magnetic field data struct on target.
Definition plasma_1DS.c:191
Header file for plasma_1DS.c.
Macros for printing console output.
#define print_out(v,...)
Print to standard output.
Definition print.h:31
@ VERBOSE_IO
Definition print.h:20
1D spline plasma parameters on the target
Definition plasma_1DS.h:30
1D spline plasma parameters that will be offloaded to target
Definition plasma_1DS.h:14
int znum[MAX_SPECIES]
Definition plasma_1DS.h:23
real mass[MAX_SPECIES]
Definition plasma_1DS.h:20
int anum[MAX_SPECIES]
Definition plasma_1DS.h:22
real charge[MAX_SPECIES]
Definition plasma_1DS.h:21
Plasma simulation data.
Definition plasma.h:57