Salome HOME
Add test for .mesh file format
[tools/medcoupling.git] / src / INTERP_KERNEL / PolyhedronIntersectorP1P0.txx
1 // Copyright (C) 2007-2024  CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20 #ifndef __POLYHEDRONINTERSECTORP1P0_TXX__
21 #define __POLYHEDRONINTERSECTORP1P0_TXX__
22
23 #include "PolyhedronIntersectorP1P0.hxx"
24 #include "Intersector3DP1P0.txx"
25 #include "MeshUtils.hxx"
26
27 #include "SplitterTetra.txx"
28
29 namespace INTERP_KERNEL
30 {
31
32   /**
33    * Constructor creating object from target cell global number 
34    * The constructor first calculates the necessary nodes, 
35    * (depending on the splitting policy) and then splits the hexahedron into 
36    * tetrahedra, placing these in the internal vector _tetra.
37    * 
38    * @param targetMesh  mesh containing the target elements
39    * @param srcMesh     mesh containing the source elements
40    * @param policy      splitting policy to be used
41    *
42    * WARNING : in _split attribute, sourceMesh and targetMesh are switched in order to fit intersectCells feature.
43    */
44   template<class MyMeshType, class MyMatrix>
45   PolyhedronIntersectorP1P0<MyMeshType,MyMatrix>::PolyhedronIntersectorP1P0(const MyMeshType& targetMesh, const MyMeshType& srcMesh, SplittingPolicy policy):Intersector3DP1P0<MyMeshType,MyMatrix>(targetMesh,srcMesh),_split(srcMesh,targetMesh,policy)
46   {
47   }
48
49   /**
50    * Destructor.
51    * Liberates the SplitterTetra objects and potential sub-node points that have been allocated.
52    *
53    */
54   template<class MyMeshType, class MyMatrix>
55   PolyhedronIntersectorP1P0<MyMeshType,MyMatrix>::~PolyhedronIntersectorP1P0()
56   {
57     releaseArrays();
58   }
59     
60   template<class MyMeshType, class MyMatrix>
61   void PolyhedronIntersectorP1P0<MyMeshType,MyMatrix>::releaseArrays()
62   {
63     for(typename std::vector< SplitterTetra<MyMeshType>* >::iterator iter = _tetra.begin(); iter != _tetra.end(); ++iter)
64       delete *iter;
65     _split.releaseArrays();
66     _tetra.clear();
67   }
68
69   template<class RowType, class ConnType>
70   void AddContributionInRow(RowType& row, ConnType colId, double value)
71   {
72     if(value != 0.)
73     {
74       typename RowType::const_iterator iterRes=row.find(colId);
75       if(iterRes==row.end())
76         row.insert(std::make_pair(colId,value));
77       else
78       {
79         double val=(*iterRes).second+value;
80         row.erase(colId);
81         row.insert(std::make_pair(colId,val));
82       }
83     }
84   }
85
86   /**
87    * Calculates the volume of intersection of an element in the source mesh and the target element
88    * represented by the object.
89    * The calculation is performed by calling the corresponding method for
90    * each SplitterTetra object created by the splitting.
91    * 
92    * @param targetCell in C mode.
93    * @param srcCells in C mode.
94    *
95    * WARNING : for all methods on _split object source and target are switched !
96    */
97   template<class MyMeshType, class MyMatrix>
98   void PolyhedronIntersectorP1P0<MyMeshType,MyMatrix>::intersectCells(ConnType targetCell, const std::vector<ConnType>& srcCells, MyMatrix& res)
99   {
100     typename MyMatrix::value_type& resRow=res[targetCell];
101     INTERP_KERNEL::SplittingPolicy sp( _split.getSplittingPolicy() );
102     if( sp == GENERAL_48 )
103       THROW_IK_EXCEPTION("GENERAL_28 spliting is not supported for P1P0 interpolation");
104     SplitterTetra<MyMeshType>* subTetras[24];
105     for(typename std::vector<ConnType>::const_iterator iterCellS=srcCells.begin();iterCellS!=srcCells.end();iterCellS++)
106     {
107       releaseArrays();
108       ConnType nbOfNodesS=this->_src_mesh.getNumberOfNodesOfElement(OTT<ConnType,numPol>::indFC(*iterCellS));
109       _split.splitTargetCell(*iterCellS,nbOfNodesS,_tetra);
110       INTERP_KERNEL::NormalizedCellType srcType = this->_src_mesh.getTypeOfElement( OTT<ConnType,numPol>::indFC(*iterCellS) );
111       if( srcType == NORM_TETRA4 || (srcType == NORM_HEXA8 && sp != GENERAL_24 ))
112       {
113         for(typename std::vector<SplitterTetra<MyMeshType>*>::const_iterator iter = _tetra.cbegin(); iter != _tetra.cend(); ++iter)
114           {
115             (*iter)->splitIntoDualCells(subTetras);
116             double vol2 = 0.;
117             for(int i=0;i<24;i++)
118               {
119                 SplitterTetra<MyMeshType> *tmp=subTetras[i];
120                 double volume = tmp->intersectSourceCell(targetCell);
121                 vol2 += volume;
122                 ConnType sourceNode=tmp->getId(0);
123                 AddContributionInRow(resRow,OTT<ConnType,numPol>::indFC(sourceNode),volume);
124                 delete tmp;
125               }
126           }
127       }
128       else
129       {// for HEXA and GENERAL_24 no need to use subsplitting into dual mesh
130         for(typename std::vector<ConnType>::const_iterator iterCellS=srcCells.begin();iterCellS!=srcCells.end();iterCellS++)
131           {
132             releaseArrays();
133             ConnType nbOfNodesS=Intersector3D<MyMeshType,MyMatrix>::_src_mesh.getNumberOfNodesOfElement(OTT<ConnType,numPol>::indFC(*iterCellS));
134             _split.splitTargetCell2(*iterCellS,_tetra);
135             for(typename std::vector<SplitterTetra<MyMeshType>*>::const_iterator iter = _tetra.cbegin(); iter != _tetra.cend(); ++iter)
136               {
137                 double volume = std::abs( (*iter)->intersectSourceCell(targetCell) );
138                 // node #0 is for internal node node #1 is for the node at the middle of the face
139                 ConnType sourceNode0( (*iter)->getId(0) ), sourceNode1( (*iter)->getId(1) );
140                 AddContributionInRow(resRow,OTT<ConnType,numPol>::indFC(sourceNode0),volume/2.);
141                 AddContributionInRow(resRow,OTT<ConnType,numPol>::indFC(sourceNode1),volume/2.);
142               }
143           }
144       }
145     }
146   }
147 }
148
149 #endif