Research Article

Finite Element Assembly Using an Embedded Domain Specific Language

Listing 11

Definition of the Coofluid 3 gradient operation, using a user-defined terminal.
(  // Operator definition
()  struct DivOp
()  {
()   // The result is a scalar
()   typedef Real result_type;
()  
()   // Return the divergence of unknown var, computed at mapped_coords
()   template<typename VarT>
()   Real operator()(const VarT& var, const typename VarT::MappedCoordsT& mapped_coords)
() {
()  // Get the gradient matrix
()  const typename VarT::GradientT& nabla = var.nabla(mapped_coords);
()  Real result = 0.;
()  // Apply each component and return the result
()  for(int i = 0; i != VarT::EtypeT::dimensionality; ++i)
()  {
()   result += nabla.row(i) * var.value().col(i);
()  }
()  return result;
() }
()  
() // Divergence at the current quadrature point
() template<typename VarT>
() Real operator()(const VarT& var);
() };
()  
() // The terminal to use in the language
() // divergence(v, xi): compute the divergence at any mapped coordinate xi
() // divergence(v): compute the divergence at current quadrature point
() static MakeSFOp<DivOp>::type const divergence = ;