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 IntVec bc(true, true, true);
91  const BoundaryCellInfo bcInfo = BoundaryCellInfo::build<FieldT>( bc,bc );
92  const MemoryWindow window( get_window_with_ghost( fieldDim, nghost, bcInfo ) );
93 
94  FieldT x( window, bcInfo, nghost, NULL, InternalStorage, LOCATION );
95  FieldT y( window, bcInfo, nghost, NULL, InternalStorage, LOCATION );
96  FieldT z( window, bcInfo, nghost, NULL, InternalStorage, LOCATION );
97 
98  FieldT f( window, bcInfo, nghost, NULL, InternalStorage, LOCATION );
99  FieldT g( window, bcInfo, nghost, NULL, InternalStorage, LOCATION );
100 
101  //----------------------------------------------------------------------------
102  // Build a grid. This is a convenient way to set coordinate values that will
103  // be used below.
104  const Grid grid( fieldDim, length );
105  grid.set_coord<XDIR>(x);
106  grid.set_coord<YDIR>(y);
107  grid.set_coord<ZDIR>(z);
108 
109  //----------------------------------------------------------------------------
110  // Print fields to standard output:
111  std::cout << "x:" << std::endl; print_field( x, std::cout );
112  std::cout << "y:" << std::endl; print_field( y, std::cout );
113  std::cout << "z:" << std::endl; print_field( z, std::cout );
114 
115  //----------------------------------------------------------------------------
116  // Perform operations on fields
117 
118  // Assign field values. Note that this is a vectorized statement that will
119  // work on GPU, serial CPU and multicore CPU.
120  f <<= sin(x) + cos(y) + tanh(z);
121 
122 # ifdef ENABLE_CUDA
123  //If f uses GPU memory, to print f, f needs to be copied to CPU memory.
124  f.add_device( CPU_INDEX );
125 # endif
126  std::cout << "f:" << std::endl;
127  print_field( f, std::cout );
128 
129  //----------------------------------------------------------------------------
130  // Conditional (if/then/else...) evaluation.
131  // cond in Nebo creates conditional expressions. Each cond clause, except the last,
132  // must have two arguments. The first argument is the condition (if this), and the
133  // second is the result (then that). The final clause takes only one argument
134  // (else other), which is returned only if all previous conditions fail.
135  //
136  // The conditions are evaluated first to last, and evaluation stops when a
137  // condition returns true. Thus, the order of conditions can and will effect
138  // the results.
139  g <<= cond( f > 2.0, x+z ) // if ( f[i] > 2.0 ) g[i] = x[i]+z[i];
140  ( f > 1.5, y ) // else if( f[i] > 1.5 ) g[i] = y[i];
141  ( f > 1.0, -f ) // else if( f[i] > 1.0 ) g[i] = -f[i];
142  ( f ); // else g[i] = f[i];
143 
144 # ifdef ENABLE_CUDA
145  //If g uses GPU memory, to print g, g needs to be copied to CPU memory.
146  g.add_device( CPU_INDEX );
147 # endif
148  std::cout << "g:" << std::endl;
149  print_field( g, std::cout );
150 
151  return 0;
152 }
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.