KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
periodic_condition_utilities.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3 KratosFluidDynamicsApplication
4 A library based on:
5 Kratos
6 A General Purpose Software for Multi-Physics Finite Element Analysis
7 Version 1.0 (Released on march 05, 2007).
8 
9 Copyright 2007
10 Pooyan Dadvand, Riccardo Rossi
11 pooyan@cimne.upc.edu
12 rrossi@cimne.upc.edu
13 - CIMNE (International Center for Numerical Methods in Engineering),
14 Gran Capita' s/n, 08034 Barcelona, Spain
15 
16 
17 Permission is hereby granted, free of charge, to any person obtaining
18 a copy of this software and associated documentation files (the
19 "Software"), to deal in the Software without restriction, including
20 without limitation the rights to use, copy, modify, merge, publish,
21 distribute, sublicense and/or sell copies of the Software, and to
22 permit persons to whom the Software is furnished to do so, subject to
23 the following condition:
24 
25 Distribution of this code for any commercial purpose is permissible
26 ONLY BY DIRECT ARRANGEMENT WITH THE COPYRIGHT OWNERS.
27 
28 The above copyright notice and this permission notice shall be
29 included in all copies or substantial portions of the Software.
30 
31 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
34 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
35 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
36 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
37 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 
39 ==============================================================================
40 */
41 
42 /*
43  * File: periodic_condition_utilities.h
44  * Author: jcotela
45  *
46  * Created on September 22, 2011, 3:22 PM
47  */
48 
49 #ifndef KRATOS_PERIODIC_CONDITION_UTILITIES_H
50 #define KRATOS_PERIODIC_CONDITION_UTILITIES_H
51 
52 // System includes
53 #include <string>
54 #include <iostream>
55 #include <algorithm>
56 #include <cmath>
57 
58 // External includes
59 
60 
61 // Project includes
62 #include "includes/define.h"
63 #include "includes/node.h"
64 #include "includes/model_part.h"
65 #include "includes/properties.h"
66 #include "geometries/point_3d.h"
70 
71 #include "includes/variables.h"
72 
73 namespace Kratos
74 {
77 
80 
84 
85 
89 
93 
97 
99 
121 {
122 public:
125 
128 
129  typedef std::size_t IndexType;
130 
131  typedef std::size_t SizeType;
132 
133  //defintions for spatial search
134  typedef Node PointType;
135  typedef Node::Pointer PointTypePointer;
136  typedef std::vector<PointType::Pointer> PointVector;
137  typedef std::vector<PointType::Pointer>::iterator PointIterator;
138  typedef std::vector<double> DistanceVector;
139  typedef std::vector<double>::iterator DistanceIterator;
140 
141  // Bucket types
142 // typedef Bucket< 3, PointType, PointVector, PointTypePointer, PointIterator, DistanceIterator > BucketType;
144 // typedef BinsDynamic< 3, PointType, PointVector, PointTypePointer, PointIterator, DistanceIterator > DynamicBins;
145 
146  // DynamicBins
147 // typedef Tree< KDTreePartition<BucketType> > tree; //Kdtree;
148 // typedef Tree< OCTreePartition<BucketType> > tree; //Octree;
149  typedef Tree< StaticBins > tree; //Binstree;
150 // typedef Tree< KDTreePartition<StaticBins> > tree; //KdtreeBins;
151 
155 
157 
162  SizeType ThisDomainSize):
163  mrModelPart(ThisModelPart),
164  /*mDomainSize(ThisDomainSize),*/
165  mpSearchStrategy()
166  {}
167 
170  {}
171 
172 
176 
177 
178 
182 
184 
192  const double FlagValue)
193  {
194  KRATOS_TRY
195 
196  // Find candidate nodes for periodic conditions
197  mCandidateNodes = PointVector();
198 
199  for(ModelPart::NodesContainerType::ptr_iterator itNode = mrModelPart.Nodes().ptr_begin();
200  itNode != mrModelPart.Nodes().ptr_end(); itNode++)
201  {
202  if( (**itNode).FastGetSolutionStepValue(rFlagVar,0) == FlagValue)
203  mCandidateNodes.push_back(*itNode);
204  }
205 
206  if(mCandidateNodes.size() == 0) KRATOS_THROW_ERROR(std::invalid_argument,"No nodes found for periodic conditions generation","");
207 
208  // Initialize Search Strategy
209  SizeType BucketSize = 20;
210  mpSearchStrategy = Kratos::shared_ptr<tree>(new tree(mCandidateNodes.begin(),mCandidateNodes.end(),BucketSize));
211 
212 // std::cout << *mpSearchStrategy << std::endl;
213 
214  KRATOS_CATCH("")
215  }
216 
218 
228  template< class TReference >
229  void GenerateConditions(const TReference& MovementRef,
230  Properties::Pointer pProperties,
231  const std::string& rConditionLabel,
232  const double Tolerance = 1e-4)
233  {
234  KRATOS_TRY
235 
236  // check that the spatial seach structure was initialized
237  if(mpSearchStrategy == 0)
238  KRATOS_THROW_ERROR(std::logic_error,"PeriodicConditionUtilities error: GenerateConditions() called without a spatial search structure. Please call SetUpSearchStructure() first.","")
239 
240  // Get reference condition
241  const Condition& rCondition = KratosComponents<Condition>::Get(rConditionLabel);
242 
243  // Create a geometry for new conditions
244  ModelPart::ConditionsContainerType& rConditions = mrModelPart.Conditions();
245  Geometry< Node >::PointsArrayType ConditionNodes(2);
246 
247  // Get Id for new conditions
248  IndexType Id;
249  if( mrModelPart.ConditionsArray().size() == 0)
250  Id = 0;
251  else
252  Id = (mrModelPart.ConditionsEnd()-1)->Id();
253 
254  // Spatial search setup (for SearchNearestPoint)
255  double ResultDistance = 0.0;
256  PointTypePointer SecondNode;
257  PointType ImageNode;
258 
259  // Spatial search, create new conditions with results
260  unsigned int ConditionCount = 0;
261  for(PointVector::iterator itNode = mCandidateNodes.begin(); itNode != mCandidateNodes.end(); itNode++)
262  {
263  this->MoveNode(**itNode,ImageNode,MovementRef); // * for iterator + * for pointer
264 
265  SecondNode = mpSearchStrategy->SearchNearestPoint(ImageNode,ResultDistance);
266  if(ResultDistance < Tolerance)
267  {
268  ConditionNodes.GetContainer()[0] = *itNode;
269  ConditionNodes.GetContainer()[1] = SecondNode;
270  // Add condition to model part
271  Condition::Pointer pNewCondition = rCondition.Create(++Id,ConditionNodes,pProperties);
272  rConditions.push_back(pNewCondition);
273  ++ConditionCount;
274  }
275 
276  }
277 
278  std::cout << "Found " << ConditionCount << " node pairs in periodic boundary." << std::endl;
279 
280  KRATOS_CATCH("")
281  }
282 
283 
285 
297  void DefinePeriodicBoundary(Properties::Pointer pNewProperties,
298  const std::string& rConditionLabel,
299  const double TranslationX,
300  const double TranslationY,
301  const double TranslationZ = 0.0)
302  {
303  KRATOS_TRY
304 
305  // check that the spatial seach structure was initialized
306  if(mpSearchStrategy == 0)
307  KRATOS_THROW_ERROR(std::logic_error,"PeriodicConditionUtilities error: DefinePeriodicBoundary() called without a spatial search structure. Please call SetUpSearchStructure() first.","")
308 
309 
310  const double Tolerance = 1e-4; // Relative tolerance when searching for node pairs
311 
312  array_1d<double,3> Translation;
313  Translation[0] = TranslationX;
314  Translation[1] = TranslationY;
315  Translation[2] = TranslationZ;
316 
317  GenerateConditions(Translation,pNewProperties,rConditionLabel,Tolerance);
318 
319  KRATOS_CATCH("")
320  }
321 
322  void AddPeriodicVariable(Properties& rProperties,
323  Variable<double>& rVariable)
324  {
325  rProperties.GetValue(PERIODIC_VARIABLES).Add(rVariable);
326  }
327 
331 
332 
336 
337 
341 
343  virtual std::string Info() const
344  {
345  return "PeriodicConditionUtilities";
346  }
347 
349  virtual void PrintInfo(std::ostream& rOStream) const
350  {
351  rOStream << "PeriodicConditionUtilities";
352  }
353 
355  virtual void PrintData(std::ostream& rOStream) const
356  {}
357 
358 
362 
363 
365 
366 protected:
369 
370 
374 
375 
379 
380 
384 
385 
389 
390 
394 
395 
399 
400 
402 
403 private:
406 
407 
411 
413  ModelPart& mrModelPart;
414 
416  //SizeType mDomainSize;
417 
419  PointVector mCandidateNodes;
420 
422  tree::Pointer mpSearchStrategy;
423 
427 
428 
432 
433  void MoveNode( const Node& rInNode, Node& rOutNode, const array_1d<double,3>& rTranslation) const
434  {
435  rOutNode.Coordinates() = rInNode.Coordinates() + rTranslation;
436  }
437 
438  void MoveNode( const Node& rInNode, Node& rOutNode, const Node& rCentreNode )
439  {
440  rOutNode.Coordinates() = rInNode.Coordinates() + 2.0 * ( rCentreNode.Coordinates() - rInNode.Coordinates() );
441  }
442 
446 
447 
451 
452 
456 
457 
458 
460 
461 }; // Class PeriodicConditionUtilities
462 
464 
467 
468 
472 
473 
475 inline std::istream& operator >> (std::istream& rIStream,
477 {
478  return rIStream;
479 }
480 
482 inline std::ostream& operator << (std::ostream& rOStream,
483  const PeriodicConditionUtilities& rThis)
484 {
485  rThis.PrintInfo(rOStream);
486  rOStream << std::endl;
487  rThis.PrintData(rOStream);
488 
489  return rOStream;
490 }
492 
494 
495 } // namespace Kratos.
496 
497 #endif /* KRATOS_PERIODIC_CONDITION_UTILITIES_H */
498 
Definition: bins_static.h:35
Base class for all Conditions.
Definition: condition.h:59
virtual Pointer Create(IndexType NewId, NodesArrayType const &ThisNodes, PropertiesType::Pointer pProperties) const
It creates a new condition pointer.
Definition: condition.h:205
static const TComponentType & Get(const std::string &rName)
Retrieves a component with the specified name.
Definition: kratos_components.h:114
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
MeshType::ConditionsContainerType ConditionsContainerType
Condintions container. A vector set of Conditions with their Id's as key.
Definition: model_part.h:183
ConditionsContainerType::ContainerType & ConditionsArray(IndexType ThisIndex=0)
Definition: model_part.h:1401
ConditionsContainerType & Conditions(IndexType ThisIndex=0)
Definition: model_part.h:1381
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
ConditionIterator ConditionsEnd(IndexType ThisIndex=0)
Definition: model_part.h:1371
This class defines the node.
Definition: node.h:65
Auxiliary utilitiy to define periodic boundary conditions for flow problems.
Definition: periodic_condition_utilities.h:121
Bins< 3, PointType, PointVector, PointTypePointer, PointIterator, DistanceIterator > StaticBins
Definition: periodic_condition_utilities.h:143
Node PointType
Definition: periodic_condition_utilities.h:134
std::size_t SizeType
Definition: periodic_condition_utilities.h:131
void SetUpSearchStructure(Variable< double > const &rFlagVar, const double FlagValue)
Set a spatial search structure that will be used to find the periodic boundary node pairs.
Definition: periodic_condition_utilities.h:191
PeriodicConditionUtilities(ModelPart &ThisModelPart, SizeType ThisDomainSize)
Default constructor.
Definition: periodic_condition_utilities.h:161
std::vector< PointType::Pointer > PointVector
Definition: periodic_condition_utilities.h:136
Node::Pointer PointTypePointer
Definition: periodic_condition_utilities.h:135
void DefinePeriodicBoundary(Properties::Pointer pNewProperties, const std::string &rConditionLabel, const double TranslationX, const double TranslationY, const double TranslationZ=0.0)
Find node pairs to define periodic boundary conditions.
Definition: periodic_condition_utilities.h:297
virtual ~PeriodicConditionUtilities()
Destructor.
Definition: periodic_condition_utilities.h:169
Tree< StaticBins > tree
Definition: periodic_condition_utilities.h:149
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: periodic_condition_utilities.h:349
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: periodic_condition_utilities.h:355
KRATOS_CLASS_POINTER_DEFINITION(PeriodicConditionUtilities)
Pointer definition of PeriodicConditionUtilities.
std::vector< PointType::Pointer >::iterator PointIterator
Definition: periodic_condition_utilities.h:137
virtual std::string Info() const
Turn back information as a string.
Definition: periodic_condition_utilities.h:343
std::vector< double >::iterator DistanceIterator
Definition: periodic_condition_utilities.h:139
void GenerateConditions(const TReference &MovementRef, Properties::Pointer pProperties, const std::string &rConditionLabel, const double Tolerance=1e-4)
Generate a set of conditions linking each node in the periodic boundary to its image on the other sid...
Definition: periodic_condition_utilities.h:229
std::vector< double > DistanceVector
Definition: periodic_condition_utilities.h:138
std::size_t IndexType
Definition: periodic_condition_utilities.h:129
void AddPeriodicVariable(Properties &rProperties, Variable< double > &rVariable)
Definition: periodic_condition_utilities.h:322
CoordinatesArrayType const & Coordinates() const
Definition: point.h:215
PointerVector is a container like stl vector but using a vector to store pointers to its data.
Definition: pointer_vector.h:72
TContainerType & GetContainer()
Definition: pointer_vector.h:334
Properties encapsulates data shared by different Elements or Conditions. It can store any type of dat...
Definition: properties.h:69
TVariableType::Type & GetValue(const TVariableType &rVariable)
Definition: properties.h:228
A generic tree data structure for spatial partitioning.
Definition: tree.h:190
#define KRATOS_THROW_ERROR(ExceptionType, ErrorMessage, MoreInfo)
Definition: define.h:77
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::shared_ptr< T > shared_ptr
Definition: smart_pointers.h:27
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
std::vector< PointTypePointer > PointVector
Definition: internal_variables_interpolation_process.h:53
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
e
Definition: run_cpp_mpi_tests.py:31