SpatialOps
masks.cpp
Go to the documentation of this file.
1 
60 #include <spatialops/structured/Grid.h>
62 
63 using namespace SpatialOps;
64 using namespace std;
65 
66 // If we are compiling with GPU CUDA support, create fields on the device.
67 // Otherwise, create them on the host.
68 #ifdef ENABLE_CUDA
69 # define LOCATION GPU_INDEX
70 #else
71 # define LOCATION CPU_INDEX
72 #endif
73 
74 int main()
75 {
76  //----------------------------------------------------------------------------
77  // Define the domain size and number of points
78  const DoubleVec length( 5, 5, 5 ); // a cube of length 5.0
79  const IntVec fieldDim( 5, 5, 1 ); // a 5 x 5 x 1 problem
80 
81  //----------------------------------------------------------------------------
82  // Create fields
83  const GhostData nghost(0); // try changing this to 1 and see what happens.
84  const BoundaryCellInfo sVolBCInfo = BoundaryCellInfo::build<SVolField>( true, true, true );
85  const MemoryWindow sVolWindow( get_window_with_ghost( fieldDim, nghost, sVolBCInfo) );
86 
87  SVolField x( sVolWindow, sVolBCInfo, nghost, NULL, InternalStorage, LOCATION );
88  SVolField y( sVolWindow, sVolBCInfo, nghost, NULL, InternalStorage, LOCATION );
89  SVolField f( sVolWindow, sVolBCInfo, nghost, NULL, InternalStorage, LOCATION );
90 
91  //----------------------------------------------------------------------------
92  // Build a coordinates.
93  const Grid grid( fieldDim, length );
94  grid.set_coord<XDIR>(x);
95  grid.set_coord<YDIR>(y);
96 
97  //----------------------------------------------------------------------------
98  // Assign f:
99  f <<= x + y;
100 
101  //----------------------------------------------------------------------------
102  // Build a mask.
103  // A mask is a set of indices where something different should happen, such
104  // as a boundary condition: Edge of a flame, fuel injection or exhaust pipe.
105  // Indexing for masks has the origin at the first interior point.
106  // (Ghost points are negative indices.)
107  vector<IntVec> maskSet;
108  for( int i=0; i<fieldDim[1]; ++i ){
109  maskSet.push_back( IntVec(0, i, 0) );
110  }
111 
112  // Creating a mask requires a prototype field and a vector of indices.
113  SpatialMask<SVolField> mask( f, maskSet );
114 # ifdef ENABLE_CUDA
115  // Note that these are created only on a CPU, and must be explicitly
116  // transferred to the GPU for CUDA enabled cases as shown below.
117  mask.add_consumer( GPU_INDEX );
118 # endif
119 
120  //----------------------------------------------------------------------------
121  // Use the mask:
122 # ifdef ENABLE_CUDA
123  // If f uses GPU memory, f needs to be copied to CPU memory to print it.
124  f.add_device( CPU_INDEX );
125 # endif
126  cout << "f before applying mask:" << endl;
127  print_field( f, cout );
128 
129  f <<= cond( mask, 90.0 )
130  ( f );
131 
132 # ifdef ENABLE_CUDA
133  // If f uses GPU memory, f needs to be copied to CPU memory to print it.
134  f.add_device( CPU_INDEX );
135 # endif
136  cout << "f after applying mask:" << endl;
137  print_field( f, cout );
138 
139  return 0;
140 }
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 print_field(const Field &f, std::ostream &os, const bool addFormat=false)
print the values of a field (and ghost cells) to standard output
Definition: FieldHelper.h:281
STL namespace.
Abstracts a mask.
Definition: SpatialMask.h:70
Provides a simple interface to set coordinate fields.
Definition: Grid.h:36
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.
Defines a type for the y-direction.
Abstracts a field.
Definition: SpatialField.h:132
Provides information about boundary cells for various fields.