ASCOT5
Loading...
Searching...
No Matches
plasma_1Dt.c
Go to the documentation of this file.
1
5#include <stdio.h>
6#include <stdlib.h>
7#include <math.h>
8#include "../ascot5.h"
9#include "../error.h"
10#include "../consts.h"
11#include "../print.h"
12#include "plasma_1Dt.h"
13
14
22int plasma_1Dt_init(plasma_1Dt_data* data, int nrho, int ntime, int nion,
23 real* rho, real* time, int* anum, int* znum, real* mass,
24 real* charge, real* Te, real* Ti, real* ne, real* ni) {
25
26 data->n_rho = nrho;
27 data->n_time = ntime;
28 data->n_species = nion + 1;
29
30 data->anum = (int*) malloc( nion*sizeof(int) );
31 data->znum = (int*) malloc( nion*sizeof(int) );
32 data->mass = (real*) malloc( (nion+1)*sizeof(real) );
33 data->charge = (real*) malloc( (nion+1)*sizeof(real) );
34 for(int i = 0; i < data->n_species; i++) {
35 if(i < nion) {
36 data->znum[i] = znum[i];
37 data->anum[i] = anum[i];
38 }
39 data->mass[i] = mass[i];
40 data->charge[i] = charge[i];
41 }
42 data->rho = (real*) malloc( nrho*sizeof(real) );
43 for(int i = 0; i < nrho; i++) {
44 data->rho[i] = rho[i];
45 }
46 data->time = (real*) malloc( ntime*sizeof(real) );
47 for(int i = 0; i < ntime; i++) {
48 data->time[i] = time[i];
49 }
50 data->temp = (real*) malloc( 2*nrho*ntime*sizeof(real) );
51 data->dens = (real*) malloc( (nion+1)*nrho*ntime*sizeof(real) );
52 for(int i = 0; i < nrho; i++) {
53 for(int j = 0; j < ntime; j++) {
54 data->temp[j*2*nrho + i] = Te[j*nrho + i];
55 data->temp[(j*2+1)*nrho + i] = Ti[j*nrho + i];
56 data->dens[j*nrho + i] = ne[j*nrho + i];
57 for(int k = 0; k < nion; k++) {
58 data->dens[(k+1)*nrho*ntime + j*nrho + i] =
59 ni[k*nrho*ntime + j*nrho + i];
60 }
61 }
62 }
63
64 print_out(VERBOSE_IO, "\n1D plasma profiles (P_1Dt)\n");
66 "Min rho = %1.2le, Max rho = %1.2le,"
67 " Number of rho grid points = %d\n",
68 data->rho[0], data->rho[data->n_rho-1], data->n_rho);
70 "Min time = %1.2le, Max time = %1.2le,"
71 " Number of time points = %d\n",
72 data->time[0], data->time[data->n_time-1], data->n_time);
73 print_out(VERBOSE_IO, "Number of ion species = %d\n", nion);
75 "Species Z/A charge [e]/mass [amu] "
76 "Density [m^-3] at Min/Max rho(t=t0)"
77 " Temperature [eV] at Min/Max rho(t=t0)\n");
78 for(int i=0; i < nion; i++) {
80 " %3d /%3d %3d /%7.3f %1.2le/%1.2le "
81 " %1.2le/%1.2le \n",
82 data->znum[i], data->anum[i],
83 (int)round(data->charge[i+1]/CONST_E),
84 data->mass[i+1]/CONST_U,
85 data->dens[nrho*ntime + i*nrho],
86 data->dens[nrho*ntime + (i+1)*nrho - 1],
87 data->temp[ntime*nrho] / CONST_E,
88 data->temp[ntime*nrho + nrho - 1] / CONST_E);
89 }
91 "[electrons] %3d /%7.3f %1.2le/%1.2le "
92 "%1.2le/%1.2le \n",
94 data->dens[0], data->dens[nrho-1],
95 data->temp[0] / CONST_E, data->temp[nrho-1] / CONST_E);
96 real quasineutrality = 0;
97 for(int k = 0; k < nrho; k++) {
98 real ele_qdens =
99 data->dens[ntime + nrho + k] * CONST_E;
100 real ion_qdens = 0;
101 for(int i=0; i < nion; i++) {
102 int idx = nrho*ntime + ntime + nrho * (2+1) + k;
103 ion_qdens += data->dens[idx] * data->charge[i+1];
104 }
105 quasineutrality = fmax( quasineutrality,
106 fabs( 1 - ion_qdens / ele_qdens ) );
107 }
108 print_out(VERBOSE_IO, "Quasi-neutrality is (electron / ion charge density)"
109 " %.2f\n", 1+quasineutrality);
110 return 0;
111}
112
119 free(data->mass);
120 free(data->charge);
121 free(data->anum);
122 free(data->znum);
123 free(data->rho);
124 free(data->time);
125 free(data->temp);
126 free(data->dens);
127}
128
135 GPU_MAP_TO_DEVICE(
136 data->mass[0:data->n_species], data->charge[0:data->n_species], \
137 data->anum[0:data->n_species-1], data->znum[0:data->n_species-1], \
138 data->rho[0:data->n_rho], data->time[0:data->n_time], \
139 data->temp[0:data->n_time*data->n_rho*data->n_species], \
140 data->dens[0:data->n_rho*data->n_species*data->n_time]
141 )
142}
143
158a5err plasma_1Dt_eval_temp(real* temp, real rho, real t, int species,
159 plasma_1Dt_data* pls_data) {
160
161 real temp_dens[MAX_SPECIES], temp_temp[MAX_SPECIES];
162
163 a5err err = plasma_1Dt_eval_densandtemp(temp_dens, temp_temp, rho, t,
164 pls_data);
165
166 *temp = temp_temp[species];
167
168 return err;
169
170}
171
186a5err plasma_1Dt_eval_dens(real* dens, real rho, real t, int species,
187 plasma_1Dt_data* pls_data) {
188 real temp_dens[MAX_SPECIES], temp_temp[MAX_SPECIES];
189
190 a5err err = plasma_1Dt_eval_densandtemp(temp_dens, temp_temp, rho, t,
191 pls_data);
192
193 *dens = temp_dens[species];
194
195 return err;
196
197}
198
214 plasma_1Dt_data* pls_data) {
215
216 a5err err = 0;
217 if(rho < pls_data->rho[0]) {
219 }
220 else if(rho >= pls_data->rho[pls_data->n_rho-1]) {
222 }
223 else {
224 int i_rho = 0;
225 while(i_rho < pls_data->n_rho-1 && pls_data->rho[i_rho] <= rho) {
226 i_rho++;
227 }
228 i_rho--;
229
230 real t_rho = (rho - pls_data->rho[i_rho])
231 / (pls_data->rho[i_rho+1] - pls_data->rho[i_rho]);
232
233 int i_time = 0;
234 while(i_time < pls_data->n_time-1 && pls_data->time[i_time] <= t) {
235 i_time++;
236 }
237 i_time--;
238
239 real t_time = (t - pls_data->time[i_time])
240 / (pls_data->time[i_time+1] - pls_data->time[i_time]);
241
242 if(i_time < 0) {
243 /* time < t[0], use first profile */
244 i_time = 0;
245 t_time = 0;
246 }
247 else if(i_time >= pls_data->n_time-2) {
248 /* time > t[n_time-1], use last profile */
249 i_time = pls_data->n_time-2;
250 t_time = 1;
251 }
252
253 for(int i = 0; i < pls_data->n_species; i++) {
254 real p11, p12, p21, p22, p1, p2;
255
256 p11 = pls_data->dens[i_time*pls_data->n_species*pls_data->n_rho
257 + i*pls_data->n_rho
258 + i_rho];
259 p12 = pls_data->dens[i_time*pls_data->n_species*pls_data->n_rho
260 + i*pls_data->n_rho
261 + i_rho + 1];
262 p21 = pls_data->dens[(i_time+1)*pls_data->n_species*pls_data->n_rho
263 + i*pls_data->n_rho
264 + i_rho];
265 p22 = pls_data->dens[(i_time+1)*pls_data->n_species*pls_data->n_rho
266 + i*pls_data->n_rho
267 + i_rho + 1];
268
269 p1 = p11 + t_rho * (p12 - p11);
270 p2 = p21 + t_rho * (p22 - p21);
271
272 dens[i] = p1 + t_time * (p2 - p1);
273
274 if(i < 2) {
275 /* Electron and ion temperature */
276 p11 = pls_data->temp[i_time*2*pls_data->n_rho
277 + i*pls_data->n_rho
278 +i_rho];
279 p12 = pls_data->temp[i_time*2*pls_data->n_rho
280 + i*pls_data->n_rho
281 + i_rho + 1];
282 p21 = pls_data->temp[(i_time+1)*2*pls_data->n_rho
283 + i*pls_data->n_rho
284 + i_rho];
285 p22 = pls_data->temp[(i_time+1)*2*pls_data->n_rho
286 + i*pls_data->n_rho
287 + i_rho + 1];
288
289 p1 = p11 + t_rho * (p12 - p11);
290 p2 = p21 + t_rho * (p22 - p21);
291
292 temp[i] = p1 + t_time * (p2 - p1);
293 }
294 else {
295 /* Temperature is same for all ion species */
296 temp[i] = temp[1];
297 }
298 }
299 }
300
301 return err;
302}
Main header file for ASCOT5.
double real
Definition ascot5.h:85
#define MAX_SPECIES
Maximum number of plasma species.
Definition ascot5.h:95
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_1D
Definition error.h:40
@ 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
Header file for math.c.
void plasma_1Dt_offload(plasma_1Dt_data *data)
Offload data to the accelerator.
Definition plasma_1Dt.c:134
void plasma_1Dt_free(plasma_1Dt_data *data)
Free allocated resources.
Definition plasma_1Dt.c:118
a5err plasma_1Dt_eval_densandtemp(real *dens, real *temp, real rho, real t, plasma_1Dt_data *pls_data)
Evaluate plasma density and temperature for all species.
Definition plasma_1Dt.c:213
int plasma_1Dt_init(plasma_1Dt_data *data, int nrho, int ntime, int nion, real *rho, real *time, int *anum, int *znum, real *mass, real *charge, real *Te, real *Ti, real *ne, real *ni)
Initialize 1Dt plasma data and check inputs.
Definition plasma_1Dt.c:22
a5err plasma_1Dt_eval_temp(real *temp, real rho, real t, int species, plasma_1Dt_data *pls_data)
Evaluate plasma temperature.
Definition plasma_1Dt.c:158
a5err plasma_1Dt_eval_dens(real *dens, real rho, real t, int species, plasma_1Dt_data *pls_data)
Evaluate plasma density.
Definition plasma_1Dt.c:186
Header file for plasma_1Dt.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 plasma parameters on the target
Definition plasma_1Dt.h:14