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 IntVec bc(true, true, false);
85  const BoundaryCellInfo sVolBCInfo = BoundaryCellInfo::build<SVolField>( bc, bc );
86  const MemoryWindow sVolWindow( get_window_with_ghost( fieldDim, nghost, sVolBCInfo) );
87 
88  SVolField x( sVolWindow, sVolBCInfo, nghost, NULL, InternalStorage, LOCATION );
89  SVolField y( sVolWindow, sVolBCInfo, nghost, NULL, InternalStorage, LOCATION );
90  SVolField f( sVolWindow, sVolBCInfo, nghost, NULL, InternalStorage, LOCATION );
91 
92  //----------------------------------------------------------------------------
93  // Build a coordinates.
94  const Grid grid( fieldDim, length );
95  grid.set_coord<XDIR>(x);
96  grid.set_coord<YDIR>(y);
97 
98  //----------------------------------------------------------------------------
99  // Assign f:
100  f <<= x + y;
101 
102  //----------------------------------------------------------------------------
103  // Build a mask.
104  // A mask is a set of indices where something different should happen, such
105  // as a boundary condition: Edge of a flame, fuel injection or exhaust pipe.
106  // Indexing for masks has the origin at the first interior point.
107  // (Ghost points are negative indices.)
108  vector<IntVec> maskSet;
109  for( int i=0; i<fieldDim[1]; ++i ){
110  maskSet.push_back( IntVec(0, i, 0) );
111  }
112 
113  // Creating a mask requires a prototype field and a vector of indices.
114  SpatialMask<SVolField> mask( f, maskSet );
115 # ifdef ENABLE_CUDA
116  // Note that these are created only on a CPU, and must be explicitly
117  // transferred to the GPU for CUDA enabled cases as shown below.
118  mask.add_consumer( GPU_INDEX );
119 # endif
120 
121  //----------------------------------------------------------------------------
122  // Use the mask:
123 # ifdef ENABLE_CUDA
124  // If f uses GPU memory, f needs to be copied to CPU memory to print it.
125  f.add_device( CPU_INDEX );
126 # endif
127  cout << "f before applying mask:" << endl;
128  print_field( f, cout );
129 
130  f <<= cond( mask, 90.0 )
131  ( f );
132 
133 # ifdef ENABLE_CUDA
134  // If f uses GPU memory, f needs to be copied to CPU memory to print it.
135  f.add_device( CPU_INDEX );
136 # endif
137  cout << "f after applying mask:" << endl;
138  print_field( f, cout );
139 
140  return 0;
141 }
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.