Line data Source code
1 : /*
2 : * Copyright (c) 2011-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Andreas Vogel
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #ifndef __H__UG__LIB_DISC__SPATIAL_DISC__USER_DATA__CONST_USER_DATA__
34 : #define __H__UG__LIB_DISC__SPATIAL_DISC__USER_DATA__CONST_USER_DATA__
35 :
36 : #include "common/common.h"
37 : #include "common/math/ugmath.h"
38 :
39 : #include "std_user_data.h"
40 :
41 : namespace ug {
42 :
43 :
44 : ///////////////////////////////////////////////////////////////////////////////
45 : // Base class for Constant Data
46 : ///////////////////////////////////////////////////////////////////////////////
47 :
48 : /**
49 : * This class is a base class for all Constant user data. The data thus does not
50 : * depend neither on space, time or subset nor on the a computed solution.
51 : * In order to use the interface, the deriving class must implement the method:
52 : *
53 : * inline void evaluate(TData& data) const
54 : *
55 : */
56 : template <typename TImpl, typename TData, int dim>
57 : class StdConstData
58 : : public StdUserData<StdConstData<TImpl,TData,dim>, TData, dim>
59 : {
60 : public:
61 0 : virtual void operator() (TData& value,
62 : const MathVector<dim>& globIP,
63 : number time, int si) const
64 : {
65 : getImpl().evaluate(value);
66 0 : }
67 :
68 0 : virtual void operator()(TData vValue[],
69 : const MathVector<dim> vGlobIP[],
70 : number time, int si, const size_t nip) const
71 : {
72 0 : for(size_t ip = 0; ip < nip; ++ip)
73 0 : getImpl().evaluate(vValue[ip]);
74 0 : }
75 :
76 : template <int refDim>
77 : inline void evaluate(TData vValue[],
78 : const MathVector<dim> vGlobIP[],
79 : number time, int si,
80 : GridObject* elem,
81 : const MathVector<dim> vCornerCoords[],
82 : const MathVector<refDim> vLocIP[],
83 : const size_t nip,
84 : LocalVector* u,
85 : const MathMatrix<refDim, dim>* vJT = NULL) const
86 : {
87 0 : for(size_t ip = 0; ip < nip; ++ip)
88 0 : getImpl().evaluate(vValue[ip]);
89 : }
90 :
91 : /// implement as a UserData
92 0 : virtual void compute(LocalVector* u, GridObject* elem,
93 : const MathVector<dim> vCornerCoords[], bool bDeriv = false)
94 : {
95 0 : for(size_t s = 0; s < this->num_series(); ++s)
96 0 : for(size_t ip = 0; ip < this->num_ip(s); ++ip)
97 : getImpl().evaluate(this->value(s,ip));
98 0 : }
99 :
100 : /// implement as a UserData
101 0 : virtual void compute(LocalVectorTimeSeries* u, GridObject* elem,
102 : const MathVector<dim> vCornerCoords[], bool bDeriv = false)
103 : {
104 0 : for(size_t s = 0; s < this->num_series(); ++s)
105 0 : for(size_t ip = 0; ip < this->num_ip(s); ++ip)
106 : getImpl().evaluate(this->value(s,ip));
107 0 : }
108 :
109 : /// callback, invoked when data storage changed
110 0 : virtual void value_storage_changed(const size_t seriesID)
111 : {
112 0 : for(size_t ip = 0; ip < this->num_ip(seriesID); ++ip)
113 : getImpl().evaluate(this->value(seriesID,ip));
114 0 : }
115 :
116 : /// returns if data is constant
117 0 : virtual bool constant() const {return true;}
118 :
119 : /// returns if grid function is needed for evaluation
120 0 : virtual bool requires_grid_fct() const {return false;}
121 :
122 : /// returns if provided data is continuous over geometric object boundaries
123 0 : virtual bool continuous() const {return true;}
124 :
125 : protected:
126 : /// access to implementation
127 : TImpl& getImpl() {return static_cast<TImpl&>(*this);}
128 :
129 : /// const access to implementation
130 : const TImpl& getImpl() const {return static_cast<const TImpl&>(*this);}
131 : };
132 :
133 : ///////////////////////////////////////////////////////////////////////////////
134 : // Constant UserData
135 : ///////////////////////////////////////////////////////////////////////////////
136 :
137 : /**
138 : * \brief User Data
139 : *
140 : * User Data that can be used in assembling routines.
141 : *
142 : * \defgroup lib_disc_user_data User Data
143 : * \ingroup lib_discretization
144 : */
145 :
146 : /// \addtogroup lib_disc_user_data
147 : /// @{
148 :
149 : /// constant scalar user data
150 : template <int dim>
151 : class ConstUserNumber
152 : : public StdConstData<ConstUserNumber<dim>, number, dim>
153 : {
154 : public:
155 : /// creates empty user number
156 0 : ConstUserNumber() {set(0.0);}
157 :
158 : /// creates user number with value
159 0 : ConstUserNumber(number val) {set(val);}
160 :
161 : /// set constant value
162 0 : void set(number val) {m_Number = val;}
163 :
164 : /// print current setting
165 0 : void print() const {UG_LOG("ConstUserNumber:" << m_Number << "\n");}
166 :
167 : /// evaluate
168 0 : inline void evaluate (number& value) const {value = m_Number;}
169 :
170 : /// get value
171 0 : number get() const {return m_Number;}
172 :
173 : protected:
174 : number m_Number;
175 : };
176 :
177 : /// constant vector user data
178 : /**
179 : * Constant vector user data that can be used in assembling routines.
180 : *
181 : * \param dim the dimensionality of the vector itself (for ex. 2 for vectors of two components)
182 : * \param worldDim the dimensionality of the space embedding the grid (for ex. 3 for 3d PDE problems)
183 : */
184 : template <int dim, int worldDim = dim>
185 : class ConstUserVector
186 : : public StdConstData<ConstUserVector<dim, worldDim>, MathVector<dim>, worldDim>
187 : {
188 : public:
189 : /// Constructor: no arguments, zero entries
190 0 : ConstUserVector() {set_all_entries(0.0);}
191 :
192 : /// Constructor: set all the entries to the given value
193 0 : ConstUserVector(number val) {set_all_entries(val);}
194 :
195 : /// Constructor: initialize with a given std::vector
196 0 : ConstUserVector(const std::vector<number>& val) {set_vector(val);}
197 :
198 : /// set all vector entries
199 0 : void set_all_entries(number val) { m_Vector = val;}
200 :
201 : /// set i'th vector entry
202 0 : void set_entry(size_t i, number val){m_Vector[i] = val;}
203 :
204 : /// set from a given vector:
205 0 : void set_vector(const std::vector<number>& val)
206 : {
207 0 : if(val.size() != dim) UG_THROW("Size mismatch in ConstUserVector");
208 0 : for(size_t i = 0; i < dim; i++) m_Vector[i] = val[i];
209 0 : }
210 :
211 : /// print current setting
212 0 : void print() const {UG_LOG("ConstUserVector:" << m_Vector << "\n");}
213 :
214 : /// evaluate
215 : inline void evaluate (MathVector<dim>& value) const{value = m_Vector;}
216 :
217 : protected:
218 : MathVector<dim> m_Vector;
219 : };
220 :
221 : /// constant matrix user data
222 : /**
223 : * Constant matrix user data that can be used in assembling routines.
224 : *
225 : * \param N the row size of the matrix
226 : * \param M the column size of the matrix
227 : * \param worldDim the dimensionality of the space embedding the grid (for ex. 3 for 3d PDE problems)
228 : */
229 : template <int N, int M = N, int worldDim = N>
230 : class ConstUserMatrix
231 : : public StdConstData<ConstUserMatrix<N, M, worldDim>, MathMatrix<N, M>, worldDim>
232 : {
233 : public:
234 : /// Constructor
235 0 : ConstUserMatrix() {set_diag_tensor(1.0);}
236 :
237 : /// Constructor setting the diagonal
238 0 : ConstUserMatrix(number val) {set_diag_tensor(val);}
239 :
240 : /// set diagonal of matrix to a vector
241 0 : void set_diag_tensor(number val)
242 : {
243 0 : for(size_t i = 0; i < N; ++i){
244 0 : for(size_t j = 0; j < M; ++j){
245 0 : m_Tensor[i][j] = 0;
246 : }
247 0 : m_Tensor[i][i] = val;
248 : }
249 0 : }
250 :
251 : /// sets all entries of the matrix
252 0 : void set_all_entries(number val)
253 : {
254 0 : for(size_t i = 0; i < N; ++i){
255 0 : for(size_t j = 0; j < M; ++j){
256 0 : m_Tensor[i][j] = val;
257 : }
258 : }
259 0 : }
260 :
261 : /// sets a single entry
262 0 : void set_entry(size_t i, size_t j, number val){m_Tensor[i][j] = val;}
263 :
264 : /// print current setting
265 0 : void print() const{UG_LOG("ConstUserMatrix:\n" << m_Tensor << "\n");}
266 :
267 : /// evaluate
268 : inline void evaluate (MathMatrix<N, M>& value) const{value = m_Tensor;}
269 :
270 : protected:
271 : MathMatrix<N, M> m_Tensor;
272 : };
273 :
274 : /// constant tensor user data
275 : template <int TRank, int dim>
276 : class ConstUserTensor
277 : : public StdConstData<ConstUserTensor<TRank,dim>, MathTensor<TRank, dim>, dim>
278 : {
279 : public:
280 : /// Constructor
281 : ConstUserTensor() {set(0.0);}
282 :
283 : /// Constructor setting the diagonal
284 0 : ConstUserTensor(number val) {set(val);}
285 :
286 : /// set diagonal of matrix to a vector
287 : void set(number val) {m_Tensor.set(val);}
288 :
289 : /// print current setting
290 : void print() const{UG_LOG("ConstUserTensor:\n" << m_Tensor << "\n");}
291 :
292 : /// evaluate
293 0 : inline void evaluate (MathTensor<TRank, dim>& value) const{value = m_Tensor;}
294 :
295 : protected:
296 : MathTensor<TRank, dim> m_Tensor;
297 : };
298 :
299 : /// creates user data of desired type
300 : template <typename TData, int dim>
301 : SmartPtr<CplUserData<TData,dim> > CreateConstUserData(number val, TData dummy);
302 :
303 : template <int dim>
304 0 : inline SmartPtr<CplUserData<number,dim> > CreateConstUserData(number val, number)
305 : {
306 0 : return make_sp(new ConstUserNumber<dim>(val));
307 : };
308 :
309 : template <int dim, int worldDim=dim>
310 0 : SmartPtr<CplUserData<MathVector<dim>,worldDim> > CreateConstUserData(number val, MathVector<dim>)
311 : {
312 0 : return make_sp(new ConstUserVector<dim,worldDim>(val));
313 : }
314 :
315 : template <int dim>
316 0 : SmartPtr<CplUserData<MathMatrix<dim,dim>,dim> > CreateConstUserData(number val, MathMatrix<dim,dim>)
317 : {
318 0 : return make_sp(new ConstUserMatrix<dim>(val));
319 : }
320 :
321 : template <int dim>
322 0 : SmartPtr<CplUserData<MathTensor<4,dim>,dim> > CreateConstUserData(number val, MathTensor<4,dim>)
323 : {
324 0 : return make_sp(new ConstUserTensor<4,dim>(val));
325 : }
326 :
327 : /// @}
328 :
329 : } /// end namespace ug
330 :
331 : #endif /* __H__UG__LIB_DISC__SPATIAL_DISC__USER_DATA__CONST_USER_DATA__ */
|