HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
OutputSolution.cpp
Go to the documentation of this file.
1 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <math.h>
10 #include <basic.h>
11 #include <common_cpp.h>
12 #include <arrayfunctions.h>
13 #include <io_cpp.h>
14 #include <plotfuncs_cpp.h>
15 #include <timeintegration_cpp.h>
16 #include <mpivars_cpp.h>
17 #include <simulation_object.h>
18 
19 #if defined(HAVE_CUDA)
20 # include <arrayfunctions_gpu.h>
21 #endif
22 
23 /* Function declarations */
24 extern "C" void IncrementFilenameIndex(char*,int);
25 
27 int OutputSolution( void* s,
28  int nsims,
29  double a_time )
30 {
31  SimulationObject* simobj = (SimulationObject*) s;
32  int ns;
34 
35  for (ns = 0; ns < nsims; ns++) {
36 
37  HyPar* solver = &(simobj[ns].solver);
38  MPIVariables* mpi = &(simobj[ns].mpi);
39 
40  if ((!solver->WriteOutput) && (strcmp(solver->plot_solution,"yes"))) continue;
41 
42  /* time integration module may have auxiliary arrays to write out, so get them */
43  int NSolutions = 0;
44  IERR TimeGetAuxSolutions(&NSolutions,NULL,solver,-1,ns); CHECKERR(ierr);
45  if (NSolutions > 10) NSolutions = 10;
46 
47  int nu;
48  char fname_root[_MAX_STRING_SIZE_];
49  char aux_fname_root[_MAX_STRING_SIZE_];
50  strcpy(fname_root, solver->op_fname_root);
51  strcpy(aux_fname_root, solver->aux_op_fname_root);
52 
53  if (nsims > 1) {
54  char index[_MAX_STRING_SIZE_];
55  GetStringFromInteger(ns, index, (int)log10(nsims)+1);
56  strcat(fname_root, "_");
57  strcat(fname_root, index);
58  strcat(aux_fname_root, "_");
59  strcat(aux_fname_root, index);
60  }
61 
62  for (nu=0; nu<NSolutions; nu++) {
63 
64  double *uaux = NULL;
65  IERR TimeGetAuxSolutions(&NSolutions,&uaux,solver,nu,ns); CHECKERR(ierr);
66 
67  IERR WriteArray( solver->ndims,
68  solver->nvars,
69  solver->dim_global,
70  solver->dim_local,
71  solver->ghosts,
72  solver->x,
73  uaux,
74  solver,
75  mpi,
76  aux_fname_root ); CHECKERR(ierr);
77 
78  aux_fname_root[2]++;
79  }
80 
81 #if defined(HAVE_CUDA)
82  if (solver->use_gpu) {
83  /* Copy values from GPU memory to CPU memory for writing. */
84  gpuMemcpy(solver->x, solver->gpu_x, sizeof(double)*solver->size_x, gpuMemcpyDeviceToHost);
85 
86 #ifdef CUDA_VAR_ORDERDING_AOS
87  gpuMemcpy( solver->u,
88  solver->gpu_u,
89  sizeof(double)*solver->ndof_cells_wghosts,
91 #else
92  double *h_u = (double *) malloc(sizeof(double)*solver->ndof_cells_wghosts);
93  gpuMemcpy(h_u, solver->gpu_u, sizeof(double)*solver->ndof_cells_wghosts, gpuMemcpyDeviceToHost);
94  for (int i=0; i<solver->npoints_local_wghosts; i++) {
95  for (int v=0; v<solver->nvars; v++) {
96  solver->u[i*solver->nvars+v] = h_u[i+v*solver->npoints_local_wghosts];
97  }
98  }
99  free(h_u);
100 #endif
101  }
102 #endif
103 
104  WriteArray( solver->ndims,
105  solver->nvars,
106  solver->dim_global,
107  solver->dim_local,
108  solver->ghosts,
109  solver->x,
110  solver->u,
111  solver,
112  mpi,
113  fname_root );
114 
115  if (!strcmp(solver->plot_solution, "yes")) {
116  PlotArray( solver->ndims,
117  solver->nvars,
118  solver->dim_global,
119  solver->dim_local,
120  solver->ghosts,
121  solver->x,
122  solver->u,
123  a_time,
124  solver,
125  mpi,
126  fname_root );
127  }
128 
129  /* increment the index string, if required */
130  if ((!strcmp(solver->output_mode,"serial")) && (!strcmp(solver->op_overwrite,"no"))) {
132  }
133 
134  }
135 
136  return(0);
137 }
char plot_solution[_MAX_STRING_SIZE_]
Definition: hypar.h:194
Function declarations for plotting functions.
int nvars
Definition: hypar.h:29
#define IERR
Definition: basic.h:16
char * filename_index
Definition: hypar.h:197
Contains function definitions for common array operations on GPU.
#define CHECKERR(ierr)
Definition: basic.h:18
Structure defining a simulation.
int PlotArray(int, int, int *, int *, int, double *, double *, double, void *, void *, char *)
Definition: PlotArray.c:31
C++ declarations for MPI-related functions.
int TimeGetAuxSolutions(int *, double **, void *, int, int)
double * u
Definition: hypar.h:116
Some basic definitions and macros.
Simulation object.
char output_mode[_MAX_STRING_SIZE_]
Definition: hypar.h:183
double * x
Definition: hypar.h:107
int size_x
Definition: hypar.h:48
double * gpu_x
Definition: hypar.h:457
char aux_op_fname_root[_MAX_STRING_SIZE_]
Definition: hypar.h:208
int ndims
Definition: hypar.h:26
int ndof_cells_wghosts
Definition: hypar.h:47
Structure containing all solver-specific variables and functions.
Definition: hypar.h:23
#define _MAX_STRING_SIZE_
Definition: basic.h:14
Contains C++ function declarations for time integration.
Function declarations for file I/O functions.
double * gpu_u
Definition: hypar.h:459
char op_overwrite[_MAX_STRING_SIZE_]
Definition: hypar.h:191
int WriteArray(int, int, int *, int *, int, double *, double *, void *, void *, char *)
Definition: WriteArray.c:27
void GetStringFromInteger(int, char *, int)
int index_length
Definition: hypar.h:199
char op_fname_root[_MAX_STRING_SIZE_]
Definition: hypar.h:206
int * dim_local
Definition: hypar.h:37
void gpuMemcpy(void *, const void *, size_t, enum gpuMemcpyKind)
void IncrementFilenameIndex(char *, int)
Some common functions used here and there (C++ declarations)
int(* WriteOutput)(int, int, int *, double *, double *, char *, int *)
Definition: hypar.h:211
int ghosts
Definition: hypar.h:52
Structure of MPI-related variables.
int use_gpu
Definition: hypar.h:449
int npoints_local_wghosts
Definition: hypar.h:42
int OutputSolution(void *s, int nsims, double a_time)
#define _DECLARE_IERR_
Definition: basic.h:17
Contains macros and function definitions for common array operations.
int * dim_global
Definition: hypar.h:33