G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsWeightMapperUtils.hpp
1 
14 #include <gsMSplines/gsWeightMapperUtils.h>
15 #include <vector>
16 #include <algorithm>
17 
18 namespace gismo {
19 
20 template <class T>
21 index_t reorderMapperTarget(gsWeightMapper<T> &mapper, const std::vector<index_t>& perm, gsPermutationMatrix* permMatrix)
22 {
23  const bool matSupplied = (permMatrix != NULL);
24  const index_t size=mapper.getNrOfTargets();
25  const index_t start=size-perm.size();
26  const index_t flag=size+1; // invalid index
27 
28  std::vector<index_t> reordered (size,flag);
29  for (size_t i=0; i<perm.size(); ++i)
30  {
31  reordered[perm[i]]=start+i;
32  }
33  index_t pos=0;
34  for (size_t i=0; i<reordered.size(); ++i)
35  {
36  if(reordered[i]==flag)
37  {
38  reordered[i]=pos;
39  ++pos;
40  }
41  }
42  gsAsVector<index_t> permVec(reordered);
43  gsPermutationMatrix m_perm(permVec);
44  if(matSupplied)
45  *permMatrix=m_perm;
46  mapper*=m_perm.inverse();
47  return start;
48 }
49 
50 
51 // assumes that the indices are not preset in the destination
52 // assumes row mayor
53 template <class T>
54 void copyToBlock (const gsWeightMapper<T> &src, typename gsWeightMapper<T>::LToGMatrix &dst, index_t rowShift, index_t colShift)
55 {
56  src.optimize();
57  for (index_t r=0; r<src.getNrOfSources(); ++r)
58  for (typename gsWeightMapper<T>::Iterator entry=src.fastSourceToTarget(r); entry; ++entry)
59  dst.insert(r+rowShift,entry.index()+colShift)=entry.weight();
60 }
61 
62 
63 template <class T>
64 gsWeightMapper<T>* combineMappers (const std::vector<gsWeightMapper<T>*> &mappers, std::vector<index_t> &shifts, bool needShifting)
65 {
66  gsWeightMapper<T>* result=new gsWeightMapper<T>();
67  combineMappers(mappers,needShifting,shifts,*result);
68  return result;
69 }
70 
71 template <class T>
72 void combineMappers (const std::vector<gsWeightMapper<T>*> &mappers, bool needShifting, std::vector<index_t> &shifts, gsWeightMapper<T>& result)
73 {
74  typename gsWeightMapper<T>::LToGMatrix &resultMatrix=result.asMatrix();
75 
76  shifts.clear();
77  shifts.resize(mappers.size()+1);
78  shifts[0]=0;
79 
80  index_t totTarSize=0;
81  index_t totNNZ=0;
82  index_t totShift=0;
83 
84  for (size_t m=0;m<mappers.size();++m)
85  {
86  totShift+=mappers[m]->getNrOfSources();
87  shifts[m+1]=totShift;
88  if (needShifting)
89  totTarSize+=mappers[m]->getNrOfTargets();
90  else
91  totTarSize=std::max(totTarSize,mappers[m]->getNrOfTargets());
92  totNNZ+=mappers[m]->asMatrix().nonZeros();
93  }
94 
95  resultMatrix.resize(shifts.back(),totTarSize);
96  resultMatrix.reserve(totNNZ);
97 
98  index_t startingGlobal=0;
99  for (size_t m=0;m<mappers.size();++m)
100  {
101  copyToBlock( *mappers[m], resultMatrix, shifts[m], startingGlobal);
102  if (needShifting)
103  startingGlobal+= mappers[m]->getNrOfTargets();
104  }
105 }
106 
107 
108 
109 
110 
111 }
112 
113 
gsEigen::PermutationMatrix< Dynamic, Dynamic, index_t > gsPermutationMatrix
reorderMapperTarget permutes the target indices of mapper according to permutation. If permutation does not contain all indices then the order of the remaining indices is preserved and the specified indices are appended to the end.
Definition: gsWeightMapperUtils.h:36
#define index_t
Definition: gsConfig.h:32
gsWeightMapper< T > * combineMappers(const std::vector< gsWeightMapper< T > * > &mappers, std::vector< index_t > &shifts, bool needShifting=true)
combineMappers Given a set of mappers it creates a new mapper that combines all. It can be used to co...
Definition: gsWeightMapperUtils.hpp:64