ASCOT5
Loading...
Searching...
No Matches
plasma_1D.c
Go to the documentation of this file.
1
8#include <stdio.h>
9#include <stdlib.h>
10#include <math.h>
11#include "../ascot5.h"
12#include "../error.h"
13#include "../consts.h"
14#include "../print.h"
15#include "plasma_1D.h"
16
17
25int plasma_1D_init(plasma_1D_data* data, int nrho, int nion, real* rho,
26 int* anum, int* znum, real* mass, real* charge,
27 real* Te, real* Ti, real* ne, real* ni) {
28
29 data->n_rho = nrho;
30 data->n_species = nion + 1;
31
32 data->anum = (int*) malloc( nion*sizeof(int) );
33 data->znum = (int*) malloc( nion*sizeof(int) );
34 data->mass = (real*) malloc( (nion+1)*sizeof(real) );
35 data->charge = (real*) malloc( (nion+1)*sizeof(real) );
36 for(int i = 0; i < data->n_species; i++) {
37 if(i < nion) {
38 data->znum[i] = znum[i];
39 data->anum[i] = anum[i];
40 }
41 data->mass[i] = mass[i];
42 data->charge[i] = charge[i];
43 }
44 data->rho = (real*) malloc( nrho*sizeof(real) );
45 data->temp = (real*) malloc( 2*nrho*sizeof(real) );
46 data->dens = (real*) malloc( (nion+1)*nrho*sizeof(real) );
47 for(int i = 0; i < data->n_rho; i++) {
48 data->rho[i] = rho[i];
49 data->temp[i] = Te[i];
50 data->temp[nrho + i] = Ti[i];
51 data->dens[i] = ne[i];
52 for(int j = 0; j < nion; j++) {
53 data->dens[(j+1) * nrho + i] = ni[j*nrho + i];
54 }
55 }
56
57 print_out(VERBOSE_IO, "\n1D plasma profiles (P_1D)\n");
59 "Min rho = %1.2le, Max rho = %1.2le,"
60 " Number of rho grid points = %d,"
61 " Number of ion species = %d\n",
62 data->rho[0], data->rho[data->n_rho-1], data->n_rho, nion);
64 "Species Z/A charge [e]/mass [amu] Density [m^-3] at Min/Max rho"
65 " Temperature [eV] at Min/Max rho\n");
66 for(int i=0; i < nion; i++) {
68 " %3d /%3d %3d /%7.3f %1.2le/%1.2le "
69 " %1.2le/%1.2le \n",
70 data->znum[i], data->anum[i],
71 (int)round(data->charge[i+1]/CONST_E),
72 data->mass[i+1]/CONST_U,
73 data->dens[(i+1)*nrho], data->dens[(i+1)*nrho - 1],
74 data->temp[nrho] / CONST_E, data->temp[2*nrho-1] / CONST_E);
75 }
77 "[electrons] %3d /%7.3f %1.2le/%1.2le "
78 " %1.2le/%1.2le \n",
80 data->dens[0], data->dens[nrho - 1],
81 data->temp[0] / CONST_E, data->temp[nrho-1] / CONST_E);
82 real quasineutrality = 0;
83 for(int k = 0; k < nrho; k++) {
84 real ele_qdens = data->dens[k] * CONST_E;
85 real ion_qdens = 0;
86 for(int i = 0; i < nion; i++) {
87 ion_qdens += data->dens[(i+1)*nrho + k] * data->charge[i+1];
88 }
89 quasineutrality = fmax( quasineutrality,
90 fabs( 1 - ion_qdens / ele_qdens ) );
91 }
92 print_out(VERBOSE_IO, "Quasi-neutrality is (electron / ion charge density)"
93 " %.2f\n", 1+quasineutrality);
94 return 0;
95}
96
103 free(data->mass);
104 free(data->charge);
105 free(data->anum);
106 free(data->znum);
107 free(data->rho);
108 free(data->temp);
109 free(data->dens);
110}
111
118 GPU_MAP_TO_DEVICE(
119 data->mass[0:data->n_species], data->charge[0:data->n_species], \
120 data->anum[0:data->n_species-1], data->znum[0:data->n_species-1], \
121 data->rho[0:data->n_rho], data->temp[0:2*data->n_rho], \
122 data->dens[0:data->n_rho*data->n_species]
123 )
124}
125
139a5err plasma_1D_eval_temp(real* temp, real rho, int species,
140 plasma_1D_data* pls_data) {
141
142 a5err err = 0;
143 if(rho < pls_data->rho[0]) {
145 }
146 else if(rho >= pls_data->rho[pls_data->n_rho-1]) {
148 }
149 else {
150 int i_rho = 0;
151 while(i_rho < pls_data->n_rho - 1 && pls_data->rho[i_rho] <= rho) {
152 i_rho++;
153 }
154 i_rho--;
155 real t_rho = (rho - pls_data->rho[i_rho])
156 / (pls_data->rho[i_rho+1] - pls_data->rho[i_rho]);
157
158 real p1 = pls_data->temp[species*pls_data->n_rho + i_rho];
159 real p2 = pls_data->temp[species*pls_data->n_rho + i_rho+1];
160 temp[0] = p1 + t_rho * (p2 - p1);
161 }
162
163 return err;
164}
165
179a5err plasma_1D_eval_dens(real* dens, real rho, int species,
180 plasma_1D_data* pls_data) {
181
182 a5err err = 0;
183 if(rho < pls_data->rho[0]) {
185 }
186 else if(rho >= pls_data->rho[pls_data->n_rho-1]) {
188 }
189 else {
190 int i_rho = 0;
191 while(i_rho < pls_data->n_rho - 1 && pls_data->rho[i_rho] <= rho) {
192 i_rho++;
193 }
194 i_rho--;
195 real t_rho = (rho - pls_data->rho[i_rho])
196 / (pls_data->rho[i_rho+1] - pls_data->rho[i_rho]);
197
198 real p1 = pls_data->dens[species*pls_data->n_rho + i_rho];
199 real p2 = pls_data->dens[species*pls_data->n_rho + i_rho+1];
200 dens[0] = p1 + t_rho * (p2 - p1);
201 }
202
203 return err;
204}
205
220 plasma_1D_data* pls_data) {
221 a5err err = 0;
222 if(rho < pls_data->rho[0]) {
224 }
225 else if(rho >= pls_data->rho[pls_data->n_rho-1]) {
227 }
228 else {
229 int i_rho = 0;
230 while(i_rho < pls_data->n_rho-1 && pls_data->rho[i_rho] <= rho) {
231 i_rho++;
232 }
233 i_rho--;
234
235 real t_rho = (rho - pls_data->rho[i_rho])
236 / (pls_data->rho[i_rho+1] - pls_data->rho[i_rho]);
237
238 real p1, p2;
239 for(int i = 0; i < pls_data->n_species; i++) {
240 p1 = pls_data->dens[i*pls_data->n_rho + i_rho];
241 p2 = pls_data->dens[i*pls_data->n_rho + i_rho+1];
242 dens[i] = p1 + t_rho * (p2 - p1);
243
244 if(i < 2) {
245 /* Electron and ion temperature */
246 p1 = pls_data->temp[i*pls_data->n_rho + i_rho];
247 p2 = pls_data->temp[i*pls_data->n_rho + i_rho+1];
248 temp[i] = p1 + t_rho * (p2 - p1);
249 }
250 else {
251 /* Temperature is same for all ion species */
252 temp[i] = temp[1];
253 }
254 }
255 }
256
257 return err;
258}
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_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.
a5err plasma_1D_eval_dens(real *dens, real rho, int species, plasma_1D_data *pls_data)
Evaluate plasma density.
Definition plasma_1D.c:179
a5err plasma_1D_eval_densandtemp(real *dens, real *temp, real rho, plasma_1D_data *pls_data)
Evaluate plasma density and temperature for all species.
Definition plasma_1D.c:219
void plasma_1D_free(plasma_1D_data *data)
Free allocated resources.
Definition plasma_1D.c:102
void plasma_1D_offload(plasma_1D_data *data)
Offload data to the accelerator.
Definition plasma_1D.c:117
a5err plasma_1D_eval_temp(real *temp, real rho, int species, plasma_1D_data *pls_data)
Evaluate plasma temperature.
Definition plasma_1D.c:139
int plasma_1D_init(plasma_1D_data *data, int nrho, int nion, real *rho, int *anum, int *znum, real *mass, real *charge, real *Te, real *Ti, real *ne, real *ni)
Initialize 1D plasma data and check inputs.
Definition plasma_1D.c:25
Header file for plasma_1D.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_1D.h:14
real * charge
Definition plasma_1D.h:18