HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
VlasovPlotSpatialField.c
Go to the documentation of this file.
1 
6 #include <stdlib.h>
7 #include <string.h>
8 #include <basic.h>
9 #include <common.h>
10 #include <arrayfunctions.h>
11 #include <mathfunctions.h>
12 #include <io.h>
13 #include <mpivars.h>
14 #include <hypar.h>
15 #include <physicalmodels/vlasov.h>
16 
17 #ifdef with_python
18 #include <Python.h>
19 #ifdef with_python_numpy
20 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
21 #include <numpy/arrayobject.h>
22 #endif
23 #endif
24 
26 int VlasovPlotSpatialField( void* s,
27  void* m,
28  double* a_field,
29  double a_time,
30  char* fname_root )
33 {
34  HyPar *solver = (HyPar*) s;
35  MPIVariables *mpi = (MPIVariables*) m;
36  Vlasov *param = (Vlasov*) solver->physics;
37 
38  if (solver->nsims > 1) {
39  char index[_MAX_STRING_SIZE_];
40  GetStringFromInteger(solver->my_idx, index, (int)log10(solver->nsims)+1);
41  strcat(fname_root, "_");
42  strcat(fname_root, index);
43  strcat(fname_root, "_");
44  }
45 
46  char filename[_MAX_STRING_SIZE_] = "";
47  strcat(filename,fname_root);
48  if (!strcmp(solver->op_overwrite,"no")) {
49  strcat(filename,"_");
50  strcat(filename,solver->filename_index);
51  }
52  strcat(filename,".png");
53 
54  int d,
55  ghosts = solver->ghosts,
56  ndims_x = param->ndims_x;
57 
58  int dim_global_x[ndims_x];
59  _ArrayCopy1D_(solver->dim_global, dim_global_x, ndims_x);
60  int dim_local_x[ndims_x];
61  _ArrayCopy1D_(solver->dim_local, dim_local_x, ndims_x);
62 
63  /* gather the spatial coordinates into a global array */
64  double *xg;
65  {
66  int size_g = param->npts_global_x;
67  xg = (double*) calloc (size_g, sizeof(double));
68  _ArraySetValue_(xg, size_g, 0.0);
69 
70  int offset_global, offset_local;
71  offset_global = offset_local = 0;
72  for (d=0; d<ndims_x; d++) {
74  (mpi->rank?NULL:&xg[offset_global]),
75  &solver->x[offset_local+ghosts],
76  mpi->is[d],
77  mpi->ie[d],
78  solver->dim_local[d],
79  0);
80  offset_global += dim_global_x[d];
81  offset_local += dim_local_x [d] + 2*ghosts;
82  }
83  }
84 
85  /* gather the field into a global array */
86  double *field_g;
87  {
88  int size_g = param->npts_global_x * param->ndims_x;
89  field_g = (double*) calloc (size_g, sizeof(double));
90  _ArraySetValue_(field_g, size_g, 0.0);
91 
92  if (param->ndims_x > 1) {
93  if (!mpi->rank) {
94  fprintf(stderr,"Warning in VlasovPlotSpatialField():\n");
95  fprintf(stderr," field plotting not yet supported for >1 spatial dimensions.\n");
96  }
97  } else {
99  (mpi->rank ? NULL : field_g),
100  (a_field+ghosts),
101  mpi->is[0],
102  mpi->ie[0],
103  solver->dim_local[0],
104  0);
105  }
106  }
107 
108  if (!mpi->rank) {
109 #ifdef with_python
110 #ifdef with_python_numpy
111  import_array();
112  PyObject* py_plt_func = (PyObject*) solver->py_plt_func;
113  PyObject* py_plt_func_args = (PyObject*) solver->py_plt_func_args;
114  py_plt_func_args = PyTuple_New(7);
115  {
116  PyObject* py_obj = Py_BuildValue("i", ndims_x);
117  PyTuple_SetItem(py_plt_func_args, 0, py_obj);
118  }
119  {
120  PyObject* py_obj = Py_BuildValue("i", 1);
121  PyTuple_SetItem(py_plt_func_args, 1, py_obj);
122  }
123  {
124  npy_intp shape[1] = {ndims_x};
125  PyObject* size_arr = PyArray_SimpleNewFromData(1,shape,NPY_INT,dim_global_x);
126  PyTuple_SetItem(py_plt_func_args, 2, size_arr);
127  }
128  {
129  PyObject* py_obj = Py_BuildValue("d", a_time);
130  PyTuple_SetItem(py_plt_func_args, 3, py_obj);
131  }
132  {
133  npy_intp shape[1] = {param->npts_global_x};
134  PyObject* x_arr = PyArray_SimpleNewFromData(1,shape,NPY_DOUBLE,xg);
135  PyTuple_SetItem(py_plt_func_args, 4, x_arr);
136  }
137  {
138  npy_intp shape[1] = {param->npts_global_x * param->ndims_x};
139  PyObject* u_arr = PyArray_SimpleNewFromData(1,shape,NPY_DOUBLE,field_g);
140  PyTuple_SetItem(py_plt_func_args, 5, u_arr);
141  }
142  {
143  PyObject* py_obj = Py_BuildValue("s", filename);
144  PyTuple_SetItem(py_plt_func_args, 6, py_obj);
145  }
146  if (!py_plt_func) {
147  fprintf(stderr,"Error in PlotArraySerial(): solver->py_plt_func is NULL!\n");
148  } else {
149  PyObject_CallObject(py_plt_func, py_plt_func_args);
150  }
151 #else
152  fprintf(stderr,"Error in PlotArraySerial(): HyPar needs to be compiled with Numpy support (-Dwith_python_numpy) to use this feature..\n");
153 #endif
154 #else
155  fprintf(stderr,"Error in PlotArraySerial(): HyPar needs to be compiled with Python support (-Dwith_python) to use this feature..\n");
156 #endif
157  }
158 
159  /* free up arrays */
160  free(xg);
161  free(field_g);
162 
163  return 0;
164 }
Definition: vlasov.h:57
Some common functions used here and there.
#define IERR
Definition: basic.h:16
MPI related function definitions.
char * filename_index
Definition: hypar.h:197
Contains function definitions for common mathematical functions.
int nsims
Definition: hypar.h:64
Some basic definitions and macros.
int VlasovPlotSpatialField(void *s, void *m, double *a_field, double a_time, char *fname_root)
double * x
Definition: hypar.h:107
void * py_plt_func
Definition: hypar.h:466
Vlasov Equation.
long npts_global_x
Definition: vlasov.h:72
Structure containing all solver-specific variables and functions.
Definition: hypar.h:23
#define _MAX_STRING_SIZE_
Definition: basic.h:14
int MPIGatherArray1D(void *, double *, double *, int, int, int, int)
Contains structure definition for hypar.
char op_overwrite[_MAX_STRING_SIZE_]
Definition: hypar.h:191
void GetStringFromInteger(int, char *, int)
int my_idx
Definition: hypar.h:61
#define _ArraySetValue_(x, size, value)
int * dim_local
Definition: hypar.h:37
void * physics
Definition: hypar.h:266
int ghosts
Definition: hypar.h:52
Structure of MPI-related variables.
Function declarations for file I/O functions.
#define _ArrayCopy1D_(x, y, size)
Contains macros and function definitions for common array operations.
int * dim_global
Definition: hypar.h:33
void * py_plt_func_args
Definition: hypar.h:467
int ndims_x
Definition: vlasov.h:63