Streamer fluid modeling - An overview of ARCoS
1.0
|
Routines for handling the CDR (convection-diffusion-reaction) equations. More...
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <sys/time.h>
#include "cdr.h"
#include "cstream.h"
#include "grid.h"
#include "interpol2.h"
#include "mapper.h"
#include "parameters.h"
#include "poisson.h"
#include "proto.h"
#include "react_table.h"
#include "rz_array.h"
#include "species.h"
#include "tree.h"
Go to the source code of this file.
Macros | |
#define | pij(p_, stride_) (((*(p_)) - *((p_) - (stride_))) / (*((p_) + (stride_)) - *(p_))) |
This thing looks really ugly, but what does it mean? For example, for stride = dens[electrons]->strides[R_INDX], it is the ![]() ![]() | |
Functions | |
decl_mapper_funcs (dens) | |
static void | set_all_bnd (cdr_grid_t *grid, int sign, int ir, int iz, int itheta, int dim0, int inout, int dim1, int dim1_from, int dim1_to, int dim2, int dim2_from, int dim2_to) |
static void | set_axis_bnd (cdr_grid_t *grid) |
Sets the proper boundary conditions for a grid that has its left boundary at r = 0 (e.g. the root grid). | |
static void | max_update (double *x, double cmp) |
static double | f_ad (rz_array_t *dens, double efield, double dc, double mc, int ir, int iz, int itheta, int dim) |
This is the F^a + F^d in the paper (times ![]() ![]() | |
static double | psi (double theta) |
static void | prepare_grid (cdr_grid_t *grid) |
Prepares a grid family : a) Set the boundaries for all subgrids, b) Restrict the coarser grids from the fine ones. . | |
static double | curv_at (cdr_grid_t *grid, rz_array_t *ar, int ir, int iz, int itheta, double(*f)(double)) |
Determines the curvature of ar, which has to be a component of grid at ir, iz. | |
static int | needs_refinement (cdr_grid_t *grid, int ir, int iz, int itheta, int *in_edge) |
Does the point in grid at ir, iz, itheta require to be further refined? | |
static int | any_needs_refinement (cdr_grid_t *grid, int ir, int iz, int *in_edge) |
Calls the needs_refinement at all ![]() | |
static int | brick_needs_refinement (cdr_grid_t *grid, int r0, int z0, int r1, int z1, int *in_edge) |
Determines whether a brick needs refinement. | |
static void | refine_in (cdr_grid_t *grid, int cr0, int cz0, int cr1, int cz1, int contains_edge) |
Adds a child to a cdr grid. | |
static void | restrict_from (cdr_grid_t *parent, cdr_grid_t *child) |
Restricts the values of the densities from the child grid into the parent. | |
static int | match_grids (cdr_grid_t *fro, cdr_grid_t *to) |
Matches the boundaries of grid to reading from grid fro. | |
double | gauss2_xyz (double x, double y, double z) |
void | aux_dump_frames_r (cdr_grid_t *grid, FILE *fp) |
aux_dump_frames_r QQQQ | |
void | cdr_init (void) |
Some initialization is needed for this module which has to be inside this function. | |
void | cdr_end (void) |
And the cleaning up has to be here. | |
cdr_grid_t * | cdr_new_3d_a (int r0, int z0, int r1, int z1, int ntheta) |
Creates a new 3D cdr grid. | |
cdr_grid_t * | cdr_guest (cdr_grid_t *host, int r0, int z0, int r1, int z1) |
Creates a guest cdr grid: | |
cdr_grid_t * | cdr_like_a (cdr_grid_t *grid) |
Returns a grid with the same dimensions as grid and at the same level. | |
cdr_grid_t * | cdr_clone_a (cdr_grid_t *grid) |
Returns a grid exactly like te one received, with the densities copied. | |
void | cdr_set_periodic (cdr_grid_t *grid) |
Impose periodic boundary conditions in ![]() | |
mk_recursive (cdr_set_periodic, cdr_grid_t) | |
Recursive version of cdr_set_periodic. | |
void | cdr_free_r (cdr_grid_t *grid) |
Recursive version of cdr_free. | |
void | cdr_calc_charge (cdr_grid_t *grid) |
Calculates the charge in a CDR grid. | |
mk_recursive (cdr_calc_charge, cdr_grid_t) | |
Recursive version of cdr_calc_charge. | |
void | cdr_dft_field_r (cdr_grid_t *grid, int sign) |
Fourier-transforms the electric field in grid and all its descendants. | |
cdr_grid_t * | cdr_create_coarser_a (cdr_grid_t *grid) |
Creates a grid one level coarser than GRID that covers the same area or somewhat more. | |
cdr_grid_t * | cdr_add_coarser_grids_a (cdr_grid_t *prev_root, int n) |
Takes a tree of input nodes and adds n coarser grids, returning a pointer to the root leaf. | |
void | cdr_free_coarser_grids (cdr_grid_t *prev_root, int n) |
Frees the memory allocated by cdr_add_coarser_grids_a. | |
pois_grid_t ** | cdr_calc_field_r (cdr_grid_t *cdr, int return_pois) |
The interface between the cdr and the poisson parts. | |
void | cdr_add_ext_field (cdr_grid_t *grid) |
Adds an external electric field to a cdr grid. | |
mk_recursive (cdr_add_ext_field, cdr_grid_t) | |
Recursive version of cdr_add_ext_field. | |
void | cdr_calc_eabs (cdr_grid_t *grid) |
Calculates the absolute value of the electric field by linear interpolation of its components. | |
mk_recursive (cdr_calc_eabs, cdr_grid_t) | |
Recursive version of cdr_calc_eabs. | |
mk_recursive (cdr_nonegative, cdr_grid_t) | |
Recursive version of cdr_nonegative. | |
mk_recursive (cdr_set_ext_bnd, cdr_grid_t) | |
Recursive version of cdr_set_ext_bnd. | |
void | cdr_calc_d_dens (cdr_grid_t *grid) |
Calculates the derivatives of the densities for each point of the grid. | |
void | cdr_advect_diffu (cdr_grid_t *grid) |
Adds the advection and diffusion terms to the derivatives of the densities. | |
mk_recursive (cdr_advect_diffu, cdr_grid_t) | |
Limiter function, psi( ![]() ![]() ![]() | |
double | cdr_courant (cdr_grid_t *grid) |
Returns the minimum tau that satisfies the Courant-Friedrichs-Lewy restriction (CFL). | |
void | cdr_update (cdr_grid_t *orig, cdr_grid_t *dest, double h) |
Writes into the densities of the grid dest the densities of grid orig plus its derivatives times h. | |
void | cdr_rk2_update (cdr_grid_t *dens_0, cdr_grid_t *d_dens_1, cdr_grid_t *d_dens_2, cdr_grid_t *dest, double h) |
Sets dest->dens = dens_0->dens + h/2 * d_dens_1->d_dens + h/2 * d_dens_2->d_dens. | |
void | cdr_rk2_update_r (cdr_grid_t *dens_0, cdr_grid_t *d_dens_1, cdr_grid_t *d_dens_2, cdr_grid_t *dest, double h) |
Recursive version of cdr_rk2_update. | |
void | cdr_self_update_r (cdr_grid_t *grid, double h) |
Recursive version of the former QQQQ, used to implement Euler integration. | |
void | cdr_like_update_ar (cdr_grid_t *grid, cdr_grid_t *new_grid, double h) |
Yet another recursive version of cdr_update, used to implement higher order Runge-Kutta integrations. | |
double | cdr_rk2 (cdr_grid_t *grid, double h, double t) |
Computes the grid error. QQQQ. | |
void | cdr_update_refined (cdr_grid_t **ptree) |
Takes a pointer to a family of cdr grids, creates a new family which is stored in that pointer and takes rid of the initial family. | |
void | cdr_calc_maxs (cdr_grid_t *grid) |
Updates the values of max_dens and max_charge. | |
void | cdr_refine (cdr_grid_t *grid) |
Uses the previous routines to refine (if needed) a cdr grid. | |
void | cdr_refine_r (cdr_grid_t *grid, cdr_grid_t *source) |
Recursively refines a cdr grid and constructs a tree based on it. | |
void | cdr_match_r (cdr_grid_t *grid1, cdr_grid_t *grid2) |
Examines pairs of grids and matches them and all their descendants. | |
void | cdr_set_bnd (cdr_grid_t *grid) |
Sets the boundary conditions of all children of grid by interpolating from grid itself. | |
mk_recursive (cdr_set_bnd, cdr_grid_t) | |
Recursive version of cdr_set_bnd. | |
void | cdr_free_mappers (mapper_t **mappers) |
Frees the memory of all mappers QQQQ. | |
void | dens_copy (mapper_t *mapper, grid_t *source, grid_t *target, int ir, int iz, int itheta) |
dens_copy Copies QQQQ | |
int | dens_interpol_set (mapper_t *mapper, grid_t *source, interpol_t *interpol, int pr, int pz, int itheta) |
Interpolates a set QQQQ. | |
void | dens_interpol (mapper_t *mapper, grid_t *source, grid_t *target, interpol_t *interpol, int ir, int iz, int itheta) |
dens_interpol Interpolates QQQQ | |
void | cdr_restrict (cdr_grid_t *grid) |
Restricts a grid with all its children. | |
mk_tail_recursive (cdr_restrict, cdr_grid_t) | |
And its recursive version. | |
void | cdr_set_dens (cdr_grid_t *cdr, int species, int mode, double factor, double(*f)(double, double, double)) |
Sets the densities of species species according to the function ![]() | |
double | f_one (double x, double y, double z) |
Returns 1.0. | |
void | cdr_init_dens (cdr_grid_t *cdr) |
Inits the densities of all species. | |
cdr_grid_t * | cdr_scratch_init (void) |
Initializes the densities allocating a new grid family. | |
void | cdr_dump (cdr_grid_t *grid, const char *prefix, const char *name) |
Dumps all the contents of the given grid into filenames given by prefix and name. | |
void | cdr_dump_r (cdr_grid_t *grid, const char *prefix, const char *name, FILE *infp, double sim_time) |
Dumps all the contents of the given grid into filenames given by prefix and name. | |
cdr_grid_t * | cdr_load_tree_r (const char *prefix, const char *name, FILE *infp) |
Reads the data format produced by cdr_dump_r and creates a complete tree from it. | |
void | cdr_dump_frames (cdr_grid_t *grid, const char *prefix, const char *name) |
Given a grid family, writes in the file named fname the coordinates of the frames that define the grid and his descendants. | |
Variables | |
mapper_t ** | dens_mappers |
mapper_t ** | dens_bnd_mappers |
int | cdr_ext_bnd_cond [4] |
double | z_cutoff = 10000000.0 |
double | max_err = 0.0 |
react_table | mu |
react_table | diff |
pois_problem_t * | pois_electrostatic |
int | curr_seed = 0 |
Routines for handling the CDR (convection-diffusion-reaction) equations.
Default interpolation methods.
May be changed elsewhere.
interpol_method_t *cdr_interpol_inside = &interpol_quadratic;
interpol_method_t *cdr_interpol_bnd = &interpol_bilin;
Definition in file cdr.c.
#define pij | ( | p_, | |
stride_ | |||
) | (((*(p_)) - *((p_) - (stride_))) / (*((p_) + (stride_)) - *(p_))) |
decl_mapper_funcs | ( | dens | ) |
Declaration of the functions to interpolate the densities.
|
static |
|
static |
Sets the proper boundary conditions for a grid that has its left boundary at r = 0 (e.g. the root grid).
We don't set Hom. Neumann at r = r0 since r = r0 is not a real boundary: the proper condition here is matching the opposite grid. But note that this condition reduces to Hom. Neumann in the case ntheta = 1 (because itheta2 = itheta = 0).
Definition at line 798 of file cdr.c.
|
static |
|
static |
|
static |
|
static |
|
static |
Determines the curvature of ar, which has to be a component of grid at ir, iz.
If f is nonnull, determines the curvature of .
Note that we only consider the curvatures in r and z (not in ). This is because
is never refined. (can that be changed in some future?)
Definition at line 1429 of file cdr.c.
|
static |
|
static |
Calls the needs_refinement at all and returns true if the grids needs to be refined in any of the angles.
This is the safest thing to do, but some other criterium could be imagined as well. The search is stopped as soon as a cell that requires refinement is found, but not if it requires refinement due to the leading edge density criterium. This is because we need to find where we have to stop applying that criterium.
Definition at line 1582 of file cdr.c.
|
static |
Determines whether a brick needs refinement.
Now we look if we need refinement in a brick
i.e. a rectangle in r, z space (usually, height = cdr_brick_dz, width = cdr_brick_dr), but can be smaller if we reach a boundary.
See above, any_needs_refinement to see when do we stop the search.
Definition at line 1609 of file cdr.c.
|
static |
|
static |
|
static |
Matches the boundaries of grid to reading from grid fro.
Returns TRUE if there was a matching, FALSE otherwise. Maybe this function should be rewritten to make it more compact and clear.
Definition at line 1808 of file cdr.c.
double gauss2_xyz | ( | double | x, |
double | y, | ||
double | z | ||
) |
void aux_dump_frames_r | ( | cdr_grid_t * | grid, |
FILE * | fp | ||
) |
void cdr_init | ( | void | ) |
void cdr_end | ( | void | ) |
cdr_grid_t* cdr_new_3d_a | ( | int | r0, |
int | z0, | ||
int | r1, | ||
int | z1, | ||
int | ntheta | ||
) |
cdr_grid_t* cdr_guest | ( | cdr_grid_t * | host, |
int | r0, | ||
int | z0, | ||
int | r1, | ||
int | z1 | ||
) |
Creates a guest cdr grid:
one that does not have memory of its own but points to the data of another grid (the host grid). This is useful to easily set boundary conditions. Probably, this function and cdr_new_3d_a should be somehow merged.
Definition at line 170 of file cdr.c.
cdr_grid_t* cdr_like_a | ( | cdr_grid_t * | grid | ) |
cdr_grid_t* cdr_clone_a | ( | cdr_grid_t * | grid | ) |
void cdr_set_periodic | ( | cdr_grid_t * | grid | ) |
mk_recursive | ( | cdr_set_periodic | , |
cdr_grid_t | |||
) |
void cdr_free_r | ( | cdr_grid_t * | grid | ) |
void cdr_calc_charge | ( | cdr_grid_t * | grid | ) |
Calculates the charge in a CDR grid.
Important Note: The charge is here divided by ntheta because our FFT functions are unnormalized and hence when they are applied forth and back they return the initial value multiplied by ntheta. I believe that this is the best point to normalize the data.
Definition at line 361 of file cdr.c.
mk_recursive | ( | cdr_calc_charge | , |
cdr_grid_t | |||
) |
void cdr_dft_field_r | ( | cdr_grid_t * | grid, |
int | sign | ||
) |
cdr_grid_t* cdr_create_coarser_a | ( | cdr_grid_t * | grid | ) |
Creates a grid one level coarser than GRID that covers the same area or somewhat more.
Note: The new grid contains information only about the charge, not the other functions. That's why we don't use cdr_restrict.
Note that here we may be going one cell out of the aparent boundaries of the input grids but we allocated enough memory for that.
Definition at line 435 of file cdr.c.
cdr_grid_t* cdr_add_coarser_grids_a | ( | cdr_grid_t * | prev_root, |
int | n | ||
) |
void cdr_free_coarser_grids | ( | cdr_grid_t * | prev_root, |
int | n | ||
) |
Frees the memory allocated by cdr_add_coarser_grids_a.
Note: be careful to pass here only a tree returned by cdr_add_coarser_grids_a, since some grids may lose their references and stay allocated forever otherwise. (see assert below).
Definition at line 508 of file cdr.c.
pois_grid_t** cdr_calc_field_r | ( | cdr_grid_t * | cdr, |
int | return_pois | ||
) |
The interface between the cdr and the poisson parts.
If return_pois is true, returns the Poisson trees and they have to be de-allocated by the calling program. If not, takes care himself of deallocation and returns NULL.
Definition at line 532 of file cdr.c.
void cdr_add_ext_field | ( | cdr_grid_t * | grid | ) |
Adds an external electric field to a cdr grid.
Really, it would be more efficient to include that field in some other routine and we will avoid one more grid sweep. However, for the sake of code clarity, extendability and maintenability I prefer to add it here.
ext_e_r, ext_e_z and ext_e_theta are functions of three real numbers (r, z, ) that return each of the components of the electric field.
Note that it is the responsibility of e_theta to behave correctly (i.e. periodically) when it receives thetas outside .
mk_recursive | ( | cdr_add_ext_field | , |
cdr_grid_t | |||
) |
void cdr_calc_eabs | ( | cdr_grid_t * | grid | ) |
mk_recursive | ( | cdr_calc_eabs | , |
cdr_grid_t | |||
) |
mk_recursive | ( | cdr_nonegative | , |
cdr_grid_t | |||
) |
mk_recursive | ( | cdr_set_ext_bnd | , |
cdr_grid_t | |||
) |
void cdr_calc_d_dens | ( | cdr_grid_t * | grid | ) |
void cdr_advect_diffu | ( | cdr_grid_t * | grid | ) |
mk_recursive | ( | cdr_advect_diffu | , |
cdr_grid_t | |||
) |
double cdr_courant | ( | cdr_grid_t * | grid | ) |
void cdr_update | ( | cdr_grid_t * | orig, |
cdr_grid_t * | dest, | ||
double | h | ||
) |
Writes into the densities of the grid dest the densities of grid orig plus its derivatives times h.
If orig = dest, this implements an Euler step. But it can also be used as a tool to implement a RK
Definition at line 1075 of file cdr.c.
void cdr_rk2_update | ( | cdr_grid_t * | dens_0, |
cdr_grid_t * | d_dens_1, | ||
cdr_grid_t * | d_dens_2, | ||
cdr_grid_t * | dest, | ||
double | h | ||
) |
void cdr_rk2_update_r | ( | cdr_grid_t * | dens_0, |
cdr_grid_t * | d_dens_1, | ||
cdr_grid_t * | d_dens_2, | ||
cdr_grid_t * | dest, | ||
double | h | ||
) |
void cdr_self_update_r | ( | cdr_grid_t * | grid, |
double | h | ||
) |
void cdr_like_update_ar | ( | cdr_grid_t * | grid, |
cdr_grid_t * | new_grid, | ||
double | h | ||
) |
double cdr_rk2 | ( | cdr_grid_t * | grid, |
double | h, | ||
double | t | ||
) |
Computes the grid error. QQQQ.
Recursive version of cdr_grid_error. QQQQ Makes a full second order Runge-Kutta step.
Returns the timestep actually performed, which can be smaller that h if it does not satisfy the Courant criterium.
Definition at line 1262 of file cdr.c.
void cdr_update_refined | ( | cdr_grid_t ** | ptree | ) |
Takes a pointer to a family of cdr grids, creates a new family which is stored in that pointer and takes rid of the initial family.
This function is the entry point of the refinement code.
Definition at line 1363 of file cdr.c.
void cdr_calc_maxs | ( | cdr_grid_t * | grid | ) |
void cdr_refine | ( | cdr_grid_t * | grid | ) |
void cdr_refine_r | ( | cdr_grid_t * | grid, |
cdr_grid_t * | source | ||
) |
Recursively refines a cdr grid and constructs a tree based on it.
if source == NULL
then uses the initial densities
else sets the densities of the newly created grids by interpolating and copying from the tree source.
Definition at line 1720 of file cdr.c.
void cdr_match_r | ( | cdr_grid_t * | grid1, |
cdr_grid_t * | grid2 | ||
) |
Examines pairs of grids and matches them and all their descendants.
If grid2 == NULL, matches only the descendats of grid1. Hence, this routine is usually called as cdr_match_r (root, NULL).
Definition at line 1763 of file cdr.c.
void cdr_set_bnd | ( | cdr_grid_t * | grid | ) |
mk_recursive | ( | cdr_set_bnd | , |
cdr_grid_t | |||
) |
void cdr_free_mappers | ( | mapper_t ** | mappers | ) |
int dens_interpol_set | ( | mapper_t * | mapper, |
grid_t * | source, | ||
interpol_t * | interpol, | ||
int | pr, | ||
int | pz, | ||
int | itheta | ||
) |
void dens_interpol | ( | mapper_t * | mapper, |
grid_t * | source, | ||
grid_t * | target, | ||
interpol_t * | interpol, | ||
int | ir, | ||
int | iz, | ||
int | itheta | ||
) |
void cdr_restrict | ( | cdr_grid_t * | grid | ) |
mk_tail_recursive | ( | cdr_restrict | , |
cdr_grid_t | |||
) |
void cdr_set_dens | ( | cdr_grid_t * | cdr, |
int | species, | ||
int | mode, | ||
double | factor, | ||
double(*)(double, double, double) | f | ||
) |
Sets the densities of species species according to the function .
mode can be one of the following:
SET_DENS_OVERWRITE: Set the dens to f(x, y, x), ignoring what was there before.
SET_DENS_ADD: Adds f to the former density.
SET_DENS_SUB: Substracts f from the former density.
Definition at line 2126 of file cdr.c.
double f_one | ( | double | x, |
double | y, | ||
double | z | ||
) |
void cdr_init_dens | ( | cdr_grid_t * | cdr | ) |
cdr_grid_t* cdr_scratch_init | ( | void | ) |
void cdr_dump | ( | cdr_grid_t * | grid, |
const char * | prefix, | ||
const char * | name | ||
) |
void cdr_dump_r | ( | cdr_grid_t * | grid, |
const char * | prefix, | ||
const char * | name, | ||
FILE * | infp, | ||
double | sim_time | ||
) |
Dumps all the contents of the given grid into filenames given by prefix and name.
It also writes in tree.NAME.dat the tree structure of grid, in a format appropiate for cdr_load_tree to understand.
infp has to be NULL when called from outside.
The format of a tree.NAME.dat is as follows. Each line is composed by a "command" and some parameters. the "commands" can be
time parameter: sim_time Writes the current simulation time, as received in sim_time
open parameters: gridname r0 z0 r1 z1 level ext_bound margin
Reads the densities from the files given by gridname, with the corresponding values of the grid parameters. Leaves the grid open to add childrens to it. Those children will be all the grids read until the grid is closed.
close parameters: gridname
Closes the latest opened grid. All subsequent grids will be regarded as brothers of gridname.
Definition at line 2360 of file cdr.c.
cdr_grid_t* cdr_load_tree_r | ( | const char * | prefix, |
const char * | name, | ||
FILE * | infp | ||
) |
Reads the data format produced by cdr_dump_r and creates a complete tree from it.
This function is really quick-and-dirty: one should check the fscanfs and do everything cleanlier.
Definition at line 2425 of file cdr.c.
void cdr_dump_frames | ( | cdr_grid_t * | grid, |
const char * | prefix, | ||
const char * | name | ||
) |
react_table mu |
react_table diff |
pois_problem_t* pois_electrostatic |