# How to use the Spatial Containers

(Difference between revisions)
Jump to: navigation, search
 Revision as of 11:03, 5 May 2012 (view source)Nelson (Talk | contribs)← Older edit Latest revision as of 15:23, 17 July 2015 (view source)m (→Calling the Constructor) (9 intermediate revisions by 2 users not shown) Line 7: Line 7: comprising the problem. comprising the problem. − + This would be very CPU intensive, and would limit the overall performance of the application. To reduce CPU requirements of processing contact interaction, it is necessary to eliminate couples of discrete elements that are far from each other and are not in contact. A set procedures designed to detect bodies that are close to each other is usually called a contact detection − This would be very CPU intensive, and would limit the application that use and need to evaluate + − the contact forces ,comprising a very small number (a few thousand) of separates bodies. + − To reduce CPU requirements of processing contact interaction, it is necessary to eliminate couples of discrete elements that are far from each other and are not in contact. A set procedures designed to detect + − bodies that are close to each other is usually called a contact detection + algorithm, or sometimes a contact search algorithm. algorithm, or sometimes a contact search algorithm. − + With this purpose a set contact search algorithms based on spatial decomposition are provided in Kratos, so they can be used for any application. − With this purpose was created in Kratos Program  a contact search algorithm based on spatial decomposition, + − so it can be used for any application and is unique in that it was implemented in a generic way. + Therefore it requires a contact search algorithm having the following characteristics: Therefore it requires a contact search algorithm having the following characteristics: Line 23: Line 17: *RAM efficient *RAM efficient − The space decomposition implemented in Kratos are: + The space decomposition algorithms implemented in Kratos are: − * Trees : Kd-Trees, Octrees + * '''Trees''' : Kd-Trees, Octrees − * Bins + * '''Bins'''  : Dynamic Bins, Static Bins There are two  kind of searches algorithms in Kratos: There are two  kind of searches algorithms in Kratos: − * Points Searches: Used commonly for searching near points in a radius  and can be used for + * '''Points Searches''': Used commonly for searching near points in a radius  and can be used for searches contacts for spheric and circle geometries. Use the Kd_tree,Octree and bins for spatial decomposition. − searches contacts for spheric and circle geometries. Use the Kd_tree,Octree and bins for spatial + − decomposition. + − + − * Object Searches: The searches is based in the concept of objects. Is a generic class, thats mean, we can use either objects type even points. For now, use the bins for spatial decomposition. It could be interesting proof hybrid methods would be a + − and future investigation lines. + − + − == Spatial decomposition based in bins. Main methods  == + − + − Exist implemented in Kratos  two versions of  domain decomposition based in bins: + − dynamic and static bins. The difference between them is the dynamic allocation of memory. + − However both define the methods.  The most common methods used are presented and described below: + − + − * Bins based in points : + − *SearchNearestPoint : Calculates the nearest point of a point. + − *SearchInRadius    : Get the list of point in a certain radius. + − + − * Bins based in Objects : + − * SearchObjects      : Get the list of contacts of a certain objects even itself. + − * SearchObjectsInner : The same as above, just without return itself. + − * SearchConcts      : Get the list of all pair contacts. + − + − + − == Using the search algorithm based in objects == + + * '''Object Searches''': The searches is based in the concept of objects. Is a generic class, thats mean, we can use either objects type even points. For now, use the bins for spatial decomposition. == The User Configure File == == The User Configure File == − It say that is the main file created by the user, + The configuration file is an user-made file where the specific characteristics of the problem. − which defines the types, and operations containers + In simple terms, it can be considered as a small template class that the user needs to fill with − necessary to make the spatial decomposition and the search for contacts. + the types, operations and containers necessary for the strategy to search the contacts. − Is then the parameter that defines Bins. + − + This file is divided of four parts: − This consists of four parts: + *Definition of the types and their containers. *Definition of the types and their containers. Line 73: Line 44: Suppose that we have a set of discrete elements like a spheres. Suppose that we have a set of discrete elements like a spheres. − In our file called "discrete_configure_file.h" need to create the class, + The first step is to create a configuration file. The easiest way to do that is − types and methods before mentioned. Is useful to create this file using + copying the template found in "KRATOS_ROOT/kratos/templates/header_template.h". − the template of Kratos found in kratos/templates directory. Only we need to do is + In this example we are going to name the file *"example_configure_file.h"*. − changes the default names and directives by the name of our file. Normally this file is stored + − in the custom_utilities directory of the application. + + === Creating the Skeleton === + The first thing we have to do is changing the default names and directives of the file. + This normal implies changing two tokens that appear repeatedly across the file: − * Change the name of directives + KRATOS_FILENAME_H_INCLUDED − #if !defined(KRATOS_DISCRETE_PARTICLE_CONFIGURE_INCLUDED) + − #define  KRATOS_DISCRETE_PARTICLE_CONFIGURE_INCLUDED''' + − + − *Change the name of class + − ///@} + − ///@name Kratos Classes + − ///@{ + − template + − class DiscreteParticleConfigure{''' + + ClassName − * Typing the typenames, objects and container of the particle + that in our case would become: − ///@} + KRATOS_EXAMPLE_CONFIGURE_INCLUDED − ///@name Type Definitions + − ///@{ + ExampleConfigure − typedef  Point                        PointType; + − typedef  std::vector::iterator                  DistanceIteratorType; + − typedef  ModelPart::ElementsContainerType::ContainerType ContainerType; + − typedef  ContainerType::value_type                      PointerType; + − typedef  ContainerType::iterator                        IteratorType; + − typedef  ModelPart::ElementsContainerType::ContainerType ResultContainerType; + − typedef  ResultContainerType::value_type                ResultPointerType; + − typedef  ResultContainerType::iterator                  ResultIteratorType; + − typedef  ContactPair                        ContactPairType; + − typedef  std::vector                    ContainerContactType; + − typedef  ContainerContactType::iterator                  IteratorContactType; + − typedef  ContainerContactType::value_type                PointerContactType; + − typedef  std::vector::iterator              PointerTypeIterator; + + === Definition of the types and containers === − {{Note| + Once our file *"example_configure_file.h"* is prepared we need to fill it with the − Another types and container different to the model part  can be used. + types and operations. − }} + + The proper place to define the files is under the "Type Definitions" section and the list of + types that need to be defined is the following: − * Computing the bounding box + *'''PointType''': Type of the points that form our element. + *'''DistanceIteratorType''': Type of the iterator for the distances between contacts. + *'''ContainerType''': Type of the elements container. + *'''ResultContainerType''': Type of the elements container for the results. This is normally the same as '''ContainerType''' but it can vary in case our results are different elements. For example if we want to search all the tetrahedrons in a X distance from a set of triangles. + *'''PointerType''': Type of the pointer to our elements + *'''IteratorType''': Type of the iterator to our elements + *'''ResultPointerType''': Type of the pointer to our result elements + *'''ResultIteratorType''': Type of the iterator to our result elements + *'''ContactPairType''': Type of the contact pair + *'''ContainerContactType''': + *'''IteratorContactType''': + *'''PointerContactType''': + *'''PointerTypeIterator''': Type of the pointer iterator − '''static inline void CalculateBoundingBox'''(const PointerType& rObject, PointType& rLowPoint, PointType& rHighPoint) + In our example, a possible definition of these types can be: − { + − rHighPoint = rLowPoint = rObject->GetGeometry().GetPoint(0); + + ///@} + ///@name Type Definitions + ///@{ + + typedef  Point<Dimension, double>                        PointType; + typedef  std::vector<double>::iterator                  DistanceIteratorType; + typedef  ModelPart::ElementsContainerType::ContainerType ContainerType; + typedef ModelPart::ElementsContainerType::ContainerType ResultContainerType; + typedef  ContainerType::value_type                      PointerType; + typedef  ContainerType::iterator                        IteratorType; + typedef  ResultContainerType::value_type                ResultPointerType; + typedef ResultContainerType::iterator                  ResultIteratorType; + typedef  ContactPair<PointerType>                        ContactPairType; + typedef  std::vector<ContactPairType>                    ContainerContactType; + typedef  ContainerContactType::iterator                  IteratorContactType; + typedef  ContainerContactType::value_type                PointerContactType; + typedef  std::vector<PointerType>::iterator              PointerTypeIterator; {{Note| {{Note| − It is necessary the name  ''' static inline void CalculateBoundingBox ''' is well type, beacause + Please, note that these are only suggestions and any other types and containers different to ModelPart can be used. − the function is called by the bins. + }} }} + === Definition of the operations === + Along with the definition of the types of the previous section one also needs to define a set of basic operations that + will be used by the strategy. This operations involve mainly interaction between objects which have to be defined apart + as they are bound to the characteristics of the elements. + The list of functions to be defined are: + + * Computing the bounding box. * The Intersection function between objects. * The Intersection function between objects. + * Intersection cell and objects. + * Distance function between two objects. − static inline bool Intersection(const PointerType& rObj_1, const PointerType& rObj_2){ + Please note that the signature of all functions is '''mandatory''' as the strategy will only be able to use them if they appear exactly as − array_1d rObj_2_to_rObj_1 = rObj_1->GetGeometry().GetPoint(0) - rObj_2->GetGeometry().GetPoint(0); + described. − double distance_2 = inner_prod(rObj_2_to_rObj_1, rObj_2_to_rObj_1); + − const double& radius_1 = rObj_1->GetGeometry()(0)->GetSolutionStepValue(RADIUS); + − const double& radius_2 = rObj_2->GetGeometry()(0)->GetSolutionStepValue(RADIUS); + − double radius_sum      = radius_1 + radius_2; + − bool intersect        = (distance_2 - radius_sum * radius_sum) <= 0; + − return intersect; + − } + + ==== BoundingBox ==== − * Intersection cell and objects + *'''static inline void CalculateBoundingBox'''(const PointerType& rObject, PointType& rLowPoint, PointType& rHighPoint): + This method is used to calculate the boundingbox of the element (rObject). Once calculated the Low and High points of the + boundingbox need to be stored in '''rLowPoint''' and '''rHighPoint''' − static inline bool  IntersectionBox(const PointerType& rObject, const PointType& rLowPoint, const PointType& rHighPoint){ + *'''static inline void CalculateBoundingBox'''(const PointerType& rObject, PointType& rLowPoint, PointType& rHighPoint, const double& Radius): − array_1d center_of_particle = rObject->GetGeometry().GetPoint(0); + This method is used to calculate the boundingbox of the element (rObject) given an imposed radius. Once calculated the Low and High points of the − const double& radius = rObject->GetGeometry()(0)->GetSolutionStepValue(RADIUS); + boundingbox need to be stored in '''rLowPoint''' and '''rHighPoint''' − bool intersect = (rLowPoint[0] - radius <= center_of_particle[0] && rLowPoint[1] - radius <= center_of_particle[1] &&  rLowPoint[2] - radius <= center_of_particle[2] && + − rHighPoint[0] + radius >= center_of_particle[0] && rHighPoint[1] + radius >= center_of_particle[1] && rHighPoint[2] + radius >= center_of_particle[2]); + − return  intersect; + − } + + As an example, for our spheric elements: + − {{Note| + static inline void CalculateBoundingBox(const PointerType& rObject, PointType& rLowPoint, PointType& rHighPoint) { − Intersection cell and objects: If we do not have this function a simple return true is enougth. + − }} + rHighPoint = rLowPoint  = rObject->GetGeometry().GetPoint(0); + double radius = rObject->GetGeometry()(0)->GetSolutionStepValue(RADIUS); + + for(std::size_t i = 0; i < 3; i++){ + rLowPoint[i]  += -radius; + rHighPoint[i] += radius; + } + } − == Calling the Constructor == + static inline void CalculateBoundingBox(const PointerType& rObject, PointType& rLowPoint, PointType& rHighPoint, const double& Radius) { + + rHighPoint = rLowPoint  = rObject->GetGeometry().GetPoint(0); + + for(std::size_t i = 0; i < 3; i++){ + rLowPoint[i]  += -Radius; + rHighPoint[i] += Radius; + } + } − Once the configure file is done, we are able to use Contact Search Algorithm, + ==== Object-Object Intersection ==== − based in  a domain decomposition. In the fail where it will be called , we need to add + − in the directives the corresponding files, those are  the configure file and the + − bins. Maybe is required some redefinition of the typedef defined above in the configure file. + − static const std::size_t space_dim                = 2; + *'''static inline bool Intersection'''(const PointerType& rObj_1, const PointerType& rObj_2): − typedef DiscreteConfigure              Configure; + This method is used to test if two given elements (rObj_1, rObj_2) intersect with each other. If the intersection − typedef Configure::PointType                      PointType; + is not defined the default return value must be '''True'''. − typedef PointType::CoordinatesArrayType          CoordinatesArrayType; + − typedef Configure::ContainerType                  ContainerType; + − typedef Configure::PointerType                   PointerType; + − typedef Configure::IteratorType                  IteratorType; + − typedef Configure::ResultContainerType            ResultContainerType; + − typedef Configure::ResultPointerType              ResultPointerType; + − typedef Configure::ResultIteratorType            ResultIteratorType; + − typedef Configure::ContactPairType                ContactPairType; + − typedef Configure::ContainerContactType          ContainerContactType; + − typedef Configure::IteratorContactType            IteratorContactType; + − typedef Configure::PointerContactType            PointerContactType; + − typedef Configure::PointerTypeIterator            PointerTypeIterator; + − typedef ContainerContactType                      ContainerContactPair; + − typedef IteratorContactType                      IteratorContainerContactPair; + − typedef PointerContactType                        PointerContainerContactPair; + + *'''static inline bool Intersection'''(const PointerType& rObj_1, const PointerType& rObj_2, const double& Radius): + This method is used to test if elements rObj_2 intersect with rObj_1 in a radius (Radius) . If the intersection + is not defined the default return value must be '''True'''. − After that we call the constructor as follow: − * Using Dynamic Bins + As an example: − + − BinsObjectStatic Bins(it_begin, it_end); + static inline bool Intersection(const PointerType& rObj_1, const PointerType& rObj_2){ − const std::size_t MaxNumberOfResults  = 1000; + − std::size_t  NumberOfResults          = 0; + array_1d<double, 3> rObj_2_to_rObj_1 = rObj_1->GetGeometry().GetPoint(0) - rObj_2->GetGeometry().GetPoint(0); − ResultIteratorType  begin; + double distance_2 = inner_prod(rObj_2_to_rObj_1, rObj_2_to_rObj_1); + + const double& radius_1 = rObj_1->GetGeometry()(0)->GetSolutionStepValue(RADIUS); + const double& radius_2 = rObj_2->GetGeometry()(0)->GetSolutionStepValue(RADIUS); + + double radius_sum      = radius_1 + radius_2; + bool intersect        = (distance_2 - radius_sum * radius_sum) <= 0; + return intersect; + } + static inline bool Intersection(const PointerType& rObj_1, const PointerType& rObj_2, const double& Radius){ + + array_1d<double, 3> rObj_2_to_rObj_1 = rObj_1->GetGeometry().GetPoint(0) - rObj_2->GetGeometry().GetPoint(0); + double distance_2 = inner_prod(rObj_2_to_rObj_1, rObj_2_to_rObj_1); + + const double& radius_2 = rObj_2->GetGeometry()(0)->GetSolutionStepValue(RADIUS); + + double radius_sum      = Radius + radius_2; + bool intersect        = (distance_2 - radius_sum * radius_sum) <= 0; + return intersect; + } + ==== Object-Cell Intersection ==== − {{Note| + *'''static inline bool IntersectionBox(const PointerType& rObject, const PointType& rLowPoint, const PointType& rHighPoint): − Note that we can use the dynamic or static bins. The difference between them + This method is used to test if a given element (rObject) intersect with a cell defined by rLowPoint and rHighPoint. − is the dynamic allocation  of memory. Naturally, the static bins is faster + If the intersection is not defined the default return value must be '''True'''. − because the we know size of containers. + − }} + − *Lastly, we can get the list of pairs of contacts + *'''static inline bool IntersectionBox(const PointerType& rObject, const PointType& rLowPoint, const PointType& rHighPoint, const double& Radius): + This method is used to test if a given element (rObject) intersect with a cell defined by rLowPoint and rHighPoint in a radius (Radius). + If the intersection is not defined the default return value must be '''True'''. + + + As an example: + + + static inline bool IntersectionBox(const PointerType& rObject,  const PointType& rLowPoint, const PointType& rHighPoint){ + + array_1d<double, 3> center_of_particle = rObject->GetGeometry().GetPoint(0); + const double& radius = rObject->GetGeometry()(0)->GetSolutionStepValue(RADIUS); + + bool intersect = ( rLowPoint[0]  - radius <= center_of_particle[0] && + rLowPoint[1]  - radius <= center_of_particle[1] && + rLowPoint[2]  - radius <= center_of_particle[2] && + rHighPoint[0] + radius >= center_of_particle[0] && + rHighPoint[1] + radius >= center_of_particle[1] && + rHighPoint[2] + radius >= center_of_particle[2] + ); + + return  intersect; + } + + static inline bool IntersectionBox(const PointerType& rObject,  const PointType& rLowPoint, const PointType& rHighPoint, const double& Radius){ + + array_1d<double, 3> center_of_particle = rObject->GetGeometry().GetPoint(0); + + bool intersect = ( rLowPoint[0]  - Radius <= center_of_particle[0] && + rLowPoint[1]  - Radius <= center_of_particle[1] && + rLowPoint[2]  - Radius <= center_of_particle[2] && + rHighPoint[0] + Radius >= center_of_particle[0] && + rHighPoint[1] + Radius >= center_of_particle[1] && + rHighPoint[2] + Radius >= center_of_particle[2] + ); + + return  intersect; + } + + Please notice that if the object-cell function returns '''True''' the strategy will behave as a brute froce search. + + ==== Distance function ==== + + *'''static inline void Distance'''(const PointerType& rObj_1, const PointerType& rObj_2, double& distance): + This function returns the distance between to given elements (rObj_1, rObj_2). Result must be stored in "distance" + + For instance: + + + static inline void Distance(const PointerType& rObj_1, const PointerType& rObj_2, double& distance) { + + array_1d<double, 3> center_of_particle1 = rObj_1->GetGeometry().GetPoint(0); + array_1d<double, 3> center_of_particle2 = rObj_2->GetGeometry().GetPoint(0); + + distance = sqrt((center_of_particle1[0] - center_of_particle2[0]) * (center_of_particle1[0] - center_of_particle2[0]) + + (center_of_particle1[1] - center_of_particle2[1]) * (center_of_particle1[1] - center_of_particle2[1]) + + (center_of_particle1[2] - center_of_particle2[2]) * (center_of_particle1[2] - center_of_particle2[2]) ); + } + + == Calling the Constructor == + + Once the configure file is finished we are able to use Contact Search Algorithm. + + In the file where our search is going to be called we will need to include our configuration file along + with the desired strategy. The available strategies can be found in "KRATOS_ROOT/kratos/spatial_containers" and + basically are: + + * Bins_static + * Bins_static_objects + * Bins_dynamic + * Bins_dynamic_objects + + Typically dynamic strategies are slower but are not bound to a specific amount of memory. Static strategies are faster + but we need to anticipate the memory requirements. + + For our example we are going to select "bins_dinamyc_objects.h". Hence our file will have to include: + − *However, is faster to do a loop and get the bodies that are in contact with a particular element. + static const std::size_t space_dim = 2; − + − For(IteratorType it =it_begin; it!=it_end; it++) + typedef ExampleConfigure<space_dim>              Configure; − rBinsObjectDynamic.SearchObjectsInner(*it, Result); + + typedef Configure::PointType                      PointType; + typedef PointType::CoordinatesArrayType          CoordinatesArrayType; + typedef Configure::ContainerType                  ContainerType; + typedef Configure::PointerType                    PointerType; + typedef Configure::IteratorType                   IteratorType; + typedef Configure::ResultContainerType            ResultContainerType; + typedef Configure::ResultPointerType              ResultPointerType; + typedef Configure::ResultIteratorType            ResultIteratorType; + typedef Configure::ContactPairType                ContactPairType; + typedef Configure::ContainerContactType          ContainerContactType; + typedef Configure::IteratorContactType            IteratorContactType; + typedef Configure::PointerContactType            PointerContactType; + typedef Configure::PointerTypeIterator            PointerTypeIterator; + typedef Configure::ContainerContactType          ContainerContactPair; + typedef Configure::IteratorContactType            IteratorContainerContactPair; + typedef Configure::PointerContactType            PointerContainerContactPair; − where ''Result'' is the list of contact for a particular element. + After that we can create the search strategy itself. + In order to create our search strategy we will need to pass all the elements in our domain: + + IteratorType it_begin = mElements.begin(); + IteratorType it_end = mElements.end(); + + BinsObjectDynamic<Configure>  rBinsObjectDynamic(it_begin, it_end); − ==References== + Finally we call the search. For example if we want to search the neighbours of our elements in a Radius N: − * ﻿ Munjiza. Ante, ''The Combined Finite-Discrete Element Method'', John Wiley & Sons, Ltd. 2004 + + + ContainerType SearchElements; + double * Radius; + ResultContainerType Results; + double * ResultDistance; + + int MaxNumberOfElements = MAX_RES; + + ... + + rBins.SearchObjectsInRadiusExclusive(SearElementPointerToGeometricalObjecPointerTemporalVector,Radius[i],Results.begin(),ResultsDistances.begin(),MaxNumberOfElements);

## Using the Spatial Search Algorithm

Large-scale in Finite Element Methods, Discrete Element Method and Combined Finite-Discrete Element Method simulations involve contact of a large number of separate bodies. Processing contact interaction for all possible contacts would involve a total number of operations proportional to N^2, where N is the total number of separates bodies comprising the problem.

This would be very CPU intensive, and would limit the overall performance of the application. To reduce CPU requirements of processing contact interaction, it is necessary to eliminate couples of discrete elements that are far from each other and are not in contact. A set procedures designed to detect bodies that are close to each other is usually called a contact detection algorithm, or sometimes a contact search algorithm.

With this purpose a set contact search algorithms based on spatial decomposition are provided in Kratos, so they can be used for any application. Therefore it requires a contact search algorithm having the following characteristics:

• Robust,
• CPU efficient,
• RAM efficient

The space decomposition algorithms implemented in Kratos are:

• Trees : Kd-Trees, Octrees
• Bins  : Dynamic Bins, Static Bins

There are two kind of searches algorithms in Kratos:

• Points Searches: Used commonly for searching near points in a radius and can be used for searches contacts for spheric and circle geometries. Use the Kd_tree,Octree and bins for spatial decomposition.
• Object Searches: The searches is based in the concept of objects. Is a generic class, thats mean, we can use either objects type even points. For now, use the bins for spatial decomposition.

## The User Configure File

The configuration file is an user-made file where the specific characteristics of the problem. In simple terms, it can be considered as a small template class that the user needs to fill with the types, operations and containers necessary for the strategy to search the contacts.

This file is divided of four parts:

• Definition of the types and their containers.
• Function that calculates bounding box
• Function of intersection between objects
• Function of intercession between the object and cells.

## An Example Configure File

Suppose that we have a set of discrete elements like a spheres. The first step is to create a configuration file. The easiest way to do that is copying the template found in "KRATOS_ROOT/kratos/templates/header_template.h". In this example we are going to name the file *"example_configure_file.h"*.

### Creating the Skeleton

The first thing we have to do is changing the default names and directives of the file. This normal implies changing two tokens that appear repeatedly across the file:

``` KRATOS_FILENAME_H_INCLUDED

ClassName
```

that in our case would become:

``` KRATOS_EXAMPLE_CONFIGURE_INCLUDED

ExampleConfigure
```

### Definition of the types and containers

Once our file *"example_configure_file.h"* is prepared we need to fill it with the types and operations.

The proper place to define the files is under the "Type Definitions" section and the list of types that need to be defined is the following:

• PointType: Type of the points that form our element.
• DistanceIteratorType: Type of the iterator for the distances between contacts.
• ContainerType: Type of the elements container.
• ResultContainerType: Type of the elements container for the results. This is normally the same as ContainerType but it can vary in case our results are different elements. For example if we want to search all the tetrahedrons in a X distance from a set of triangles.
• PointerType: Type of the pointer to our elements
• IteratorType: Type of the iterator to our elements
• ResultPointerType: Type of the pointer to our result elements
• ResultIteratorType: Type of the iterator to our result elements
• ContactPairType: Type of the contact pair
• ContainerContactType:
• IteratorContactType:
• PointerContactType:
• PointerTypeIterator: Type of the pointer iterator

In our example, a possible definition of these types can be:

``` ///@}
///@name Type Definitions
///@{

typedef  Point<Dimension, double>                        PointType;
typedef  std::vector<double>::iterator                   DistanceIteratorType;
typedef  ModelPart::ElementsContainerType::ContainerType ContainerType;
typedef  ModelPart::ElementsContainerType::ContainerType ResultContainerType;
typedef  ContainerType::value_type                       PointerType;
typedef  ContainerType::iterator                         IteratorType;
typedef  ResultContainerType::value_type                 ResultPointerType;
typedef  ResultContainerType::iterator                   ResultIteratorType;
typedef  ContactPair<PointerType>                        ContactPairType;
typedef  std::vector<ContactPairType>                    ContainerContactType;
typedef  ContainerContactType::iterator                  IteratorContactType;
typedef  ContainerContactType::value_type                PointerContactType;
typedef  std::vector<PointerType>::iterator              PointerTypeIterator;
```
 Note: Please, note that these are only suggestions and any other types and containers different to ModelPart can be used.

### Definition of the operations

Along with the definition of the types of the previous section one also needs to define a set of basic operations that will be used by the strategy. This operations involve mainly interaction between objects which have to be defined apart as they are bound to the characteristics of the elements.

The list of functions to be defined are:

• Computing the bounding box.
• The Intersection function between objects.
• Intersection cell and objects.
• Distance function between two objects.

Please note that the signature of all functions is mandatory as the strategy will only be able to use them if they appear exactly as described.

#### BoundingBox

• static inline void CalculateBoundingBox(const PointerType& rObject, PointType& rLowPoint, PointType& rHighPoint):

This method is used to calculate the boundingbox of the element (rObject). Once calculated the Low and High points of the boundingbox need to be stored in rLowPoint and rHighPoint

• static inline void CalculateBoundingBox(const PointerType& rObject, PointType& rLowPoint, PointType& rHighPoint, const double& Radius):

This method is used to calculate the boundingbox of the element (rObject) given an imposed radius. Once calculated the Low and High points of the boundingbox need to be stored in rLowPoint and rHighPoint

As an example, for our spheric elements:

```static inline void CalculateBoundingBox(const PointerType& rObject, PointType& rLowPoint, PointType& rHighPoint) {

rHighPoint = rLowPoint  = rObject->GetGeometry().GetPoint(0);
double radius = rObject->GetGeometry()(0)->GetSolutionStepValue(RADIUS);

for(std::size_t i = 0; i < 3; i++){
rLowPoint[i]  += -radius;
rHighPoint[i] += radius;
}
}
```
```static inline void CalculateBoundingBox(const PointerType& rObject, PointType& rLowPoint, PointType& rHighPoint, const double& Radius) {

rHighPoint = rLowPoint  = rObject->GetGeometry().GetPoint(0);

for(std::size_t i = 0; i < 3; i++){
rLowPoint[i]  += -Radius;
rHighPoint[i] += Radius;
}
}
```

#### Object-Object Intersection

• static inline bool Intersection(const PointerType& rObj_1, const PointerType& rObj_2):

This method is used to test if two given elements (rObj_1, rObj_2) intersect with each other. If the intersection is not defined the default return value must be True.

• static inline bool Intersection(const PointerType& rObj_1, const PointerType& rObj_2, const double& Radius):

This method is used to test if elements rObj_2 intersect with rObj_1 in a radius (Radius) . If the intersection is not defined the default return value must be True.

As an example:

``` static inline bool Intersection(const PointerType& rObj_1, const PointerType& rObj_2){

array_1d<double, 3> rObj_2_to_rObj_1 = rObj_1->GetGeometry().GetPoint(0) - rObj_2->GetGeometry().GetPoint(0);
double distance_2 = inner_prod(rObj_2_to_rObj_1, rObj_2_to_rObj_1);

const double& radius_1 = rObj_1->GetGeometry()(0)->GetSolutionStepValue(RADIUS);
const double& radius_2 = rObj_2->GetGeometry()(0)->GetSolutionStepValue(RADIUS);

double radius_sum      = radius_1 + radius_2;
bool intersect         = (distance_2 - radius_sum * radius_sum) <= 0;
return intersect;
}
```
``` static inline bool Intersection(const PointerType& rObj_1, const PointerType& rObj_2, const double& Radius){

array_1d<double, 3> rObj_2_to_rObj_1 = rObj_1->GetGeometry().GetPoint(0) - rObj_2->GetGeometry().GetPoint(0);
double distance_2 = inner_prod(rObj_2_to_rObj_1, rObj_2_to_rObj_1);

const double& radius_2 = rObj_2->GetGeometry()(0)->GetSolutionStepValue(RADIUS);

double radius_sum      = Radius + radius_2;
bool intersect         = (distance_2 - radius_sum * radius_sum) <= 0;
return intersect;
}
```

#### Object-Cell Intersection

• static inline bool IntersectionBox(const PointerType& rObject, const PointType& rLowPoint, const PointType& rHighPoint):

This method is used to test if a given element (rObject) intersect with a cell defined by rLowPoint and rHighPoint. If the intersection is not defined the default return value must be True.

• static inline bool IntersectionBox(const PointerType& rObject, const PointType& rLowPoint, const PointType& rHighPoint, const double& Radius):

This method is used to test if a given element (rObject) intersect with a cell defined by rLowPoint and rHighPoint in a radius (Radius). If the intersection is not defined the default return value must be True.

As an example:

``` static inline bool IntersectionBox(const PointerType& rObject,  const PointType& rLowPoint, const PointType& rHighPoint){

array_1d<double, 3> center_of_particle = rObject->GetGeometry().GetPoint(0);
const double& radius = rObject->GetGeometry()(0)->GetSolutionStepValue(RADIUS);

bool intersect = ( rLowPoint[0]  - radius <= center_of_particle[0] &&
rLowPoint[1]  - radius <= center_of_particle[1] &&
rLowPoint[2]  - radius <= center_of_particle[2] &&
rHighPoint[0] + radius >= center_of_particle[0] &&
rHighPoint[1] + radius >= center_of_particle[1] &&
rHighPoint[2] + radius >= center_of_particle[2]
);

return  intersect;
}
```
``` static inline bool IntersectionBox(const PointerType& rObject,  const PointType& rLowPoint, const PointType& rHighPoint, const double& Radius){

array_1d<double, 3> center_of_particle = rObject->GetGeometry().GetPoint(0);

bool intersect = ( rLowPoint[0]  - Radius <= center_of_particle[0] &&
rLowPoint[1]  - Radius <= center_of_particle[1] &&
rLowPoint[2]  - Radius <= center_of_particle[2] &&
rHighPoint[0] + Radius >= center_of_particle[0] &&
rHighPoint[1] + Radius >= center_of_particle[1] &&
rHighPoint[2] + Radius >= center_of_particle[2]
);

return  intersect;
}
```

Please notice that if the object-cell function returns True the strategy will behave as a brute froce search.

#### Distance function

• static inline void Distance(const PointerType& rObj_1, const PointerType& rObj_2, double& distance):

This function returns the distance between to given elements (rObj_1, rObj_2). Result must be stored in "distance"

For instance:

``` static inline void Distance(const PointerType& rObj_1, const PointerType& rObj_2, double& distance) {

array_1d<double, 3> center_of_particle1 = rObj_1->GetGeometry().GetPoint(0);
array_1d<double, 3> center_of_particle2 = rObj_2->GetGeometry().GetPoint(0);

distance = sqrt((center_of_particle1[0] - center_of_particle2[0]) * (center_of_particle1[0] - center_of_particle2[0]) +
(center_of_particle1[1] - center_of_particle2[1]) * (center_of_particle1[1] - center_of_particle2[1]) +
(center_of_particle1[2] - center_of_particle2[2]) * (center_of_particle1[2] - center_of_particle2[2]) );
}
```

## Calling the Constructor

Once the configure file is finished we are able to use Contact Search Algorithm.

In the file where our search is going to be called we will need to include our configuration file along with the desired strategy. The available strategies can be found in "KRATOS_ROOT/kratos/spatial_containers" and basically are:

• Bins_static
• Bins_static_objects
• Bins_dynamic
• Bins_dynamic_objects

Typically dynamic strategies are slower but are not bound to a specific amount of memory. Static strategies are faster but we need to anticipate the memory requirements.

For our example we are going to select "bins_dinamyc_objects.h". Hence our file will have to include:

``` static const std::size_t space_dim = 2;

typedef ExampleConfigure<space_dim>               Configure;

typedef Configure::PointType                      PointType;
typedef PointType::CoordinatesArrayType           CoordinatesArrayType;
typedef Configure::ContainerType                  ContainerType;
typedef Configure::PointerType                    PointerType;
typedef Configure::IteratorType                   IteratorType;
typedef Configure::ResultContainerType            ResultContainerType;
typedef Configure::ResultPointerType              ResultPointerType;
typedef Configure::ResultIteratorType             ResultIteratorType;
typedef Configure::ContactPairType                ContactPairType;
typedef Configure::ContainerContactType           ContainerContactType;
typedef Configure::IteratorContactType            IteratorContactType;
typedef Configure::PointerContactType             PointerContactType;
typedef Configure::PointerTypeIterator            PointerTypeIterator;
typedef Configure::ContainerContactType           ContainerContactPair;
typedef Configure::IteratorContactType            IteratorContainerContactPair;
typedef Configure::PointerContactType             PointerContainerContactPair;
```

After that we can create the search strategy itself. In order to create our search strategy we will need to pass all the elements in our domain:

``` IteratorType it_begin = mElements.begin();
IteratorType it_end = mElements.end();

BinsObjectDynamic<Configure>  rBinsObjectDynamic(it_begin, it_end);
```

Finally we call the search. For example if we want to search the neighbours of our elements in a Radius N:

``` ContainerType SearchElements;
double * Radius;
ResultContainerType Results;
double * ResultDistance;

int MaxNumberOfElements = MAX_RES;

...

rBins.SearchObjectsInRadiusExclusive(SearElementPointerToGeometricalObjecPointerTemporalVector,Radius[i],Results.begin(),ResultsDistances.begin(),MaxNumberOfElements);
```