COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-proto-elements.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 
7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE "Test module for proto operators"
9 
10 #include <boost/assign.hpp>
11 #include <boost/foreach.hpp>
12 #include <boost/test/unit_test.hpp>
13 
14 #include "common/Core.hpp"
15 #include "common/Log.hpp"
16 
17 #include "math/MatrixTypes.hpp"
18 #include "math/Consts.hpp"
19 
20 #include "mesh/Domain.hpp"
21 #include "mesh/Mesh.hpp"
22 #include "mesh/Region.hpp"
23 #include "mesh/Elements.hpp"
24 #include "mesh/MeshWriter.hpp"
25 #include "mesh/ElementData.hpp"
26 #include "mesh/FieldManager.hpp"
27 #include "mesh/Dictionary.hpp"
28 
30 #include "mesh/ElementTypes.hpp"
31 #include "mesh/LagrangeP0/Quad.hpp"
32 #include <mesh/LagrangeP0/Line.hpp>
33 
35 
36 #include "physics/PhysModel.hpp"
37 
38 #include "solver/Model.hpp"
39 #include "solver/Solver.hpp"
40 #include "solver/Tags.hpp"
41 
48 
50 
51 using namespace cf3;
52 using namespace cf3::solver;
53 using namespace cf3::solver::actions;
54 using namespace cf3::solver::actions::Proto;
55 using namespace cf3::mesh;
56 using namespace cf3::common;
57 
58 using namespace cf3::math::Consts;
59 
60 using namespace boost::assign;
61 
63 
64 BOOST_AUTO_TEST_SUITE( ProtoOperatorsSuite )
65 
66 
68 // Test working with element-based fields
69 BOOST_AUTO_TEST_CASE( ProtoScalarElementField )
70 {
71  // Setup a model
73  physics::PhysModel& phys_model = model.create_physics("cf3.physics.DynamicModel");
74  Domain& dom = model.create_domain("Domain");
75  Solver& solver = model.create_solver("cf3.solver.SimpleSolver");
76 
77  Mesh& mesh = *dom.create_component<Mesh>("mesh");
78 
79  // Simple graded mesh (to get non-constant volume)
80  const Real length = 20.;
81  const Real height = 20.;
82  const Real ratio = 0.2;
83  const Uint x_segs = 10;
84  const Uint y_segs = 10;
85 
87 
88  *blocks.create_points(2, 4) << 0. << 0. << length << 0. << length << height << 0. << height;
89  *blocks.create_blocks(1) << 0 << 1 << 2 << 3;
90  *blocks.create_block_subdivisions() << x_segs << y_segs;
91  *blocks.create_block_gradings() << ratio << ratio << ratio << ratio;
92 
93  *blocks.create_patch("bottom", 1) << 0 << 1;
94  *blocks.create_patch("right", 1) << 1 << 2;
95  *blocks.create_patch("top", 1) << 2 << 3;
96  *blocks.create_patch("left", 1) << 3 << 0;
97 
98  blocks.create_mesh(mesh);
99 
100  mesh.check_sanity();
101 
102  // Declare a mesh variable
103  FieldVariable<0, ScalarField> V("CellVolume", "volumes");
104 
105  // Store the total error
106  Real total_error = 0;
107 
108  // Accepted element types
109  boost::mpl::vector2<mesh::LagrangeP0::Quad, mesh::LagrangeP1::Quad2D> allowed_elements;
110 
111  // Expression to compute volumes, assuming rectangles
112  boost::shared_ptr<Expression> volumes = elements_expression
113  (
114  allowed_elements,
115  V = (nodes[1][0] - nodes[0][0]) * (nodes[3][1] - nodes[0][1])
116  );
117 
118  // Register the variables
119  volumes->register_variables(phys_model);
120  // Add actions
121  solver
122  << create_proto_action("Volumes", volumes) // Setting the field
123  << create_proto_action("Output", elements_expression(allowed_elements, total_error += V - volume)); // error calculation
124 
125  // Create the fields
126  Dictionary& elems_P0 = mesh.create_discontinuous_space("elems_P0","cf3.mesh.LagrangeP0");
127  solver.field_manager().create_field("volumes", elems_P0);
128 
129  // Set the region of all children to the root region of the mesh
130  std::vector<URI> root_regions;
131  root_regions.push_back(mesh.topology().uri());
132  solver.configure_option_recursively(solver::Tags::regions(), root_regions);
133 
134  // Run
135  model.simulate();
136 
137  BOOST_CHECK_SMALL(total_error, 1e-12);
138 
139  // Write mesh
140  MeshWriter& writer = *model.domain().add_component(build_component_abstract_type<MeshWriter>("cf3.mesh.VTKXML.Writer", "writer")).handle<MeshWriter>();
141  std::vector<URI> fields;
142  fields.push_back(elems_P0.uri()/"volumes");
143  writer.options().set("fields",fields);
144  writer.options().set("mesh",mesh.handle<Mesh>());
145  writer.options().set("file",URI("utest-proto-elements-scalar_output.pvtu"));
146  writer.execute();
147 }
148 
149 // Test working with element-based fields
150 BOOST_AUTO_TEST_CASE( ProtoVectorElementField )
151 {
152  // Setup a model
154  physics::PhysModel& phys_model = model.create_physics("cf3.physics.DynamicModel");
155  Domain& dom = model.create_domain("Domain");
156  Solver& solver = model.create_solver("cf3.solver.SimpleSolver");
157 
158  Mesh& mesh = *dom.create_component<Mesh>("mesh");
159 
160  // Simple graded mesh (to get non-constant volume)
161  const Real length = 20.;
162  const Real height = 20.;
163  const Real ratio = 0.2;
164  const Uint x_segs = 10;
165  const Uint y_segs = 10;
166 
168 
169  *blocks.create_points(2, 4) << 0. << 0. << length << 0. << length << height << 0. << height;
170  *blocks.create_blocks(1) << 0 << 1 << 2 << 3;
171  *blocks.create_block_subdivisions() << x_segs << y_segs;
172  *blocks.create_block_gradings() << ratio << ratio << ratio << ratio;
173 
174  *blocks.create_patch("bottom", 1) << 0 << 1;
175  *blocks.create_patch("right", 1) << 1 << 2;
176  *blocks.create_patch("top", 1) << 2 << 3;
177  *blocks.create_patch("left", 1) << 3 << 0;
178 
179  blocks.create_mesh(mesh);
180 
181  mesh.check_sanity();
182 
183  // Declare a mesh variable
184  FieldVariable<0, VectorField> V("VectorVal", "vector_val");
185  FieldVariable<1, ScalarField> T("Scalar", "scalars");
186 
187  // Store the total error
188  Real total_error = 0;
189 
190  // Accepted element types
191  boost::mpl::vector4<mesh::LagrangeP0::Line, mesh::LagrangeP0::Quad, mesh::LagrangeP1::Quad2D, mesh::LagrangeP1::Line2D> allowed_elements;
192 
193  RealVector2 v; v[0] = 1.; v[1] = 2.;
194 
195  // Expression to compute vector_val, assuming rectangles
196  boost::shared_ptr<Expression> vector_val = elements_expression
197  (
198  allowed_elements,
199  V = v
200  );
201 
202  boost::shared_ptr<Expression> boundary_integral_expr = elements_expression(boost::mpl::vector2<mesh::LagrangeP0::Line, mesh::LagrangeP1::Line2D>(),
203  _cout << transpose(integral<1>(transpose(N(T))*V*normal)) << "\n");
204 
205  // Register the variables
206  vector_val->register_variables(phys_model);
207  boundary_integral_expr->register_variables(phys_model);
208  // Add actions
209  solver << create_proto_action("SetVector", vector_val);
210 
211  // Create the fields
212  Dictionary& elems_P0 = mesh.create_discontinuous_space("elems_P0","cf3.mesh.LagrangeP0");
213  solver.field_manager().create_field("vector_val", elems_P0);
214  solver.field_manager().create_field("scalars", mesh.geometry_fields());
215 
216  // Set the region of all children to the root region of the mesh
217  std::vector<URI> root_regions;
218  root_regions.push_back(mesh.topology().uri());
219  solver.configure_option_recursively(solver::Tags::regions(), root_regions);
220  boost::shared_ptr<ProtoAction> boundary_integral = create_proto_action("BoundaryIntegral", boundary_integral_expr);
221  solver.add_component(boundary_integral);
222  boundary_integral->options().set("regions", std::vector<URI>(1, mesh.topology().get_child("top")->uri()));
223 
224  // Run
225  model.simulate();
226 
227  BOOST_CHECK_SMALL(total_error, 1e-12);
228 
229  // Write mesh
230  MeshWriter& writer = *model.domain().add_component(build_component_abstract_type<MeshWriter>("cf3.mesh.VTKXML.Writer", "writer")).handle<MeshWriter>();
231  std::vector<URI> fields;
232  fields.push_back(elems_P0.uri()/"vector_val");
233  writer.options().set("fields",fields);
234  writer.options().set("mesh",mesh.handle<Mesh>());
235  writer.options().set("file",URI("utest-proto-elements-vector_output.pvtu"));
236  writer.execute();
237 }
238 
240 
241 BOOST_AUTO_TEST_SUITE_END()
242 
243 
boost::proto::terminal< SFOp< ShapeFunctionOp > >::type const N
boost::proto::terminal< SFOp< NormalOp > >::type const normal
virtual void execute()
execute the action
Definition: MeshWriter.cpp:151
bool check_sanity() const
Definition: Mesh.cpp:460
virtual mesh::Domain & create_domain(const std::string &name)
creates a domain in this model
Definition: Model.cpp:201
Dictionary & create_discontinuous_space(const std::string &space_name, const std::string &space_lib_name, const std::vector< Handle< Entities > > &entities)
Definition: Mesh.cpp:316
virtual physics::PhysModel & create_physics(const std::string &builder)
Definition: Model.cpp:182
void configure_option_recursively(const std::string &optname, const boost::any &val)
Definition: Component.cpp:1157
boost::proto::terminal< SFOp< VolumeOp > >::type const volume
Static terminals that can be used in proto expressions.
mesh::FieldManager & field_manager()
Access to the FieldManager, which is a static subcomponent of Solver.
Definition: Solver.cpp:115
Basic Classes for Solver applications used by CF.
Definition: Action.cpp:29
URI uri() const
Construct the full path.
Definition: Component.cpp:248
Handle< common::Table< Uint > > create_block_subdivisions()
Definition: BlockData.cpp:1315
boost::shared_ptr< ElementsExpression< ExprT, ElementTypes > > elements_expression(ElementTypes, const ExprT &expr)
Definition: Expression.hpp:292
Real e()
Definition of the Unit charge [C].
Definition: Consts.hpp:30
boost::proto::terminal< SFOp< NodesOp > >::type const nodes
static boost::proto::terminal< std::ostream & >::type _cout
Wrap std::cout.
Definition: Terminals.hpp:219
Handle< common::Table< Uint > > create_blocks(const Uint nb_blocks)
Create the table that holds the blocks.
Definition: BlockData.cpp:1299
Static functions for mathematical constants.
Definition: Consts.hpp:25
boost::proto::terminal< TransposeFunction >::type const transpose
virtual mesh::Domain & domain()
gets the domain from this model
Definition: Model.cpp:161
virtual Solver & create_solver(const std::string &builder)
Definition: Model.cpp:211
Basic Classes for Mesh applications used by COOLFluiD.
BOOST_AUTO_TEST_CASE(ProtoScalarElementField)
tuple model
Global confifuration.
Component & add_component(const boost::shared_ptr< Component > &subcomp)
Add the passed component as a subcomponent.
Definition: Component.cpp:289
Handle< Component > get_child(const std::string &name)
Definition: Component.cpp:441
Top-level namespace for coolfluid.
Definition: Action.cpp:18
Handle< common::Table< Real > > create_block_gradings()
Definition: BlockData.cpp:1329
Handle< common::Table< Uint > > create_patch(const std::string &name, const Uint nb_faces)
Definition: BlockData.cpp:1343
std::vector< URI > fields
common::Component & root() const
Gives the default root component.
Definition: Core.cpp:145
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
virtual void simulate()
Simulates this model.
Definition: Model.cpp:132
Eigen::Matrix< Real, 2, 1 > RealVector2
Fixed size 2x1 column vector.
Definition: MatrixTypes.hpp:40
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
Handle< Component > handle()
Get a handle to the component.
Definition: Component.hpp:179
void create_field(const std::string &tag, cf3::mesh::Dictionary &dict)
Create fields. Looks up the VariablesDescriptor with the given tag, and creates a field with the same...
OptionList & options()
Definition: Component.cpp:856
Dictionary & geometry_fields() const
Definition: Mesh.cpp:339
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
Handle< common::Table< Real > > create_points(const Uint dimensions, const Uint nb_points)
Create the table that holds the points for the blocks.
Definition: BlockData.cpp:1287
Send comments to:
COOLFluiD Web Admin