HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
PlotArray.c
Go to the documentation of this file.
1 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <basic.h>
13 #include <arrayfunctions.h>
14 #include <plotfuncs.h>
15 #include <mpivars.h>
16 #include <hypar.h>
17 
18 #ifdef with_python
19 #include <Python.h>
20 #ifdef with_python_numpy
21 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
22 #include <numpy/arrayobject.h>
23 #endif
24 #endif
25 
26 /* Function declarations */
27 static int PlotArraySerial (int,int,int*,int*,int,double*,double*,double,void*,void*,char*);
28 
31 int PlotArray(int a_ndims,
32  int a_nvars,
33  int* a_dim_global,
34  int* a_dim_local,
35  int a_ghosts,
36  double* a_x,
37  double* a_u,
38  double a_time,
39  void* a_s,
40  void* a_m,
41  char* a_fname_root )
43 {
44  PlotArraySerial( a_ndims,
45  a_nvars,
46  a_dim_global,
47  a_dim_local,
48  a_ghosts,
49  a_x,
50  a_u,
51  a_time,
52  a_s,
53  a_m,
54  a_fname_root );
55 
56  return 0;
57 }
58 
66 int PlotArraySerial(int a_ndims,
67  int a_nvars,
68  int* a_dim_global,
69  int* a_dim_local,
70  int a_ghosts,
71  double* a_x,
72  double* a_u,
73  double a_time,
74  void* a_s,
75  void* a_m,
76  char* a_fname_root )
78 {
79  HyPar *solver = (HyPar*) a_s;
80  MPIVariables *mpi = (MPIVariables*)a_m;
81 
82  int size_global_x = 0;
83  int size_global_u = 0;
84 
85  /* root process: allocate global output arrays */
86  double *ug, *xg;
87  if (!mpi->rank) {
88 
89  size_global_u = 1;
90  for (int d=0; d<a_ndims; d++) size_global_u *= a_dim_global[d];
91  ug = (double*) calloc (size_global_u*a_nvars,sizeof(double));
92  _ArraySetValue_(ug,size_global_u*a_nvars,0.0);
93 
94  size_global_x = 0;
95  for (int d=0; d<a_ndims; d++) size_global_x += a_dim_global[d];
96  xg = (double*) calloc (size_global_x,sizeof(double));
97  _ArraySetValue_(xg,size_global_x,0.0);
98 
99  } else {
100 
101  /* null pointers on non-root processes */
102  ug = xg = NULL;
103 
104  }
105 
106  /* Assemble the local output arrays into the global output arrays */
107  MPIGatherArraynD( a_ndims,
108  mpi,
109  ug,
110  a_u,
111  a_dim_global,
112  a_dim_local,
113  a_ghosts,
114  a_nvars );
115  int offset_global, offset_local;
116  offset_global = offset_local = 0;
117  for (int d=0; d<a_ndims; d++) {
118  MPIGatherArray1D( mpi,
119  (mpi->rank?NULL:&xg[offset_global]),
120  &a_x[offset_local+a_ghosts],
121  mpi->is[d],
122  mpi->ie[d],
123  a_dim_local[d],
124  0 );
125  offset_global += a_dim_global[d];
126  offset_local += a_dim_local [d] + 2*a_ghosts;
127  }
128 
129  if (!mpi->rank) {
130 
131  char filename[_MAX_STRING_SIZE_] = "";
132  strcat(filename,a_fname_root);
133  if (!strcmp(solver->op_overwrite,"no")) {
134  strcat(filename,"_");
135  strcat(filename,solver->filename_index);
136  }
137  strcat(filename,solver->plotfilename_extn);
138 
139 #ifdef with_python
140 #ifdef with_python_numpy
141  import_array();
142  PyObject* py_plt_func = (PyObject*) solver->py_plt_func;
143  PyObject* py_plt_func_args = (PyObject*) solver->py_plt_func_args;
144  py_plt_func_args = PyTuple_New(7);
145  {
146  PyObject* py_obj = Py_BuildValue("i", a_ndims);
147  PyTuple_SetItem(py_plt_func_args, 0, py_obj);
148  }
149  {
150  PyObject* py_obj = Py_BuildValue("i", a_nvars);
151  PyTuple_SetItem(py_plt_func_args, 1, py_obj);
152  }
153  {
154  npy_intp shape[1] = {a_ndims};
155  PyObject* size_arr = PyArray_SimpleNewFromData(1,shape,NPY_INT,a_dim_global);
156  PyTuple_SetItem(py_plt_func_args, 2, size_arr);
157  }
158  {
159  PyObject* py_obj = Py_BuildValue("d", a_time);
160  PyTuple_SetItem(py_plt_func_args, 3, py_obj);
161  }
162  {
163  npy_intp shape[1] = {size_global_x};
164  PyObject* x_arr = PyArray_SimpleNewFromData(1,shape,NPY_DOUBLE,xg);
165  PyTuple_SetItem(py_plt_func_args, 4, x_arr);
166  }
167  {
168  npy_intp shape[1] = {size_global_u*a_nvars};
169  PyObject* u_arr = PyArray_SimpleNewFromData(1,shape,NPY_DOUBLE,ug);
170  PyTuple_SetItem(py_plt_func_args, 5, u_arr);
171  }
172  {
173  PyObject* py_obj = Py_BuildValue("s", filename);
174  PyTuple_SetItem(py_plt_func_args, 6, py_obj);
175  }
176  if (!py_plt_func) {
177  fprintf(stderr,"Error in PlotArraySerial(): solver->py_plt_func is NULL!\n");
178  } else {
179  PyObject_CallObject(py_plt_func, py_plt_func_args);
180  }
181 #else
182  fprintf(stderr,"Error in PlotArraySerial(): HyPar needs to be compiled with Numpy support (-Dwith_python_numpy) to use this feature..\n");
183 #endif
184 #else
185  fprintf(stderr,"Error in PlotArraySerial(): HyPar needs to be compiled with Python support (-Dwith_python) to use this feature..\n");
186 #endif
187 
188  /* Clean up output arrays */
189  free(xg);
190  free(ug);
191  }
192 
193  return 0;
194 }
MPI related function definitions.
char * filename_index
Definition: hypar.h:197
char plotfilename_extn[_MAX_STRING_SIZE_]
Definition: hypar.h:203
Some basic definitions and macros.
void * py_plt_func
Definition: hypar.h:466
int MPIGatherArraynD(int, void *, double *, double *, int *, int *, int, int)
Structure containing all solver-specific variables and functions.
Definition: hypar.h:23
Function declarations for plotting functions.
#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
#define _ArraySetValue_(x, size, value)
int PlotArray(int a_ndims, int a_nvars, int *a_dim_global, int *a_dim_local, int a_ghosts, double *a_x, double *a_u, double a_time, void *a_s, void *a_m, char *a_fname_root)
Definition: PlotArray.c:31
Structure of MPI-related variables.
Contains macros and function definitions for common array operations.
void * py_plt_func_args
Definition: hypar.h:467
static int PlotArraySerial(int, int, int *, int *, int, double *, double *, double, void *, void *, char *)
Definition: PlotArray.c:66