Salome HOME
Copyright update 2020
[tools/medcoupling.git] / src / ParaMEDMEM / OverlapMapping.hxx
1 // Copyright (C) 2007-2020  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 (CEA/DEN)
20
21 #ifndef __OVERLAPMAPPING_HXX__
22 #define __OVERLAPMAPPING_HXX__
23
24 #include "MCAuto.hxx"
25 #include "OverlapElementLocator.hxx"
26
27 #include <vector>
28 #include <map>
29 //#define DEC_DEBUG
30
31 namespace MEDCoupling
32 {
33   class ProcessorGroup;
34   class DataArrayInt;
35   class MEDCouplingFieldDouble;
36
37   using namespace std;
38   typedef map<mcIdType,double> SparseDoubleVec;
39
40   /*!
41    * Internal class, not part of the public API.
42    *
43    * Used by the impl of OverlapInterpolationMatrix, plays an equivalent role than what the NxM_Mapping
44    * does for the InterpolationMatrix.
45    */
46   class OverlapMapping
47   {
48   public:
49
50     OverlapMapping(const ProcessorGroup& group, const OverlapElementLocator& locator);
51     void keepTracksOfSourceIds(int procId, DataArrayIdType *ids);
52     void keepTracksOfTargetIds(int procId, DataArrayIdType *ids);
53     void addContributionST(const vector< SparseDoubleVec >& matrixST, const DataArrayIdType *srcIds, int srcProcId, const DataArrayIdType *trgIds, int trgProcId);
54     void prepare(const vector< int >& procsToSendField, mcIdType nbOfTrgElems);
55     void computeDenoConservativeVolumic(mcIdType nbOfTuplesTrg);
56 //    void computeDenoIntegralGlobConstraint();
57 //    void computeDenoIntegral();
58     void computeDenoRevIntegral(const DataArrayDouble & targetAreas);
59     //
60     void multiply(const MEDCouplingFieldDouble *fieldInput, MEDCouplingFieldDouble *fieldOutput, double default_val) const;
61     void transposeMultiply(const MEDCouplingFieldDouble *fieldInput, MEDCouplingFieldDouble *fieldOutput);
62   private:
63     void serializeMatrixStep0ST(const mcIdType *nbOfElemsSrc, mcIdType *&bigArr, int *count, int *offsets,
64                                 int *countForRecv, int *offsetsForRecv) const;
65     mcIdType serializeMatrixStep1ST(const mcIdType *nbOfElemsSrc, const mcIdType *recvStep0, const int *countStep0, const int *offsStep0,
66                                mcIdType *&bigArrI, double *&bigArrD, int *count, int *offsets,
67                                int *countForRecv, int *offsForRecv) const;
68     void unserializationST(mcIdType nbOfTrgElems, const mcIdType *nbOfElemsSrcPerProc, const mcIdType *bigArrRecv, const int *bigArrRecvCounts, const int *bigArrRecvOffs,
69                            const mcIdType *bigArrRecv2, const double *bigArrDRecv2, const int *bigArrRecv2Count, const int *bigArrRecv2Offs);
70     void finishToFillFinalMatrixST();
71     void fillSourceIdsZipReceivedForMultiply();
72
73 #ifdef DEC_DEBUG
74     void printMatrixesST() const;
75     void printTheMatrix() const;
76     void printDenoMatrix() const;
77 #endif
78   private:
79     const ProcessorGroup &_group;
80     const OverlapElementLocator& _locator;
81
82     /**! Map of DAInt of cell identifiers. For a proc ID i,
83      * gives an old2new map for the local part of the source mesh that has been sent to proc#i, just based on the
84      * bounding box computation (this is potentially a larger set than what is finally in the interp matrix).
85      * Second member gives proc ID.  */
86     map < int, MCAuto<DataArrayIdType> > _sent_src_ids;
87
88     //! See _sent_src_ids. Same for target mesh.
89     map < int, MCAuto<DataArrayIdType> > _sent_trg_ids;
90
91     /**! Vector of matrixes (partial interpolation ratios), result of the LOCAL interpolator run.
92      * Indexing shared with _source_proc_id_st, and _target_proc_id_st.   */
93     vector< vector< SparseDoubleVec > > _matrixes_st;
94     //! See _matrixes_st - vec of source proc IDs
95     vector< int > _source_proc_id_st;
96     //! See _matrixes_st - vec of target proc IDs
97     vector< int > _target_proc_id_st;
98
99     /**! Number of received source mesh IDs at mesh data exchange.
100      Counting the number of IDs suffices, as we just need this to prepare the receive side, when doing the final vector matrix multiplication.
101      First dimension is the remote proc ID from which we received. */
102     map <int, mcIdType > _nb_of_rcv_src_ids;
103
104     /**! Specifies for each (target) remote proc ID (first dim of the map) the corresponding
105      * source cell IDs to use.
106      * This information is stored from the *locally* COMPuted matrices, and corresponds hence to field value that will need to
107      * sent later on, if this matrix bit itself is sent aways.  */
108     map<int, vector<mcIdType> > _src_ids_zip_comp;
109
110     /**! Same idea as _src_ids_zip_comp above, but for RECEIVED matrix. */
111     map<int, vector<mcIdType> > _src_ids_zip_recv;
112
113     /**! THE matrix for matrix-vector product. The first dimension is indexed in the set of target procs
114     * that interacts with local source mesh. The second dim is the target cell ID.
115     * Same indexing as _the_matrix_st_source_proc_id and _the_deno_st.
116     * We don't use a map here to be more efficient in the final matrix-vector computation which requires the joint
117     * taversal of _the_matrix_st and _the_deno_st.
118     * This matrix is filled after receival from other procs, contrary to _matrixes_st which contains local computations.*/
119     vector< vector< SparseDoubleVec > > _the_matrix_st;
120     //! See _the_matrix_st above. List of source proc IDs contributing to _the_matrix_st
121     vector< int > _the_matrix_st_source_proc_id;
122     // Denominators (computed from the numerator matrix). As for _the_matrix_st it is paired with _the_matrix_st_source_proc_id
123     vector< vector< SparseDoubleVec > > _the_deno_st;
124
125     //! Proc IDs to which data will be sent (originating this current proc) for matrix-vector computation
126     vector< int > _proc_ids_to_send_vector_st;
127   };
128 }
129
130 #endif