COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-scalar-advection.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2011 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6  // boost UTF (from boost-doc:):
7 #define BOOST_TEST_DYN_LINK // To build/use dynamic library.
8 #define BOOST_TEST_MODULE "Test module for heat-conduction related proto operations" // To generate the test module initialization function, which uses the defined value to name the master
9  // test suite. For dynamic library variant default function main() implementation is generated as well
10 #include <boost/test/unit_test.hpp>
11 
12 #define BOOST_PROTO_MAX_ARITY 10 // Controls the maximum number of child nodes an expression may have.
13 #ifdef BOOST_MPL_LIMIT_METAFUNCTION_ARITY // Is an overridable configuration macro regulating the maximum supported arity of metafunctions and
14  #undef BOOST_MPL_LIMIT_METAFUNCTION_ARITY // metafunction classes.
15  #define BOOST_MPL_LIMIT_METAFUNCTION_ARITY 10
16 #endif
17 
18 #include "common/Core.hpp"
19 #include "common/Environment.hpp"
20 
21 #include "math/LSS/System.hpp"
22 
23 #include "mesh/Domain.hpp"
24 
26 #include "solver/ModelUnsteady.hpp"
27 
29 #include "solver/CriterionTime.hpp"
31 #include "solver/Time.hpp"
32 #include "math/LSS/SolveLSS.hpp"
33 #include "math/LSS/ZeroLSS.hpp"
34 
37 
39 
41 
43 #include "UFEM/Solver.hpp"
44 #include "UFEM/Tags.hpp"
45 
46 #include "UFEM/SUPG.hpp"
47 
48 using namespace cf3;
49 using namespace cf3::solver;
50 using namespace cf3::solver::actions;
51 using namespace cf3::solver::actions::Proto;
52 using namespace cf3::common;
53 using namespace cf3::math::Consts;
54 using namespace cf3::mesh;
55 
56 using boost::proto::lit;
57 
58 
59 typedef std::vector<std::string> StringsT;
60 typedef std::vector<Uint> SizesT;
61 
63 inline void
64 check_close(const Real a, const Real b, const Real threshold)
65 {
66  BOOST_CHECK_CLOSE(a, b, threshold);
67 }
68 
69 static boost::proto::terminal< void(*)(Real, Real, Real) >::type const _check_close = {&check_close};
70 
71 struct ProtoHeatFixture
72 {
74  length(1.),
75  Pe(0.0001),
76  Pe2(100.),
77  left_temp(1.),
78  right_temp(0.),
79  nb_segments(10),
80  alpha(1.),
81 
82 
83  root( Core::instance().root() )
84  {
85  }
86 
87  Component& root;
88 
90  void set_analytical_solution(Region& region, const std::string& field_name, const std::string& var_name)
91  {
92  FieldVariable<0, ScalarField > Temp(field_name, var_name);
93 
94  // Zero the field
96  (
97  region,
98  Temp = (_exp(Pe2*coordinates[0])-1.)/(_exp(Pe2)-1.) + 1.
99  );
100 
101  }
102 
103  const Real length;
104  const Real Pe;
105  const Real Pe2;
107  const Real alpha;
108  const Real left_temp;
109  const Real right_temp;
110 
111  Real t;
112 
113 };
114 
115 
116 BOOST_FIXTURE_TEST_SUITE( ProtoHeatSuite, ProtoHeatFixture )
117 
119 {
120  common::PE::Comm::instance().init(boost::unit_test::framework::master_test_suite().argc, boost::unit_test::framework::master_test_suite().argv);
121  BOOST_CHECK_EQUAL(common::PE::Comm::instance().size(), 1);
122 }
123 
124 BOOST_AUTO_TEST_CASE( Heat1DComponent )
125 {
126  Core::instance().environment().options().set("log_level", 4u);
127 
128  // Parameters
129  Real length = 1.;
130  const Uint nb_segments = 100 ;
131 
132  // Setup a model
133  ModelUnsteady& model = *root.create_component<ModelUnsteady>("Model");
134  Domain& domain = model.create_domain("Domain");
135  UFEM::Solver& solver = *model.create_component<UFEM::Solver>("Solver");
136  Handle<UFEM::LSSActionUnsteady> lss_action(solver.add_unsteady_solver("cf3.UFEM.LSSActionUnsteady"));
137  Handle<common::ActionDirector> ic(solver.get_child("InitialConditions"));
138 
139  // Proto placeholders
140  FieldVariable<0, ScalarField> T("Temperature", UFEM::Tags::solution());
141  FieldVariable<1, VectorField> u_adv("AdvectionVelocity","linearized_velocity");
142  FieldVariable<2, ScalarField> temperature_analytical("TemperatureAnalytical", UFEM::Tags::source_terms());
143  FieldVariable<3, ScalarField> nu_eff("EffectiveViscosity", "navier_stokes_viscosity");
144 
145  PhysicsConstant nu("kinematic_viscosity");
146 
147  const Real alpha = 1;
148 
149  Real tau_su;
150 
151  // Allowed elements (reducing this list improves compile times)
152  boost::mpl::vector1<mesh::LagrangeP1::Line1D> allowed_elements;
153 
154  // BCs
155  boost::shared_ptr<UFEM::BoundaryConditions> bc = allocate_component<UFEM::BoundaryConditions>("BoundaryConditions");
156 
157  RealVector initial_u(1); initial_u.setConstant(-10.);
158 
160 
161  // add the top-level actions (assembly, BC and solve)
162  *ic << create_proto_action("Initialize", nodes_expression(group(T = 0., u_adv = initial_u, nu_eff = nu)));
163  *lss_action
164  << allocate_component<math::LSS::ZeroLSS>("ZeroLSS")
165  << create_proto_action
166  (
167  "Assembly",
169  (
170  allowed_elements,
171  group
172  (
173  _A = _0, _T = _0,
174  compute_tau.apply(u_adv, nu_eff, lit(lss_action->dt()), lit(tau_su)),
175  element_quadrature( _A(T) += transpose(N(T)) * u_adv * nabla(T) + tau_su * transpose(u_adv*nabla(T)) * u_adv * nabla(T) + alpha * transpose(nabla(T)) * nabla(T) ,
176  _T(T,T) += transpose(N(T) + tau_su * u_adv * nabla(T)) * N(T) ),
177  lss_action->system_matrix += lss_action->invdt() * _T + 1.0 * _A,
178  lss_action->system_rhs += -_A * _x
179  )
180  )
181  )
182  << bc
183  << allocate_component<math::LSS::SolveLSS>("SolveLSS")
184  << create_proto_action("Increment", nodes_expression(T += lss_action->solution(T)))
185  << allocate_component<solver::actions::AdvanceTime>("AdvanceTime")
186  << create_proto_action("Output", nodes_expression(_cout << "T(" << coordinates(0,0) << ") = " << T << "\n"));
187 
188  // Setup physics
189  physics::PhysModel& physical_model = model.create_physics("cf3.UFEM.NavierStokesPhysics");
190  physical_model.options().set("dynamic_viscosity", 1.7894e-5);
191  physical_model.options().set("density", 1.);
192 
193  // Setup mesh
194  // Mesh& mesh = *domain.create_component<Mesh>("Mesh");
195  // Tools::MeshGeneration::create_line(mesh, length, nb_segments);
196  boost::shared_ptr<MeshGenerator> create_line = build_component_abstract_type<MeshGenerator>("cf3.mesh.SimpleMeshGenerator","create_line");
197  create_line->options().set("mesh",domain.uri()/"Mesh");
198  create_line->options().set("lengths",std::vector<Real>(DIM_1D, length));
199  create_line->options().set("nb_cells",std::vector<Uint>(DIM_1D, nb_segments));
200  Mesh& mesh = create_line->generate();
201 
202  lss_action->options().set("regions", std::vector<URI>(1, mesh.topology().uri()));
203  ic->get_child("Initialize")->options().set("regions", std::vector<URI>(1, mesh.topology().uri()));
204 
205  // Set boundary conditions
206  bc->add_constant_bc("xneg", "Temperature", 1.);
207  bc->add_constant_bc("xpos", "Temperature", 0.);
208 
209  // Configure timings
210  Time& time = model.create_time();
211  time.options().set("time_step", 1.);
212  time.options().set("end_time", 100.);
213 
214  // Run the solver
215  model.simulate();
216 
217  // Check result
218 // t = model.time().current_time();
219 // std::cout << "Checking solution at time " << t << std::endl;
220 // for_each_node
221 // (
222 // mesh.topology(),
223 // _check_close(-(_exp(Pe2 * coordinates[0])-1.)/(_exp(Pe2)-1.)+1, T, 1.)
224 // );
225 
226  std::cout << "Finished test" << std::endl;
227 
228 }
229 
231 
232 BOOST_AUTO_TEST_SUITE_END()
233 
234 
boost::proto::terminal< SFOp< ShapeFunctionOp > >::type const N
Time & create_time(const std::string &name="Time")
Create Time component.
static boost::proto::terminal< ZeroTag >::type _0
Placeholder for the zero matrix.
Definition: Terminals.hpp:209
void create_line(Mesh &mesh, const Real x_len, const Uint x_segments)
Create a 1D line mesh.
virtual mesh::Domain & create_domain(const std::string &name)
creates a domain in this model
Definition: Model.cpp:201
Safe pointer to an object. This is the supported method for referring to components.
Definition: Handle.hpp:39
virtual physics::PhysModel & create_physics(const std::string &builder)
Definition: Model.cpp:182
static boost::proto::terminal< ElementSystemMatrix< boost::mpl::int_< 0 > > >::type const _A
Some predefined element matrices (more can be user-defined, but you have to change the number in the ...
This header collects all the headers needed for the linear system solver, also including configure-ti...
Handle< common::Action > add_unsteady_solver(const std::string &builder_name)
Definition: Solver.cpp:138
static boost::proto::terminal< ElementSystemMatrix< boost::mpl::int_< 1 > > >::type const _T
Basic Classes for Solver applications used by CF.
Definition: Action.cpp:29
URI uri() const
Construct the full path.
Definition: Component.cpp:248
solver::actions::Proto::MakeSFOp< ComputeTauImpl >::reference_type apply
Definition: SUPG.hpp:249
static boost::proto::terminal< ElementQuadratureTag >::type element_quadrature
Use element_quadrature(expr1, expr2, ..., exprN) to evaluate a group of expressions.
std::vector< std::string > StringsT
tuple root
Definition: coolfluid.py:24
boost::shared_ptr< ElementsExpression< ExprT, ElementTypes > > elements_expression(ElementTypes, const ExprT &expr)
Definition: Expression.hpp:292
static boost::proto::terminal< double(*)(double) >::type const _exp
Definition: Terminals.hpp:243
std::vector< Uint > SizesT
Manage a collection of UFEM solvers.
Definition: Solver.hpp:31
static boost::proto::terminal< ElementRHS >::type const _x
Terminal for the element RHS vector ("b")
Real e()
Definition of the Unit charge [C].
Definition: Consts.hpp:30
void for_each_node(mesh::Region &root_region, const ExprT &expr)
Definition: NodeLooper.hpp:239
static boost::proto::terminal< std::ostream & >::type _cout
Wrap std::cout.
Definition: Terminals.hpp:219
Static functions for mathematical constants.
Definition: Consts.hpp:25
Refers to a value from the physical model.
Storage for time, and time steps for unsteady simulation.
Definition: Time.hpp:26
boost::proto::terminal< TransposeFunction >::type const transpose
boost::proto::terminal< SFOp< NablaOp > >::type const nabla
Basic Classes for Mesh applications used by COOLFluiD.
Eigen::Matrix< Real, Eigen::Dynamic, 1 > RealVector
Dynamic sized column vector.
Definition: MatrixTypes.hpp:25
tuple model
Global confifuration.
Handle< Component > get_child(const std::string &name)
Definition: Component.cpp:441
void init(int argc=0, char **args=0)
Definition: Comm.cpp:80
Top-level namespace for coolfluid.
Definition: Action.cpp:18
virtual void simulate()
Simulates this model.
BOOST_AUTO_TEST_CASE(InitMPI)
boost::proto::terminal< SFOp< CoordinatesOp > >::type const coordinates
Convenience type for a compute_tau operation, grouping the stored operator and its proto counterpart...
Definition: SUPG.hpp:238
static boost::proto::terminal< ExpressionGroupTag >::type group
Use group(expr1, expr2, ..., exprN) to evaluate a group of expressions.
common::Environment & environment() const
Definition: Core.cpp:168
void set_analytical_solution(Region &region, const std::string &field_name, const std::string &var_name)
Write the analytical solution.
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
boost::shared_ptr< ProtoAction > create_proto_action(const std::string &name, const boost::shared_ptr< Expression > &expression)
Create a new ProtoAction, immediatly setting the expression.
Region & topology() const
Definition: Mesh.hpp:51
static Core & instance()
Definition: Core.cpp:37
static boost::proto::terminal< void(*)(Real, Real, Real) >::type const _check_close
boost::shared_ptr< NodesExpression< ExprT, boost::mpl::range_c< Uint, 1, 4 > > > nodes_expression(const ExprT &expr)
Definition: Expression.hpp:308
OptionList & options()
Definition: Component.cpp:856
ComputeTau compute_tau
static Comm & instance()
Return a reference to the current PE.
Definition: Comm.cpp:44
Base class for defining CF components.
Definition: Component.hpp:82
void set(const std::string &pname, const boost::any &val)
Definition: OptionList.cpp:132
Handle< Component > create_component(const std::string &name, const std::string &builder)
Build a (sub)component of this component using the extended type_name of the component.
Definition: Component.cpp:568
Most basic kernel library.
Definition: Action.cpp:19
void check_close(const Real a, const Real b, const Real threshold)
Check close, for testing purposes.
Send comments to:
COOLFluiD Web Admin