Salome HOME
7905f68808790e0fd057a37c14b6eb0f3fe12d2a
[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     // basic set/get
79     MEDCOUPLING_EXPORT const std::vector< std::pair<int,int> >& getBLTRRange() const { return _bl_tr; }
80   private:
81     std::size_t getHeapMemorySizeWithoutChildren() const;
82   private:
83     //! bottom left/top right cell range relative to \a _father
84     std::vector< std::pair<int,int> > _bl_tr;
85   };
86
87   /*!
88    * This class does not inherit from TimeLabel so only const method should exist.
89    */
90   class MEDCouplingCartesianAMRPatchGF : public MEDCouplingCartesianAMRPatchGen
91   {
92   public:
93     MEDCouplingCartesianAMRPatchGF(MEDCouplingCartesianAMRMesh *mesh);
94   private:
95     std::size_t getHeapMemorySizeWithoutChildren() const;
96   };
97
98   class MEDCouplingDataForGodFather : public RefCountObject
99   {
100     friend class MEDCouplingCartesianAMRMesh;
101   public:
102     MEDCOUPLING_EXPORT virtual void synchronizeFineToCoarse(int ghostLev) = 0;
103     MEDCOUPLING_EXPORT virtual void synchronizeCoarseToFine(int ghostLev) = 0;
104     MEDCOUPLING_EXPORT virtual void synchronizeCoarseToFineOnlyInGhostZone(int ghostLev) = 0;
105     MEDCOUPLING_EXPORT virtual void synchronizeFineEachOtherInGhostZone(int ghostLev) = 0;
106     MEDCOUPLING_EXPORT virtual void alloc(int ghostLev) = 0;
107     MEDCOUPLING_EXPORT virtual void dealloc() = 0;
108   protected:
109     MEDCouplingDataForGodFather(MEDCouplingCartesianAMRMesh *gf);
110     void checkGodFatherFrozen() const;
111   protected:
112     virtual bool changeGodFather(MEDCouplingCartesianAMRMesh *gf);
113   protected:
114     MEDCouplingCartesianAMRMesh *_gf;
115     TimeLabelConstOverseer _tlc;
116   };
117   /// @endcond
118
119   /*!
120    * This class is the base class dedicated to AMR using Adaptative Hierarchical Overlapped image Grid.
121    * This class does \b NOT inherit from MEDCouplingMesh because this class overlaps image grid structured meshes to perform adaptative mesh refinement.
122    * But this class aggregates MEDCouplingMesh instances !
123    */
124   class MEDCouplingCartesianAMRMeshGen : public RefCountObject, public TimeLabel
125   {
126   public:
127     MEDCOUPLING_EXPORT int getSpaceDimension() const;
128     MEDCOUPLING_EXPORT const std::vector<int>& getFactors() const { return _factors; }
129     MEDCOUPLING_EXPORT void setFactors(const std::vector<int>& newFactors);
130     MEDCOUPLING_EXPORT int getMaxNumberOfLevelsRelativeToThis() const;
131     MEDCOUPLING_EXPORT int getNumberOfCellsAtCurrentLevel() const;
132     MEDCOUPLING_EXPORT int getNumberOfCellsAtCurrentLevelGhost(int ghostLev) const;
133     MEDCOUPLING_EXPORT int getNumberOfCellsRecursiveWithOverlap() const;
134     MEDCOUPLING_EXPORT int getNumberOfCellsRecursiveWithoutOverlap() const;
135     MEDCOUPLING_EXPORT const MEDCouplingIMesh *getImageMesh() const { return _mesh; }
136     //
137     MEDCOUPLING_EXPORT const MEDCouplingCartesianAMRMeshGen *getFather() const;
138     MEDCOUPLING_EXPORT const MEDCouplingCartesianAMRMeshGen *getGodFather() const;
139     MEDCOUPLING_EXPORT int getAbsoluteLevel() const;
140     MEDCOUPLING_EXPORT std::vector<MEDCouplingCartesianAMRPatchGen *> retrieveGridsAt(int absoluteLev) const;
141     MEDCOUPLING_EXPORT void detachFromFather();
142     MEDCOUPLING_EXPORT void addPatch(const std::vector< std::pair<int,int> >& bottomLeftTopRight, const std::vector<int>& factors);
143     MEDCOUPLING_EXPORT void createPatchesFromCriterion(const INTERP_KERNEL::BoxSplittingOptions& bso, const std::vector<bool>& criterion, const std::vector<int>& factors);
144     MEDCOUPLING_EXPORT void createPatchesFromCriterion(const INTERP_KERNEL::BoxSplittingOptions& bso, const DataArrayByte *criterion, const std::vector<int>& factors);
145     MEDCOUPLING_EXPORT void removeAllPatches();
146     MEDCOUPLING_EXPORT void removePatch(int patchId);
147     MEDCOUPLING_EXPORT int getNumberOfPatches() const;
148     MEDCOUPLING_EXPORT int getPatchIdFromChildMesh(const MEDCouplingCartesianAMRMeshGen *mesh) const;
149     MEDCOUPLING_EXPORT std::vector< const MEDCouplingCartesianAMRPatch *> getPatches() const;
150     MEDCOUPLING_EXPORT const MEDCouplingCartesianAMRPatch *getPatch(int patchId) const;
151     MEDCOUPLING_EXPORT bool isPatchInNeighborhoodOf(int patchId1, int patchId2, int ghostLev) const;
152     MEDCOUPLING_EXPORT DataArrayDouble *createCellFieldOnPatch(int patchId, const DataArrayDouble *cellFieldOnThis) const;
153     // coarse to fine
154     MEDCOUPLING_EXPORT void fillCellFieldOnPatch(int patchId, const DataArrayDouble *cellFieldOnThis, DataArrayDouble *cellFieldOnPatch) const;
155     MEDCOUPLING_EXPORT void fillCellFieldOnPatchGhost(int patchId, const DataArrayDouble *cellFieldOnThis, DataArrayDouble *cellFieldOnPatch, int ghostLev) const;
156     MEDCOUPLING_EXPORT void fillCellFieldOnPatchOnlyOnGhostZone(int patchId, const DataArrayDouble *cellFieldOnThis, DataArrayDouble *cellFieldOnPatch, int ghostLev) const;
157     // coarse to fine + fine to fine
158     MEDCOUPLING_EXPORT void fillCellFieldOnPatchGhostAdv(int patchId, const DataArrayDouble *cellFieldOnThis, int ghostLev, const std::vector<const DataArrayDouble *>& arrsOnPatches) const;
159     // fine to fine
160     MEDCOUPLING_EXPORT void fillCellFieldOnPatchOnlyGhostAdv(int patchId, int ghostLev, const std::vector<const DataArrayDouble *>& arrsOnPatches) const;
161     // fine to coarse
162     MEDCOUPLING_EXPORT void fillCellFieldComingFromPatch(int patchId, const DataArrayDouble *cellFieldOnPatch, DataArrayDouble *cellFieldOnThis) const;
163     MEDCOUPLING_EXPORT void fillCellFieldComingFromPatchGhost(int patchId, const DataArrayDouble *cellFieldOnPatch, DataArrayDouble *cellFieldOnThis, int ghostLev) const;
164     //
165     MEDCOUPLING_EXPORT DataArrayInt *findPatchesInTheNeighborhoodOf(int patchId, int ghostLev) const;
166     //
167     MEDCOUPLING_EXPORT MEDCouplingUMesh *buildUnstructured() const;
168     MEDCOUPLING_EXPORT MEDCoupling1SGTUMesh *buildMeshFromPatchEnvelop() const;
169     MEDCOUPLING_EXPORT MEDCoupling1SGTUMesh *buildMeshOfDirectChildrenOnly() const;
170     MEDCOUPLING_EXPORT MEDCouplingFieldDouble *buildCellFieldOnRecurseWithoutOverlapWithoutGhost(int ghostSz, const std::vector<const DataArrayDouble *>& recurseArrs) const;
171     MEDCOUPLING_EXPORT DataArrayDouble *extractGhostFrom(int ghostSz, const DataArrayDouble *arr) const;
172   protected:
173     MEDCouplingCartesianAMRMeshGen(const std::string& meshName, int spaceDim, const int *nodeStrctStart, const int *nodeStrctStop,
174                                    const double *originStart, const double *originStop, const double *dxyzStart, const double *dxyzStop);
175     MEDCouplingCartesianAMRMeshGen(MEDCouplingCartesianAMRMeshGen *father, MEDCouplingIMesh *mesh);
176     void checkPatchId(int patchId) const;
177     void checkFactorsAndIfNotSetAssign(const std::vector<int>& factors);
178     void retrieveGridsAtInternal(int lev, std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingCartesianAMRPatchGen> >& grids) const;
179     static void ApplyFactorsOnCompactFrmt(std::vector< std::pair<int,int> >& partBeforeFact, const std::vector<int>& factors);
180     static void ApplyGhostOnCompactFrmt(std::vector< std::pair<int,int> >& partBeforeFact, int ghostSize);
181     static void ApplyAllGhostOnCompactFrmt(std::vector< std::pair<int,int> >& partBeforeFact, int ghostSize);
182     std::vector<const DataArrayDouble *> extractSubTreeFromGlobalFlatten(const MEDCouplingCartesianAMRMeshGen *head, const std::vector<const DataArrayDouble *>& all) const;
183   protected:
184     MEDCOUPLING_EXPORT std::size_t getHeapMemorySizeWithoutChildren() const;
185     MEDCOUPLING_EXPORT std::vector<const BigMemoryObject *> getDirectChildren() const;
186     MEDCOUPLING_EXPORT void updateTime() const;
187   private:
188     MEDCouplingCartesianAMRMeshGen *_father;
189     MEDCouplingAutoRefCountObjectPtr<MEDCouplingIMesh> _mesh;
190     std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingCartesianAMRPatch> > _patches;
191     std::vector<int> _factors;
192   };
193
194   class MEDCouplingCartesianAMRMeshSub : public MEDCouplingCartesianAMRMeshGen
195   {
196   public:
197     MEDCouplingCartesianAMRMeshSub(MEDCouplingCartesianAMRMeshGen *father, MEDCouplingIMesh *mesh);
198   };
199
200   class MEDCouplingCartesianAMRMesh : public MEDCouplingCartesianAMRMeshGen
201   {
202   public:
203     MEDCOUPLING_EXPORT static MEDCouplingCartesianAMRMesh *New(const std::string& meshName, int spaceDim, const int *nodeStrctStart, const int *nodeStrctStop,
204                                                                const double *originStart, const double *originStop, const double *dxyzStart, const double *dxyzStop);
205     MEDCOUPLING_EXPORT const MEDCouplingDataForGodFather *getDataConst() const { return _data; }
206     MEDCOUPLING_EXPORT MEDCouplingDataForGodFather *getData() { return _data; }
207     MEDCOUPLING_EXPORT void setData(MEDCouplingDataForGodFather *data);
208     MEDCOUPLING_EXPORT void allocData(int ghostLev) const;
209     MEDCOUPLING_EXPORT void deallocData() const;
210   private:
211     MEDCouplingCartesianAMRMesh(const std::string& meshName, int spaceDim, const int *nodeStrctStart, const int *nodeStrctStop,
212                                 const double *originStart, const double *originStop, const double *dxyzStart, const double *dxyzStop);
213     MEDCOUPLING_EXPORT std::vector<const BigMemoryObject *> getDirectChildren() const;
214     void checkData() const;
215     ~MEDCouplingCartesianAMRMesh();
216   private:
217     mutable MEDCouplingAutoRefCountObjectPtr<MEDCouplingDataForGodFather> _data;
218   };
219 }
220
221 #endif
222