Streamer fluid modeling - An overview of ARCoS  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
photo.c
Go to the documentation of this file.
1 
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <math.h>
13 
14 #include "cdr.h"
15 #include "interpol2.h"
16 #include "mapper.h"
17 #include "parameters.h"
18 #include "photo.h"
19 #include "poisson.h"
20 #include "proto.h"
21 #include "rz_array.h"
22 #include "species.h"
23 
24 void photo_copy (mapper_t *mapper, grid_t *source, grid_t *target,
25  int ir, int iz, int itheta);
26 void photo_coarsen (mapper_t *mapper, grid_t *source, grid_t *target,
27  int ir, int iz, int itheta);
28 int photo_interpol_set (mapper_t *mapper, grid_t *source,
29  interpol_t *interpol,
30  int pr, int pz, int itheta);
31 void photo_interpol (mapper_t *mapper, grid_t *source, grid_t *target,
32  interpol_t *interpol,
33  int ir, int iz, int itheta);
34 
36 
39 
41 
45 
47 void
49 {
50  /* Since it makes no sense, we use photo_extra_levels < 0 to say that
51  the conditions for the photoionization are the same as for
52  the electrostatic problem. This is needed for backwards compatibility. */
53  if (extra_photo_levels < 0)
54  {
55  pois_photo_1 = pois_electrostatic;
56  pois_photo_2 = pois_electrostatic;
57  return;
58  }
59 
60  /* Two different sets of refinement criteria for the two photoionization
61  terms. If extra_photo_levels_2 < 0, then pois_photo_2 = pois_photo_1 */
62  pois_photo_1 = xmalloc (sizeof (pois_problem_t));
63 
64  pois_photo_1->max_level = photo_max_level;
65  pois_photo_1->extra_levels = extra_photo_levels;
66  pois_photo_1->max_error = photo_max_error;
67 
68  pois_photo_1->bnd_right = photo_bnd_right;
69  pois_photo_1->bnd_top = photo_bnd_top;
70  pois_photo_1->bnd_bottom = photo_bnd_bottom;
71 
72  if (extra_photo_levels_2 < 0)
73  {
74  pois_photo_2 = pois_photo_1;
75  return;
76  }
77 
78  pois_photo_2 = xmalloc (sizeof (pois_problem_t));
79 
80  pois_photo_2->max_level = photo_max_level_2;
81  pois_photo_2->extra_levels = extra_photo_levels_2;
82  pois_photo_2->max_error = photo_max_error_2;
83 
84  pois_photo_2->bnd_right = photo_bnd_right_2;
85  pois_photo_2->bnd_top = photo_bnd_top_2;
86  pois_photo_2->bnd_bottom = photo_bnd_bottom_2;
87 }
88 
89 
91 void
92 photo_copy (mapper_t *mapper, grid_t *source, grid_t *target,
93  int ir, int iz, int itheta)
94 {
95  cdr_grid_t *cdr;
96  pois_grid_t *pois;
97 
98  cdr = (cdr_grid_t*) target;
99  pois = (pois_grid_t*) source;
100 
101  RZT (cdr->photo, ir, iz, itheta) = RZ (pois->phi, ir, iz);
102 }
103 
105 void
106 photo_coarsen (mapper_t *mapper, grid_t *source, grid_t *target,
107  int ir, int iz, int itheta)
108 {
109  cdr_grid_t *cdr;
110  pois_grid_t *pois;
111  int level_diff, z, r;
112 
113  cdr = (cdr_grid_t*) target;
114  pois = (pois_grid_t*) source;
115 
116  level_diff = pois->level - cdr->level;
117 
118  z = (iz << level_diff) + (1 << (level_diff - 1));
119  r = (ir << level_diff) + (1 << (level_diff - 1));
120 
121  if (grid_contains (source, r, z, GRID_INSIDE) &&
122  grid_contains (source, r - 1, z, GRID_INSIDE) &&
123  grid_contains (source, r, z - 1, GRID_INSIDE) &&
124  grid_contains (source, r - 1, z - 1, GRID_INSIDE)) {
125 
126  RZT (cdr->photo, ir, iz, itheta) = 0.25 *
127  (RZ (pois->phi, r, z)
128  + RZ (pois->phi, r - 1, z)
129  + RZ (pois->phi, r, z - 1)
130  + RZ (pois->phi, r - 1, z - 1));
131  }
132 
133 }
134 
136 int
137 photo_interpol_set (mapper_t *mapper, grid_t *source, interpol_t *interpol,
138  int pr, int pz, int itheta)
139 {
140  pois_grid_t *pois;
141 
142  pois = (pois_grid_t*) source;
143 
144  interpol_set_stencil (interpol,
145  r_at (pr, pois->level),
146  z_at (pz, pois->level),
147  RZ (pois->phi, pr, pz),
148  RZ (pois->phi, pr, pz + 1),
149  RZ (pois->phi, pr + 1, pz),
150  RZ (pois->phi, pr + 1, pz + 1));
151 
152  return TRUE;
153 }
154 
156 void
157 photo_interpol (mapper_t *mapper, grid_t *source, grid_t *target,
158  interpol_t *interpol, int ir, int iz, int itheta)
159 {
160  double r, z;
161  cdr_grid_t *cdr;
162 
163  cdr = (cdr_grid_t *) target;
164 
165  r = r_at (ir, cdr->level);
166  z = z_at (iz, cdr->level);
167 
168  RZT (cdr->photo, ir, iz, itheta) = interpol_apply (interpol, r, z);
169 }
170 
171 
173 void
174 photo_register (double A, double lambda)
175 {
176  photo_term_t *t;
177 
178  t = (photo_term_t *) xmalloc (sizeof (photo_term_t));
179 
180  t->A = A;
181  t->lambda = lambda;
182  t->next = photo_terms;
183 
184  photo_terms = t;
185 }
186 
188 void
190 {
191  photo_term_t *ptr_term;
192  photo_term_t *p = NULL, *p1;
193 
194  *dest = NULL;
195 
196  while (src) {
197  p1 = (photo_term_t *) xmalloc (sizeof (photo_term_t));
198  if (p) {
199  p->next = p1;
200  } else {
201  *dest = p1;
202  }
203 
204  p = p1;
205  p->A = src->A;
206  p->lambda = src->lambda;
207  src = src->next;
208  }
209 
210  if (p) p->next = NULL;
211 }
212 
213 
217 void
219 {
220  photo_term_t *t, *next;
221 
222  for (t = photo_terms; t; t = next) {
223  next = t->next;
224  free (t);
225  }
226 
227  photo_terms = NULL;
228 }
229 
231 void
232 photo_dft_r (cdr_grid_t *grid, int sign)
233 {
234  cdr_grid_t *leaf;
235 
236  debug (3, "photo_dft_r(" grid_printf_str ", %d)\n",
237  grid_printf_args(grid), sign);
238 
239  iter_childs (grid, leaf) {
240  photo_dft_r (leaf, sign);
241  }
242 
243  dft_transform (grid->photo, grid->photo, sign);
244 }
245 
251 void
253 {
254  int ir, iz, itheta;
255 
256  debug(3, "photo_copy_source (" grid_printf_str ")\n",
257  grid_printf_args(grid));
258 
259 #pragma omp parallel
260  {
261 #pragma omp for private (ir, iz)
262  iter_grid_3d_n (grid, ir, iz, itheta, 2) {
263  RZT (grid->charge, ir, iz, itheta) =
264  RZT (grid->d_dens[ions], ir, iz, itheta) / grid->ntheta;
265  }
266  }
267 }
268 
271 
272 
278 void
280 {
281  int s, ir, iz, itheta;
282  int updated[2] = {electrons, photo_ions};
283 
284  debug (3, "photo_add_term (" photo_printf_str ", " grid_printf_str ")\n",
285  photo_printf_args(term), grid_printf_args(cdr));
286 
287 #pragma omp parallel
288  {
289 #pragma omp for private (ir, iz, s)
290  iter_grid_3d_n (cdr, ir, iz, itheta, 2) {
291  for (s = 0; s < 2; s++) {
292  RZT (cdr->d_dens[updated[s]], ir, iz, itheta) +=
293  term->A * RZT (cdr->photo, ir, iz, itheta);
294  }
295  }
296  }
297 }
298 
300 void
302 {
303  cdr_grid_t *child;
304 
305  photo_add_term (term, cdr);
306 
307  iter_childs (cdr, child) {
308  photo_add_term_r (term, child);
309  }
310 }
311 
313 pois_grid_t **
315 {
316  /* Call to the generic Poisson/Helmholtz solver */
317  if (i == 1) {
318  return pois_gen_solve_a (cdr, pois_photo_2, photo_mappers, term->lambda);
319  } else {
320  return pois_gen_solve_a (cdr, pois_photo_1, photo_mappers, term->lambda);
321  }
322 }
323 
326 void
328 {
329  pois_grid_t **pois_modes;
330  photo_term_t *term;
331  // photo_term_t *ptr_term;
332 
333  photo_copy_source_r (cdr);
334 
335  if (cdr->ntheta != 1)
336  cdr_dft_charge_r (cdr, 1);
337 
338  // for (ptr_term = terms; ptr_term != NULL; ptr_term = ptr_term->next) {
339  // printf("terms: A=%g lambda=%g ptr=%d\n", ptr_term->A, ptr_term->lambda, (int)ptr_term->next);
340  // }
341 
342  int j = 0;
343  for (term = terms; term; term = term->next ) {
344  int i;
345  pois_modes = photo_calc_term (term, cdr, j);
346  j++;
347  if (cdr->ntheta != 1)
348  photo_dft_r (cdr, -1);
349 
350  photo_add_term_r (term, cdr);
351  debug (3, "photo_calc (" photo_printf_str ", " grid_printf_str ")\n",
352  photo_printf_args(term), grid_printf_args(cdr));
353 
354  /* Free the allocated memory. */
355  for (i = 0; i < max_ntheta; i++) {
356  pois_free_r (pois_modes[i]);
357  }
358  free (pois_modes);
359  }
360 }
361 
364 void
365 photo_load_file (char *fname)
366 {
367  FILE *fp;
368  double A, lambda;
369  int c;
370  int i;
371 
372  printf("\n");
373  printf ("Loading photoionization data from `%s'...\n", fname);
374  printf("\n");
375  fp = fopen (fname, "r");
376 
377  if (NULL == fp) {
378  warning ("Unable to open photoionization file `%s'\n", fname);
379  exit (-1);
380  return;
381  }
382 
383 
384  i=0;
385  do {
386  c = fscanf (fp, "%lf %lf", &A, &lambda);
387  if (c != 2) {
388  break;
389  }
390 
391  printf ("Registring photoionization term A = %.5g, lambda = %.4g\n",
392  A, lambda);
393 
394  photo_register (A, lambda);
395  i++;
396  } while (TRUE);
397  printf("\n");
398 
399  fclose (fp);
400 }