SpatialOps
field_operations.cpp
Go to the documentation of this file.
1 
65 #include <spatialops/structured/Grid.h> // a convenient way to define coordinates
66 #include <spatialops/structured/FieldHelper.h> // provides methods (print_field()) to view small fields
67 
68 using namespace SpatialOps;
69 
70 // If we are compiling with GPU CUDA support, create fields on the device.
71 // Otherwise, create them on the host.
72 #ifdef ENABLE_CUDA
73 # define LOCATION GPU_INDEX
74 #else
75 # define LOCATION CPU_INDEX
76 #endif
77 
78 int main()
79 {
80  // Define the size of the domain
81  const IntVec fieldDim( 5, 5, 1 ); // (nx,ny,nz)
82  const DoubleVec length( 1.0, 1.0, 1.0 ); // a cube of unit length
83 
84  //----------------------------------------------------------------------------
85  // Create fields:
86  typedef SpatialOps::SVolField FieldT;
87 
88  // Use default values to create objects that are required to construct a field.
89  const GhostData nghost(0);
90  const BoundaryCellInfo bcInfo = BoundaryCellInfo::build<FieldT>( true, true, true );
91  const MemoryWindow window( get_window_with_ghost( fieldDim, nghost, bcInfo ) );
92 
93  FieldT x( window, bcInfo, nghost, NULL, InternalStorage, LOCATION );
94  FieldT y( window, bcInfo, nghost, NULL, InternalStorage, LOCATION );
95  FieldT z( window, bcInfo, nghost, NULL, InternalStorage, LOCATION );
96 
97  FieldT f( window, bcInfo, nghost, NULL, InternalStorage, LOCATION );
98  FieldT g( window, bcInfo, nghost, NULL, InternalStorage, LOCATION );
99 
100  //----------------------------------------------------------------------------
101  // Build a grid. This is a convenient way to set coordinate values that will
102  // be used below.
103  const Grid grid( fieldDim, length );
104  grid.set_coord<XDIR>(x);
105  grid.set_coord<YDIR>(y);
106  grid.set_coord<ZDIR>(z);
107 
108  //----------------------------------------------------------------------------
109  // Print fields to standard output:
110  std::cout << "x:" << std::endl; print_field( x, std::cout );
111  std::cout << "y:" << std::endl; print_field( y, std::cout );
112  std::cout << "z:" << std::endl; print_field( z, std::cout );
113 
114  //----------------------------------------------------------------------------
115  // Perform operations on fields
116 
117  // Assign field values. Note that this is a vectorized statement that will
118  // work on GPU, serial CPU and multicore CPU.
119  f <<= sin(x) + cos(y) + tanh(z);
120 
121 # ifdef ENABLE_CUDA
122  //If f uses GPU memory, to print f, f needs to be copied to CPU memory.
123  f.add_device( CPU_INDEX );
124 # endif
125  std::cout << "f:" << std::endl;
126  print_field( f, std::cout );
127 
128  //----------------------------------------------------------------------------
129  // Conditional (if/then/else...) evaluation.
130  // cond in Nebo creates conditional expressions. Each cond clause, except the last,
131  // must have two arguments. The first argument is the condition (if this), and the
132  // second is the result (then that). The final clause takes only one argument
133  // (else other), which is returned only if all previous conditions fail.
134  //
135  // The conditions are evaluated first to last, and evaluation stops when a
136  // condition returns true. Thus, the order of conditions can and will effect
137  // the results.
138  g <<= cond( f > 2.0, x+z ) // if ( f[i] > 2.0 ) g[i] = x[i]+z[i];
139  ( f > 1.5, y ) // else if( f[i] > 1.5 ) g[i] = y[i];
140  ( f > 1.0, -f ) // else if( f[i] > 1.0 ) g[i] = -f[i];
141  ( f ); // else g[i] = f[i];
142 
143 # ifdef ENABLE_CUDA
144  //If g uses GPU memory, to print g, g needs to be copied to CPU memory.
145  g.add_device( CPU_INDEX );
146 # endif
147  std::cout << "g:" << std::endl;
148  print_field( g, std::cout );
149 
150  return 0;
151 }
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
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.
Defines a type for the z-direction.
Abstracts a field.
Definition: SpatialField.h:132
Provides information about boundary cells for various fields.