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