HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
PlotArray.c File Reference

Plot a vector field, stored as an array, and save to file. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <basic.h>
#include <arrayfunctions.h>
#include <plotfuncs.h>
#include <mpivars.h>
#include <hypar.h>
#include <Python.h>
#include <numpy/arrayobject.h>

Go to the source code of this file.

Macros

#define NPY_NO_DEPRECATED_API   NPY_1_7_API_VERSION
 

Functions

static int PlotArraySerial (int, int, int *, int *, int, double *, double *, double, void *, void *, char *)
 
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)
 

Detailed Description

Plot a vector field, stored as an array, and save to file.

Author
Debojyoti Ghosh Contains functions to plot out a vector field, stored as an array and save the figure to a file.

Definition in file PlotArray.c.

Macro Definition Documentation

◆ NPY_NO_DEPRECATED_API

#define NPY_NO_DEPRECATED_API   NPY_1_7_API_VERSION

Definition at line 21 of file PlotArray.c.

Function Documentation

◆ PlotArraySerial()

int PlotArraySerial ( 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 
)
static

Function to plot a vector field, stored as an array, and save the figure to a file. It will allocate the global domain on rank 0, so do not use for big problems for which the entire global domain will not fit on one node. This approach is also not very scalable.

  • Needs HyPar to be compiled with Python.
Parameters
a_ndimsNumber of spatial dimensions
a_nvarsNumber of variables per grid point
a_dim_globalInteger array of size a_ndims with global grid size in each dimension
a_dim_localInteger array of size a_ndims with local grid size in each dimension
a_ghostsNumber of ghost points
a_xArray of spatial coordinates (i.e. the grid)
a_uVector field to write
a_timeCurrent simulation time
a_sSolver object of type HyPar
a_mMPI object of type MPIVariables
a_fname_rootFilename root (extension is added automatically). For unsteady output, a numerical index is added that is the same as for the solution output files.

Definition at line 66 of file PlotArray.c.

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 }
char * filename_index
Definition: hypar.h:197
char plotfilename_extn[_MAX_STRING_SIZE_]
Definition: hypar.h:203
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
#define _MAX_STRING_SIZE_
Definition: basic.h:14
int MPIGatherArray1D(void *, double *, double *, int, int, int, int)
char op_overwrite[_MAX_STRING_SIZE_]
Definition: hypar.h:191
#define _ArraySetValue_(x, size, value)
Structure of MPI-related variables.
void * py_plt_func_args
Definition: hypar.h:467

◆ PlotArray()

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 
)

Plot a vector field, stored as an array, and save figure to file: this is a wrapper function that calls PlotArraySerial().

Parameters
a_ndimsNumber of spatial dimensions
a_nvarsNumber of variables per grid point
a_dim_globalInteger array of size a_ndims with global grid size in each dimension
a_dim_localInteger array of size a_ndims with local grid size in each dimension
a_ghostsNumber of ghost points
a_xArray of spatial coordinates (i.e. the grid)
a_uVector field to write
a_timeCurrent simulation time
a_sSolver object of type HyPar
a_mMPI object of type MPIVariables
a_fname_rootFilename root (extension is added automatically). For unsteady output, a numerical index is added that is the same as for the solution output files.

Definition at line 31 of file PlotArray.c.

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 }
static int PlotArraySerial(int, int, int *, int *, int, double *, double *, double, void *, void *, char *)
Definition: PlotArray.c:66