G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsFunctionSum.h
Go to the documentation of this file.
1 
15 #pragma once
16 
17 #include<gsCore/gsFunctionSet.h>
18 
19 namespace gismo
20 {
21 template<class T> class gsFunctionPieceSum;
22 
23 template<class T>
24 class gsFunctionSum : public gsFunctionSet<T>
25 {
26 public:
27  gsFunctionSum()
28  :
29  m_size(0)
30  {}
31 
32  gsFunctionSum(const std::vector<const gsFunctionSet<T> * > & functions)
33  :
34  m_functions(functions),
35  m_size(functions.size())
36  {
37  _init(m_functions);
38  }
39 
40  gsFunctionSum(const gsFunctionSet<T> * undeformed, const gsFunctionSet<T> * deformation)
41  :
42  m_size(2)
43  {
44  m_functions.push_back(undeformed);
45  m_functions.push_back(deformation);
46 
47  _init(m_functions);
48  }
49 
50  index_t nPieces() const {return m_functions[0]->nPieces();}
51 
52  short_t domainDim() const
53  { return m_functions[0]->domainDim(); }
54 
55  short_t targetDim() const
56  { return m_functions[0]->targetDim(); }
57 
58  const gsFunctionPieceSum<T> & piece(const index_t k) const { return m_pieces[k]; }
59 
60  index_t size() const {return nPieces();}
61 
62  std::ostream &print(std::ostream &os) const
63  {
64  for (index_t p = 1; p!=m_size; p++)
65  gsInfo<<"Piece "<<p<<":\n"<<m_pieces[p]<<"\n";
66  return os;
67  }
68 
69 
70  index_t funcSize() const {return m_functions.size();}
71 
72  const gsFunctionSet<T> * func(const index_t i) const { return m_functions[i]; }
73 
74 private:
75  void _init(const std::vector<const gsFunctionSet<T> * > & functions)
76  {
77  GISMO_ASSERT(functions.size()>0,"Must give one or more function sets.");
78  for (index_t p = 1; p!=m_size; p++)
79  {
80  GISMO_ASSERT(functions[p-1]->nPieces()==functions[p]->nPieces(),"Number of pieces does not match for function "<<p-1<<" and "<<p<<"!");
81  GISMO_ASSERT(functions[p-1]->domainDim()==functions[p]->domainDim(),"Domain dimension does not match for function "<<p-1<<" and "<<p<<"!");
82  GISMO_ASSERT(functions[p-1]->targetDim()==functions[p]->targetDim(),"Target dimension does not match for function "<<p-1<<" and "<<p<<"!");
83  }
84 
85  for (index_t p = 0; p!=functions[0]->nPieces(); p++)
86  m_pieces.push_back(gsFunctionPieceSum<T>(this,p));
87  }
88 
89 protected:
90  std::vector<const gsFunctionSet<T> * > m_functions;
91  std::vector<gsFunctionPieceSum<T> > m_pieces;
92  const index_t m_size;
93  gsMatrix<T> m_support;
94 
95 };
96 
97 template<class T>
98 class gsFunctionPieceSum : public gsFunction<T>
99 {
101  typedef memory::shared_ptr< gsFunctionPieceSum<T> > Ptr;
102 
104  typedef memory::unique_ptr< gsFunctionPieceSum<T> > uPtr;
105 
106 public:
107  gsFunctionPieceSum(const gsFunctionSum<T> * geom, const index_t index)
108  :
109  m_geom(geom),
110  m_index(index)
111  {
112  m_support = m_geom->func(0)->piece(m_index).support();
113  for (index_t p = 1; p!=m_geom->funcSize(); p++)
114  for (index_t d=0; d!=m_support.rows(); d++)
115  {
116  GISMO_ASSERT(m_geom->func(p)->piece(m_index).support().rows()!=0 && m_geom->func(p)->piece(m_index).support().cols()!=0,"Support is empty");
117 
118  if (m_support(d,0) > m_geom->func(p)->piece(m_index).support()(d,0))
119  m_support(d,0) = m_geom->func(p)->piece(m_index).support()(d,0);
120  if (m_support(d,1) < m_geom->func(p)->piece(m_index).support()(d,1))
121  m_support(d,1) = m_geom->func(p)->piece(m_index).support()(d,1);
122  }
123  }
124 
125  gsMatrix<T> support() const
126  {
127  return m_support;
128  }
129 
130  short_t domainDim() const
131  { return m_geom->domainDim(); }
132 
133  short_t targetDim() const
134  { return m_geom->targetDim(); }
135 
137  void eval_into(const gsMatrix<T> & u, gsMatrix<T>& result) const
138  {
139  gsMatrix<T> tmp;
140  m_geom->func(0)->piece(m_index).eval_into(u,result);
141  for (index_t p = 1; p!=m_geom->funcSize(); p++)
142  {
143  m_geom->func(p)->piece(m_index).eval_into(u,tmp);
144  result += tmp;
145  }
146  }
147 
149  void deriv_into(const gsMatrix<T> & u, gsMatrix<T>& result ) const
150  {
151  gsMatrix<T> tmp;
152  m_geom->func(0)->piece(m_index).deriv_into(u,result);
153  for (index_t p = 1; p!=m_geom->funcSize(); p++)
154  {
155  m_geom->func(p)->piece(m_index).deriv_into(u,tmp);
156  result += tmp;
157  }
158 
159  }
160 
162  void deriv2_into(const gsMatrix<T> & u, gsMatrix<T>& result ) const
163  {
164  gsMatrix<T> tmp;
165  m_geom->func(0)->piece(m_index).deriv2_into(u,result);
166  for (index_t p = 1; p!=m_geom->funcSize(); p++)
167  {
168  m_geom->func(p)->piece(m_index).deriv2_into(u,tmp);
169  result += tmp;
170  }
171 
172  }
173 
176  void evalAllDers_into(const gsMatrix<T> & u, int n, std::vector<gsMatrix<T> >& result) const
177  {
178  std::vector<gsMatrix<T> > tmp;
179  m_geom->func(0)->piece(m_index).evalAllDers_into(u,n,result);
180  for (index_t p = 1; p!=m_geom->funcSize(); p++)
181  {
182  m_geom->func(p)->piece(m_index).evalAllDers_into(u,n,tmp);
183  for (size_t k = 0; k!=result.size(); k++)
184  {
185  result[k] += tmp[k];
186  }
187  }
188  }
189 
190  GISMO_CLONE_FUNCTION(gsFunctionPieceSum)
191 
192  std::ostream &print(std::ostream &os) const
193  {
194  for (index_t p = 0; p!= m_geom->funcSize(); p++)
195  gsInfo<<" function "<<p<<":\n"<<m_geom->func(p)->piece(m_index)<<"\n";
196  return os;
197  }
198 
199 protected:
200  const gsFunctionSum<T> * m_geom;
201  const index_t m_index;
202  gsMatrix<T> m_support;
203 
204 };
205 
206 } // namespace gismo
#define short_t
Definition: gsConfig.h:35
virtual void evalAllDers_into(const gsMatrix< T > &u, int n, std::vector< gsMatrix< T > > &result, bool sameElement=false) const
Evaluate the nonzero functions and their derivatives up to order n at points u into result...
Definition: gsFunctionSet.hpp:81
#define index_t
Definition: gsConfig.h:32
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
#define gsInfo
Definition: gsDebug.h:43
This is the interface of all objects that computes functions on points like gsBasis, gsGeometry and gsFunctions.