Salome HOME
Stash 5.
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingCartesianAMRMesh.hxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D
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
20
21 #ifndef __MEDCOUPLINGCARTESIANAMRMESH_HXX__
22 #define __MEDCOUPLINGCARTESIANAMRMESH_HXX__
23
24 #include "MEDCoupling.hxx"
25 #include "MEDCouplingTimeLabel.hxx"
26 #include "MEDCouplingRefCountObject.hxx"
27 #include "MEDCouplingAutoRefCountObjectPtr.hxx"
28
29 #include "BoxSplittingOptions.hxx"
30 #include "InterpKernelException.hxx"
31
32 namespace ParaMEDMEM
33 {
34   class MEDCouplingIMesh;
35   class MEDCouplingUMesh;
36   class DataArrayInt;
37   class DataArrayByte;
38   class DataArrayDouble;
39   class MEDCoupling1SGTUMesh;
40   class MEDCouplingFieldDouble;
41   class MEDCouplingCartesianAMRMesh;
42   class MEDCouplingCartesianAMRMeshGen;
43
44   /// @cond INTERNAL
45
46   /*!
47    * This class does not inherit from TimeLabel so only const method should exist.
48    */
49   class MEDCouplingCartesianAMRPatchGen : public RefCountObject
50   {
51   public:
52     MEDCOUPLING_EXPORT int getNumberOfCellsRecursiveWithOverlap() const;
53     MEDCOUPLING_EXPORT int getNumberOfCellsRecursiveWithoutOverlap() const;
54     MEDCOUPLING_EXPORT int getMaxNumberOfLevelsRelativeToThis() const;
55     MEDCOUPLING_EXPORT const MEDCouplingCartesianAMRMeshGen *getMesh() const { return _mesh; }
56   protected:
57     MEDCouplingCartesianAMRPatchGen(MEDCouplingCartesianAMRMeshGen *mesh);
58     const MEDCouplingCartesianAMRMeshGen *getMeshSafe() const;
59     MEDCouplingCartesianAMRMeshGen *getMeshSafe();
60   private:
61     std::vector<const BigMemoryObject *> getDirectChildren() const;
62   protected:
63     MEDCouplingAutoRefCountObjectPtr<MEDCouplingCartesianAMRMeshGen> _mesh;
64   };
65
66   /*!
67    * This class does not inherit from TimeLabel so only const method should exist.
68    */
69   class MEDCouplingCartesianAMRPatch : public MEDCouplingCartesianAMRPatchGen
70   {
71   public:
72     MEDCouplingCartesianAMRPatch(MEDCouplingCartesianAMRMeshGen *mesh, const std::vector< std::pair<int,int> >& bottomLeftTopRight);
73     // direct forward to _mesh
74     MEDCOUPLING_EXPORT void addPatch(const std::vector< std::pair<int,int> >& bottomLeftTopRight, const std::vector<int>& factors);
75     // end of direct forward to _mesh
76     MEDCOUPLING_EXPORT int getNumberOfOverlapedCellsForFather() const;
77     MEDCOUPLING_EXPORT bool isInMyNeighborhood(const MEDCouplingCartesianAMRPatch *other, int ghostLev) const;
78     MEDCOUPLING_EXPORT bool isInMyNeighborhoodExt(const MEDCouplingCartesianAMRPatch *other, int ghostLev) const;
79     MEDCOUPLING_EXPORT bool isInMyNeighborhoodDiffLev(const MEDCouplingCartesianAMRPatch *other, int ghostLev) const;
80     // basic set/get
81     MEDCOUPLING_EXPORT const std::vector< std::pair<int,int> >& getBLTRRange() const { return _bl_tr; }
82     MEDCOUPLING_EXPORT static bool IsInMyNeighborhood(int ghostLev, const std::vector< std::pair<int,int> >& p1, const std::vector< std::pair<int,int> >& p2);
83     //
84     static std::vector< std::vector< std::pair<const MEDCouplingCartesianAMRPatch *,const MEDCouplingCartesianAMRPatch *> > > FindNeighborsOfSubPatchesOfSameLev(int ghostLev, const MEDCouplingCartesianAMRPatch *p1, const MEDCouplingCartesianAMRPatch *p2);
85     static void FindNeighborsOfSubPatchesOf(int ghostLev, const MEDCouplingCartesianAMRPatch *p1, const MEDCouplingCartesianAMRPatch *p2, std::vector< std::pair<const MEDCouplingCartesianAMRPatch *,const MEDCouplingCartesianAMRPatch *> >& ret);
86     static void UpdateNeighborsOfOneWithTwo(int ghostLev, const std::vector<int>& factors, const MEDCouplingCartesianAMRPatch *p1, const MEDCouplingCartesianAMRPatch *p2, DataArrayDouble *dataOnP1, const DataArrayDouble *dataOnP2);
87     static void UpdateNeighborsOfOneWithTwoInternal(int ghostLev, const std::vector<int>& factors, const std::vector< std::pair<int,int> >&p1 ,const std::vector< std::pair<int,int> >&p2, DataArrayDouble *dataOnP1, const DataArrayDouble *dataOnP2);
88     static void UpdateNeighborsOfOneWithTwoExt(int ghostLev, const MEDCouplingCartesianAMRPatch *p1, const MEDCouplingCartesianAMRPatch *p2, DataArrayDouble *dataOnP1, const DataArrayDouble *dataOnP2);
89     static void UpdateNeighborsOfOneWithTwoMixedLev(int ghostLev, const MEDCouplingCartesianAMRPatch *p1, const MEDCouplingCartesianAMRPatch *p2, DataArrayDouble *dataOnP1, const DataArrayDouble *dataOnP2);
90   private:
91     std::size_t getHeapMemorySizeWithoutChildren() const;
92     static const MEDCouplingCartesianAMRMeshGen *FindCommonAncestor(const MEDCouplingCartesianAMRPatch *p1, const MEDCouplingCartesianAMRPatch *p2, int& lev);
93     static std::vector<int> ComputeOffsetFromTwoToOne(const MEDCouplingCartesianAMRMeshGen *comAncestor, int lev, const MEDCouplingCartesianAMRPatch *p1, const MEDCouplingCartesianAMRPatch *p2);
94   public:
95     static void ApplyFactorsOnCompactFrmt(std::vector< std::pair<int,int> >& partBeforeFact, const std::vector<int>& factors);
96     static void ApplyGhostOnCompactFrmt(std::vector< std::pair<int,int> >& partBeforeFact, int ghostSize);
97     static void ApplyAllGhostOnCompactFrmt(std::vector< std::pair<int,int> >& partBeforeFact, int ghostSize);
98   private:
99     //! bottom left/top right cell range relative to \a _father
100     std::vector< std::pair<int,int> > _bl_tr;
101   };
102
103   /*!
104    * This class does not inherit from TimeLabel so only const method should exist.
105    */
106   class MEDCouplingCartesianAMRPatchGF : public MEDCouplingCartesianAMRPatchGen
107   {
108   public:
109     MEDCouplingCartesianAMRPatchGF(MEDCouplingCartesianAMRMesh *mesh);
110   private:
111     std::size_t getHeapMemorySizeWithoutChildren() const;
112   };
113
114   class MEDCouplingDataForGodFather : public RefCountObject
115   {
116     friend class MEDCouplingCartesianAMRMesh;
117   public:
118     MEDCOUPLING_EXPORT virtual void synchronizeFineToCoarse() = 0;
119     MEDCOUPLING_EXPORT virtual void synchronizeCoarseToFine() = 0;
120     MEDCOUPLING_EXPORT virtual void synchronizeCoarseToFineOnlyInGhostZone() = 0;
121     MEDCOUPLING_EXPORT virtual void synchronizeFineEachOtherInGhostZone() = 0;
122     MEDCOUPLING_EXPORT virtual void alloc() = 0;
123     MEDCOUPLING_EXPORT virtual void dealloc() = 0;
124   protected:
125     MEDCouplingDataForGodFather(MEDCouplingCartesianAMRMesh *gf);
126     void checkGodFatherFrozen() const;
127   protected:
128     virtual bool changeGodFather(MEDCouplingCartesianAMRMesh *gf);
129   protected:
130     MEDCouplingCartesianAMRMesh *_gf;
131     TimeLabelConstOverseer _tlc;
132   };
133   /// @endcond
134
135   /*!
136    * This class is the base class dedicated to AMR using Adaptative Hierarchical Overlapped image Grid.
137    * This class does \b NOT inherit from MEDCouplingMesh because this class overlaps image grid structured meshes to perform adaptative mesh refinement.
138    * But this class aggregates MEDCouplingMesh instances !
139    */
140   class MEDCouplingCartesianAMRMeshGen : public RefCountObject, public TimeLabel
141   {
142   public:
143     MEDCOUPLING_EXPORT int getSpaceDimension() const;
144     MEDCOUPLING_EXPORT const std::vector<int>& getFactors() const { return _factors; }
145     MEDCOUPLING_EXPORT void setFactors(const std::vector<int>& newFactors);
146     MEDCOUPLING_EXPORT int getMaxNumberOfLevelsRelativeToThis() const;
147     MEDCOUPLING_EXPORT int getNumberOfCellsAtCurrentLevel() const;
148     MEDCOUPLING_EXPORT int getNumberOfCellsAtCurrentLevelGhost(int ghostLev) const;
149     MEDCOUPLING_EXPORT int getNumberOfCellsRecursiveWithOverlap() const;
150     MEDCOUPLING_EXPORT int getNumberOfCellsRecursiveWithoutOverlap() const;
151     MEDCOUPLING_EXPORT const MEDCouplingIMesh *getImageMesh() const { return _mesh; }
152     //
153     MEDCOUPLING_EXPORT const MEDCouplingCartesianAMRMeshGen *getFather() const;
154     MEDCOUPLING_EXPORT const MEDCouplingCartesianAMRMeshGen *getGodFather() const;
155     MEDCOUPLING_EXPORT int getAbsoluteLevel() const;
156     MEDCOUPLING_EXPORT std::vector<MEDCouplingCartesianAMRPatchGen *> retrieveGridsAt(int absoluteLev) const;
157     MEDCOUPLING_EXPORT void detachFromFather();
158     MEDCOUPLING_EXPORT void addPatch(const std::vector< std::pair<int,int> >& bottomLeftTopRight, const std::vector<int>& factors);
159     MEDCOUPLING_EXPORT void createPatchesFromCriterion(const INTERP_KERNEL::BoxSplittingOptions& bso, const std::vector<bool>& criterion, const std::vector<int>& factors);
160     MEDCOUPLING_EXPORT void createPatchesFromCriterion(const INTERP_KERNEL::BoxSplittingOptions& bso, const DataArrayByte *criterion, const std::vector<int>& factors);
161     MEDCOUPLING_EXPORT void removeAllPatches();
162     MEDCOUPLING_EXPORT void removePatch(int patchId);
163     MEDCOUPLING_EXPORT int getNumberOfPatches() const;
164     MEDCOUPLING_EXPORT int getPatchIdFromChildMesh(const MEDCouplingCartesianAMRMeshGen *mesh) const;
165     MEDCOUPLING_EXPORT std::vector< const MEDCouplingCartesianAMRPatch *> getPatches() const;
166     MEDCOUPLING_EXPORT const MEDCouplingCartesianAMRPatch *getPatch(int patchId) const;
167     MEDCOUPLING_EXPORT bool isPatchInNeighborhoodOf(int patchId1, int patchId2, int ghostLev) const;
168     MEDCOUPLING_EXPORT DataArrayDouble *createCellFieldOnPatch(int patchId, const DataArrayDouble *cellFieldOnThis) const;
169     // coarse to fine
170     MEDCOUPLING_EXPORT void fillCellFieldOnPatch(int patchId, const DataArrayDouble *cellFieldOnThis, DataArrayDouble *cellFieldOnPatch) const;
171     MEDCOUPLING_EXPORT void fillCellFieldOnPatchGhost(int patchId, const DataArrayDouble *cellFieldOnThis, DataArrayDouble *cellFieldOnPatch, int ghostLev) const;
172     MEDCOUPLING_EXPORT void fillCellFieldOnPatchOnlyOnGhostZone(int patchId, const DataArrayDouble *cellFieldOnThis, DataArrayDouble *cellFieldOnPatch, int ghostLev) const;
173     // coarse to fine + fine to fine
174     MEDCOUPLING_EXPORT void fillCellFieldOnPatchGhostAdv(int patchId, const DataArrayDouble *cellFieldOnThis, int ghostLev, const std::vector<const DataArrayDouble *>& arrsOnPatches) const;
175     // fine to fine
176     MEDCOUPLING_EXPORT void fillCellFieldOnPatchOnlyGhostAdv(int patchId, int ghostLev, const std::vector<const DataArrayDouble *>& arrsOnPatches) const;
177     MEDCOUPLING_EXPORT void fillCellFieldOnPatchOnlyOnGhostZoneWith(int ghostLev, const MEDCouplingCartesianAMRPatch *patchToBeModified, const MEDCouplingCartesianAMRPatch *neighborPatch, DataArrayDouble *cellFieldOnPatch, const DataArrayDouble *cellFieldNeighbor) const;
178     // fine to coarse
179     MEDCOUPLING_EXPORT void fillCellFieldComingFromPatch(int patchId, const DataArrayDouble *cellFieldOnPatch, DataArrayDouble *cellFieldOnThis) const;
180     MEDCOUPLING_EXPORT void fillCellFieldComingFromPatchGhost(int patchId, const DataArrayDouble *cellFieldOnPatch, DataArrayDouble *cellFieldOnThis, int ghostLev) const;
181     //
182     MEDCOUPLING_EXPORT DataArrayInt *findPatchesInTheNeighborhoodOf(int patchId, int ghostLev) const;
183     //
184     MEDCOUPLING_EXPORT MEDCouplingUMesh *buildUnstructured() const;
185     MEDCOUPLING_EXPORT MEDCoupling1SGTUMesh *buildMeshFromPatchEnvelop() const;
186     MEDCOUPLING_EXPORT MEDCoupling1SGTUMesh *buildMeshOfDirectChildrenOnly() const;
187     MEDCOUPLING_EXPORT MEDCouplingFieldDouble *buildCellFieldOnRecurseWithoutOverlapWithoutGhost(int ghostSz, const std::vector<const DataArrayDouble *>& recurseArrs) const;
188     MEDCOUPLING_EXPORT DataArrayDouble *extractGhostFrom(int ghostSz, const DataArrayDouble *arr) const;
189     MEDCOUPLING_EXPORT std::vector<int> getPatchIdsInTheNeighborhoodOf(int patchId, int ghostLev) const;
190   protected:
191     MEDCouplingCartesianAMRMeshGen(const std::string& meshName, int spaceDim, const int *nodeStrctStart, const int *nodeStrctStop,
192                                    const double *originStart, const double *originStop, const double *dxyzStart, const double *dxyzStop);
193     MEDCouplingCartesianAMRMeshGen(MEDCouplingCartesianAMRMeshGen *father, MEDCouplingIMesh *mesh);
194     void checkPatchId(int patchId) const;
195     void checkFactorsAndIfNotSetAssign(const std::vector<int>& factors);
196     void retrieveGridsAtInternal(int lev, std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingCartesianAMRPatchGen> >& grids) const;
197     static int GetGhostLevelInFineRef(int ghostLev, const std::vector<int>& factors);
198     std::vector<const DataArrayDouble *> extractSubTreeFromGlobalFlatten(const MEDCouplingCartesianAMRMeshGen *head, const std::vector<const DataArrayDouble *>& all) const;
199   protected:
200     MEDCOUPLING_EXPORT std::size_t getHeapMemorySizeWithoutChildren() const;
201     MEDCOUPLING_EXPORT std::vector<const BigMemoryObject *> getDirectChildren() const;
202     MEDCOUPLING_EXPORT void updateTime() const;
203   private:
204     MEDCouplingCartesianAMRMeshGen *_father;
205     MEDCouplingAutoRefCountObjectPtr<MEDCouplingIMesh> _mesh;
206     std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingCartesianAMRPatch> > _patches;
207     std::vector<int> _factors;
208   };
209
210   class MEDCouplingCartesianAMRMeshSub : public MEDCouplingCartesianAMRMeshGen
211   {
212   public:
213     MEDCouplingCartesianAMRMeshSub(MEDCouplingCartesianAMRMeshGen *father, MEDCouplingIMesh *mesh);
214   };
215
216   class MEDCouplingCartesianAMRMesh : public MEDCouplingCartesianAMRMeshGen
217   {
218   public:
219     MEDCOUPLING_EXPORT static MEDCouplingCartesianAMRMesh *New(const std::string& meshName, int spaceDim, const int *nodeStrctStart, const int *nodeStrctStop,
220                                                                const double *originStart, const double *originStop, const double *dxyzStart, const double *dxyzStop);
221     MEDCOUPLING_EXPORT const MEDCouplingDataForGodFather *getDataConst() const { return _data; }
222     MEDCOUPLING_EXPORT MEDCouplingDataForGodFather *getData() { return _data; }
223     MEDCOUPLING_EXPORT void setData(MEDCouplingDataForGodFather *data);
224     MEDCOUPLING_EXPORT void allocData() const;
225     MEDCOUPLING_EXPORT void deallocData() const;
226   private:
227     MEDCouplingCartesianAMRMesh(const std::string& meshName, int spaceDim, const int *nodeStrctStart, const int *nodeStrctStop,
228                                 const double *originStart, const double *originStop, const double *dxyzStart, const double *dxyzStop);
229     MEDCOUPLING_EXPORT std::vector<const BigMemoryObject *> getDirectChildren() const;
230     void checkData() const;
231     ~MEDCouplingCartesianAMRMesh();
232   private:
233     mutable MEDCouplingAutoRefCountObjectPtr<MEDCouplingDataForGodFather> _data;
234   };
235 }
236
237 #endif
238