1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #include "OverlapInterpolationMatrix.hxx"
22 #include "ParaMESH.hxx"
23 #include "ParaFIELD.hxx"
24 #include "ProcessorGroup.hxx"
25 #include "TranslationRotationMatrix.hxx"
26 #include "Interpolation.hxx"
27 #include "Interpolation1D.txx"
28 #include "Interpolation2DCurve.hxx"
29 #include "Interpolation2D.txx"
30 #include "Interpolation3DSurf.hxx"
31 #include "Interpolation3D.txx"
32 #include "Interpolation3D2D.txx"
33 #include "Interpolation2D1D.txx"
34 #include "MEDCouplingUMesh.hxx"
35 #include "MEDCouplingNormalizedUnstructuredMesh.txx"
36 #include "InterpolationOptions.hxx"
37 #include "NormalizedUnstructuredMesh.hxx"
38 #include "ElementLocator.hxx"
39 #include "InterpKernelAutoPtr.hxx"
47 OverlapInterpolationMatrix::OverlapInterpolationMatrix(ParaFIELD *source_field,
48 ParaFIELD *target_field,
49 const ProcessorGroup& group,
50 const DECOptions& dec_options,
51 const INTERP_KERNEL::InterpolationOptions& i_opt,
52 const OverlapElementLocator & locator):
53 INTERP_KERNEL::InterpolationOptions(i_opt),
54 DECOptions(dec_options),
55 _source_field(source_field),
56 _target_field(target_field),
57 _source_support(source_field->getSupport()->getCellMesh()),
58 _target_support(target_field->getSupport()->getCellMesh()),
59 _mapping(group, locator),
64 void OverlapInterpolationMatrix::keepTracksOfSourceIds(int procId, DataArrayInt *ids)
66 _mapping.keepTracksOfSourceIds(procId,ids);
69 void OverlapInterpolationMatrix::keepTracksOfTargetIds(int procId, DataArrayInt *ids)
71 _mapping.keepTracksOfTargetIds(procId,ids);
74 OverlapInterpolationMatrix::~OverlapInterpolationMatrix()
78 // TODO? Merge with MEDCouplingRemapper::prepareInterpKernelOnlyUU() ?
80 * @param srcIds is null if the source mesh is on the local proc
81 * @param trgIds is null if the source mesh is on the local proc
83 * One of the 2 is necessarily null (the two can be null together)
85 void OverlapInterpolationMatrix::addContribution(const MEDCouplingPointSet *src, const DataArrayInt *srcIds, const std::string& srcMeth, int srcProcId,
86 const MEDCouplingPointSet *trg, const DataArrayInt *trgIds, const std::string& trgMeth, int trgProcId)
88 std::string interpMethod(srcMeth);
89 interpMethod+=trgMeth;
90 //creating the interpolator structure
91 vector<SparseDoubleVec > sparse_matrix_part;
93 //computation of the intersection volumes between source and target elements
94 const MEDCouplingUMesh *trgC=dynamic_cast<const MEDCouplingUMesh *>(trg);
95 const MEDCouplingUMesh *srcC=dynamic_cast<const MEDCouplingUMesh *>(src);
96 if ( src->getMeshDimension() == -1 )
98 if(trgC->getMeshDimension()==2 && trgC->getSpaceDimension()==2)
100 MEDCouplingNormalizedUnstructuredMesh<2,2> target_mesh_wrapper(trgC);
101 INTERP_KERNEL::Interpolation2D interpolation(*this);
102 colSize=interpolation.fromIntegralUniform(target_mesh_wrapper,sparse_matrix_part,trgMeth);
104 else if(trgC->getMeshDimension()==3 && trgC->getSpaceDimension()==3)
106 MEDCouplingNormalizedUnstructuredMesh<3,3> target_mesh_wrapper(trgC);
107 INTERP_KERNEL::Interpolation3D interpolation(*this);
108 colSize=interpolation.fromIntegralUniform(target_mesh_wrapper,sparse_matrix_part,trgMeth);
110 else if(trgC->getMeshDimension()==2 && trgC->getSpaceDimension()==3)
112 MEDCouplingNormalizedUnstructuredMesh<3,2> target_mesh_wrapper(trgC);
113 INTERP_KERNEL::Interpolation3DSurf interpolation(*this);
114 colSize=interpolation.fromIntegralUniform(target_mesh_wrapper,sparse_matrix_part,trgMeth);
117 throw INTERP_KERNEL::Exception("No para interpolation available for the given mesh and space dimension of source mesh to -1D targetMesh");
119 else if ( trg->getMeshDimension() == -1 )
121 if(srcC->getMeshDimension()==2 && srcC->getSpaceDimension()==2)
123 MEDCouplingNormalizedUnstructuredMesh<2,2> local_mesh_wrapper(srcC);
124 INTERP_KERNEL::Interpolation2D interpolation(*this);
125 colSize=interpolation.toIntegralUniform(local_mesh_wrapper,sparse_matrix_part,srcMeth);
127 else if(srcC->getMeshDimension()==3 && srcC->getSpaceDimension()==3)
129 MEDCouplingNormalizedUnstructuredMesh<3,3> local_mesh_wrapper(srcC);
130 INTERP_KERNEL::Interpolation3D interpolation(*this);
131 colSize=interpolation.toIntegralUniform(local_mesh_wrapper,sparse_matrix_part,srcMeth);
133 else if(srcC->getMeshDimension()==2 && srcC->getSpaceDimension()==3)
135 MEDCouplingNormalizedUnstructuredMesh<3,2> local_mesh_wrapper(srcC);
136 INTERP_KERNEL::Interpolation3DSurf interpolation(*this);
137 colSize=interpolation.toIntegralUniform(local_mesh_wrapper,sparse_matrix_part,srcMeth);
140 throw INTERP_KERNEL::Exception("No para interpolation available for the given mesh and space dimension of distant mesh to -1D sourceMesh");
142 else if ( src->getMeshDimension() == 2 && trg->getMeshDimension() == 3
143 && trg->getSpaceDimension() == 3 && src->getSpaceDimension() == 3 )
145 MEDCouplingNormalizedUnstructuredMesh<3,3> target_wrapper(trgC);
146 MEDCouplingNormalizedUnstructuredMesh<3,3> source_wrapper(srcC);
148 INTERP_KERNEL::Interpolation3D2D interpolator (*this);
149 colSize=interpolator.interpolateMeshes(source_wrapper,target_wrapper,sparse_matrix_part,interpMethod);
151 else if ( src->getMeshDimension() == 3 && trg->getMeshDimension() == 2
152 && trg->getSpaceDimension() == 3 && src->getSpaceDimension() == 3 )
154 MEDCouplingNormalizedUnstructuredMesh<3,3> target_wrapper(trgC);
155 MEDCouplingNormalizedUnstructuredMesh<3,3> source_wrapper(srcC);
157 INTERP_KERNEL::Interpolation3D2D interpolator (*this);
158 vector<SparseDoubleVec > matrixTranspose;
159 colSize=interpolator.interpolateMeshes(target_wrapper,source_wrapper,sparse_matrix_part,interpMethod);//not a bug target in source.
160 TransposeMatrix(matrixTranspose,colSize,sparse_matrix_part);
161 colSize=matrixTranspose.size();
163 else if ( src->getMeshDimension() == 1 && trg->getMeshDimension() == 2
164 && trg->getSpaceDimension() == 2 && src->getSpaceDimension() == 2 )
166 MEDCouplingNormalizedUnstructuredMesh<2,2> target_wrapper(trgC);
167 MEDCouplingNormalizedUnstructuredMesh<2,2> source_wrapper(srcC);
169 INTERP_KERNEL::Interpolation2D1D interpolator (*this);
170 colSize=interpolator.interpolateMeshes(source_wrapper,target_wrapper,sparse_matrix_part,interpMethod);
172 else if ( src->getMeshDimension() == 2 && trg->getMeshDimension() == 1
173 && trg->getSpaceDimension() == 2 && src->getSpaceDimension() == 2 )
175 MEDCouplingNormalizedUnstructuredMesh<2,2> target_wrapper(trgC);
176 MEDCouplingNormalizedUnstructuredMesh<2,2> source_wrapper(srcC);
178 INTERP_KERNEL::Interpolation2D1D interpolator (*this);
179 vector<SparseDoubleVec > matrixTranspose;
180 colSize=interpolator.interpolateMeshes(target_wrapper,source_wrapper,matrixTranspose,interpMethod);//not a bug target in source.
181 TransposeMatrix(matrixTranspose,colSize,sparse_matrix_part);
182 colSize=matrixTranspose.size();
184 else if (trg->getMeshDimension() != _source_support->getMeshDimension())
186 throw INTERP_KERNEL::Exception("local and distant meshes do not have the same space and mesh dimensions");
188 else if( src->getMeshDimension() == 1
189 && src->getSpaceDimension() == 1 )
191 MEDCouplingNormalizedUnstructuredMesh<1,1> target_wrapper(trgC);
192 MEDCouplingNormalizedUnstructuredMesh<1,1> source_wrapper(srcC);
194 INTERP_KERNEL::Interpolation1D interpolation(*this);
195 colSize=interpolation.interpolateMeshes(source_wrapper,target_wrapper,sparse_matrix_part,interpMethod);
197 else if( trg->getMeshDimension() == 1
198 && trg->getSpaceDimension() == 2 )
200 MEDCouplingNormalizedUnstructuredMesh<2,1> target_wrapper(trgC);
201 MEDCouplingNormalizedUnstructuredMesh<2,1> source_wrapper(srcC);
203 INTERP_KERNEL::Interpolation2DCurve interpolation(*this);
204 colSize=interpolation.interpolateMeshes(source_wrapper,target_wrapper,sparse_matrix_part,interpMethod);
206 else if ( trg->getMeshDimension() == 2
207 && trg->getSpaceDimension() == 3 )
209 MEDCouplingNormalizedUnstructuredMesh<3,2> target_wrapper(trgC);
210 MEDCouplingNormalizedUnstructuredMesh<3,2> source_wrapper(srcC);
212 INTERP_KERNEL::Interpolation3DSurf interpolator (*this);
213 colSize=interpolator.interpolateMeshes(source_wrapper,target_wrapper,sparse_matrix_part,interpMethod);
215 else if ( trg->getMeshDimension() == 2
216 && trg->getSpaceDimension() == 2)
218 MEDCouplingNormalizedUnstructuredMesh<2,2> target_wrapper(trgC);
219 MEDCouplingNormalizedUnstructuredMesh<2,2> source_wrapper(srcC);
221 INTERP_KERNEL::Interpolation2D interpolator (*this);
222 colSize=interpolator.interpolateMeshes(source_wrapper,target_wrapper,sparse_matrix_part,interpMethod);
224 else if ( trg->getMeshDimension() == 3
225 && trg->getSpaceDimension() == 3 )
227 MEDCouplingNormalizedUnstructuredMesh<3,3> target_wrapper(trgC);
228 MEDCouplingNormalizedUnstructuredMesh<3,3> source_wrapper(srcC);
230 INTERP_KERNEL::Interpolation3D interpolator (*this);
231 colSize=interpolator.interpolateMeshes(source_wrapper,target_wrapper,sparse_matrix_part,interpMethod);
235 throw INTERP_KERNEL::Exception("No interpolator exists for these mesh and space dimensions!");
237 /* Fill distributed matrix:
238 In sparse_matrix_part rows refer to target, and columns (=first param of map in SparseDoubleVec)
241 _mapping.addContributionST(sparse_matrix_part,srcIds,srcProcId,trgIds,trgProcId);
246 * 'procsToSendField' gives the list of procs field data has to be sent to.
247 * See OverlapElementLocator::computeBoundingBoxesAndTodoList()
249 void OverlapInterpolationMatrix::prepare(const std::vector< int >& procsToSendField)
252 _mapping.prepare(procsToSendField,_target_field->getField()->getNumberOfTuplesExpected());
254 _mapping.prepare(procsToSendField,0);
257 void OverlapInterpolationMatrix::computeDeno()
259 if(_target_field->getField()->getNature()==ConservativeVolumic)
260 _mapping.computeDenoConservativeVolumic(_target_field->getField()->getNumberOfTuplesExpected());
262 throw INTERP_KERNEL::Exception("Policy Not implemented yet : only ConservativeVolumic defined !");
265 void OverlapInterpolationMatrix::multiply(double default_val)
267 _mapping.multiply(_source_field->getField(),_target_field->getField(), default_val);
270 void OverlapInterpolationMatrix::transposeMultiply()
272 _mapping.transposeMultiply(_target_field->getField(),_source_field->getField());
275 // bool OverlapInterpolationMatrix::isSurfaceComputationNeeded(const std::string& method) const
277 // return method=="P0";
280 void OverlapInterpolationMatrix::TransposeMatrix(const std::vector<SparseDoubleVec >& matIn,
281 int nbColsMatIn, std::vector<SparseDoubleVec >& matOut)
283 matOut.resize(nbColsMatIn);
285 for(std::vector<SparseDoubleVec >::const_iterator iter1=matIn.begin();iter1!=matIn.end();iter1++,id++)
286 for(SparseDoubleVec::const_iterator iter2=(*iter1).begin();iter2!=(*iter1).end();iter2++)
287 matOut[(*iter2).first][id]=(*iter2).second;