SpatialOps
stencils.cpp
Go to the documentation of this file.
1 
77 #include <spatialops/structured/Grid.h>
78 
79 using namespace SpatialOps;
80 
81 
82 // If we are compiling with GPU CUDA support, create fields on the device.
83 // Otherwise, create them on the host.
84 #ifdef ENABLE_CUDA
85 # define LOCATION GPU_INDEX
86 #else
87 # define LOCATION CPU_INDEX
88 #endif
89 
90 
91 #define PI 3.141592653589793
92 
93 int main()
94 {
95  //----------------------------------------------------------------------------
96  // Define the domain size and number of points
97  const DoubleVec length(PI,PI,PI); // a cube of length pi on each side
98  const IntVec fieldDim( 10, 1, 1 ); // a 1-D problem
99 
100 
101  //----------------------------------------------------------------------------
102  // Create fields
103  const bool bcx=true, bcy=true, bcz=true;
104  const IntVec bc(bcx,bcy,bcz);
105  const GhostData nghost(1);
106  const BoundaryCellInfo sVolBCInfo = BoundaryCellInfo::build<SVolField >( bc, bc );
107  const BoundaryCellInfo ssxBCInfo = BoundaryCellInfo::build<SSurfXField>( bc, bc );
108  const MemoryWindow sVolWindow( get_window_with_ghost( fieldDim, nghost, sVolBCInfo) );
109  const MemoryWindow ssxWindow( get_window_with_ghost( fieldDim, nghost, ssxBCInfo ) );
110 
111  SVolField x( sVolWindow, sVolBCInfo, nghost, NULL, InternalStorage, LOCATION );
112  SVolField f( sVolWindow, sVolBCInfo, nghost, NULL, InternalStorage, LOCATION );
113  SSurfXField dfdx( ssxWindow, ssxBCInfo, nghost, NULL, InternalStorage, LOCATION );
114  SSurfXField fface( ssxWindow, ssxBCInfo, nghost, NULL, InternalStorage, LOCATION );
115 
116  //----------------------------------------------------------------------------
117  // Build a grid. This is a convenient way to set coordinate values that will
118  // be used below.
119  const Grid grid( fieldDim, length );
120  grid.set_coord<XDIR>(x);
121 
122 
123  //----------------------------------------------------------------------------
124  // Build the operators (stencils). Here we will use predefined stencils that
125  // can be built easily.
126  OperatorDatabase opDB; // holds stencils that can be retrieved easily
127  build_stencils( grid, opDB ); // builds stencils and places them in opDB
128 
129  typedef BasicOpTypes<SVolField>::GradX GradX; // x-derivative operator type
130  typedef BasicOpTypes<SVolField>::InterpC2FX InterpX; // x-interpolant operator type
131 
132  const GradX& gradx = *opDB.retrieve_operator<GradX >(); // retrieve the GradX operator
133  const InterpX& interpx = *opDB.retrieve_operator<InterpX>(); // retrieve the InterpX operator
134 
135 
136  //----------------------------------------------------------------------------
137  // perform calculations
138  f <<= sin( x ); // Initialize the function value
139  dfdx <<= gradx( f ); // gradient of f at the x-faces
140  fface <<= interpx( f ); // interpolated value of f at the x-faces
141 
142  return 0;
143 }
provides a template lightweight class for storing 3d vectors of numbers.
Definition: Numeric3Vec.h:45
Provides tools to index into a sub-block of memory.
Definition: MemoryWindow.h:63
MemoryWindow get_window_with_ghost(const IntVec &localDim, const GhostData &ghost, const BoundaryCellInfo &bc)
Obtain the memory window for a field on a patch that is a single, contiguous memory block...
Definition: MemoryWindow.h:314
void build_stencils(const unsigned int nx, const unsigned int ny, const unsigned int nz, const double Lx, const double Ly, const double Lz, OperatorDatabase &opdb)
builds commonly used stencil operators
Provides a simple interface to set coordinate fields.
Definition: Grid.h:36
Provides typedefs for common operator types on a given volume.
Holds information about the number of ghost cells on each side of the domain.
Definition: GhostData.h:54
Defines a type for the x-direction.
Provides a database to hold operators of any type.
Abstracts a field.
Definition: SpatialField.h:132
Provides information about boundary cells for various fields.