MFEM  v3.3 Finite element discretization library
coefficient.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
10 // Software Foundation) version 2.1 dated February 1999.
11
12 #ifndef MFEM_COEFFICIENT
13 #define MFEM_COEFFICIENT
14
15 #include "../config/config.hpp"
16 #include "../linalg/linalg.hpp"
17 #include "intrules.hpp"
18 #include "eltrans.hpp"
19
20 namespace mfem
21 {
22
23 class Mesh;
24
25 #ifdef MFEM_USE_MPI
26 class ParMesh;
27 #endif
28
29
30 /// Base class Coefficient that may optionally depend on time.
32 {
33 protected:
34  double time;
35
36 public:
37  Coefficient() { time = 0.; }
38
39  void SetTime(double t) { time = t; }
40  double GetTime() { return time; }
41
42  virtual double Eval(ElementTransformation &T,
43  const IntegrationPoint &ip) = 0;
44
46  const IntegrationPoint &ip, double t)
47  {
48  SetTime(t);
49  return Eval(T, ip);
50  }
51
52  virtual ~Coefficient() { }
53 };
54
55
56 /// Subclass constant coefficient.
58 {
59 public:
60  double constant;
61
62  /// c is value of constant function
63  explicit ConstantCoefficient(double c = 1.0) { constant=c; }
64
65  /// Evaluate the coefficient
66  virtual double Eval(ElementTransformation &T,
67  const IntegrationPoint &ip)
68  { return (constant); }
69 };
70
71 /// class for piecewise constant coefficient
73 {
74 private:
75  Vector constants;
76
77 public:
78
79  /// Constructs a piecewise constant coefficient in NumOfSubD subdomains
80  explicit PWConstCoefficient(int NumOfSubD = 0) : constants(NumOfSubD)
81  { constants = 0.0; }
82
83  /** c should be a vector defined by attributes, so for region with
84  attribute i c[i] is the coefficient in that region */
86  { constants.SetSize(c.Size()); constants=c; }
87
88  /// Member function to access or modify the value of the i-th constant
89  double &operator()(int i) { return constants(i-1); }
90
91  /// Set domain constants equal to the same constant c
92  void operator=(double c) { constants = c; }
93
94  /// Returns the number of constants
95  int GetNConst() { return constants.Size(); }
96
97  /// Evaluate the coefficient function
98  virtual double Eval(ElementTransformation &T,
99  const IntegrationPoint &ip);
100 };
101
102 /// class for C-function coefficient
104 {
105 protected:
106  double (*Function)(const Vector &);
107  double (*TDFunction)(const Vector &, double);
108
109 public:
110  /// Define a time-independent coefficient from a C-function
111  FunctionCoefficient(double (*f)(const Vector &))
112  {
113  Function = f;
114  TDFunction = NULL;
115  }
116
117  /// Define a time-dependent coefficient from a C-function
118  FunctionCoefficient(double (*tdf)(const Vector &, double))
119  {
120  Function = NULL;
121  TDFunction = tdf;
122  }
123
124  /// (DEPRECATED) Define a time-independent coefficient from a C-function
125  FunctionCoefficient(double (*f)(Vector &))
126  {
127  Function = reinterpret_cast<double(*)(const Vector&)>(f);
128  TDFunction = NULL;
129  }
130
131  /// (DEPRECATED) Define a time-dependent coefficient from a C-function
132  FunctionCoefficient(double (*tdf)(Vector &, double))
133  {
134  Function = NULL;
135  TDFunction = reinterpret_cast<double(*)(const Vector&,double)>(tdf);
136  }
137
138  /// Evaluate coefficient
139  virtual double Eval(ElementTransformation &T,
140  const IntegrationPoint &ip);
141 };
142
143 class GridFunction;
144
145 /// Coefficient defined by a GridFunction. This coefficient is mesh dependent.
147 {
148 private:
149  GridFunction *GridF;
150  int Component;
151
152 public:
153  /** Construct GridFunctionCoefficient from a given GridFunction
154  and optionally which component to use if it is a vector
155  gridfunction. */
157  { GridF = gf; Component = comp; }
158
159  void SetGridFunction(GridFunction *gf) { GridF = gf; }
160
161  virtual double Eval(ElementTransformation &T,
162  const IntegrationPoint &ip);
163 };
164
166 {
167 private:
168  Coefficient * Q1;
169  Coefficient * Q2;
170  double (*Transform1)(double);
171  double (*Transform2)(double,double);
172
173 public:
174  TransformedCoefficient (Coefficient * q,double (*F)(double))
175  : Q1(q), Transform1(F) { Q2 = 0; Transform2 = 0; }
177  double (*F)(double,double))
178  : Q1(q1), Q2(q2), Transform2(F) { Transform1 = 0; }
179
180  virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip);
181 };
182
183 /// Delta function coefficient
185 {
186 private:
187  double center[3], scale, tol;
188  Coefficient *weight;
189
190 public:
192  DeltaCoefficient(double x, double y, double s)
193  {
194  center[0] = x; center[1] = y; center[2] = 0.; scale = s; tol = 1e-12;
195  weight = NULL;
196  }
197  DeltaCoefficient(double x, double y, double z, double s)
198  {
199  center[0] = x; center[1] = y; center[2] = z; scale = s; tol = 1e-12;
200  weight = NULL;
201  }
202  void SetTol(double _tol) { tol = _tol; }
203  void SetWeight(Coefficient *w) { weight = w; }
204  const double *Center() { return center; }
205  double Scale() { return scale; }
207  Coefficient *Weight() { return weight; }
208  virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
209  { mfem_error("DeltaCoefficient::Eval"); return 0.; }
210  virtual ~DeltaCoefficient() { delete weight; }
211 };
212
213 /// Coefficient defined on a subset of domain or boundary attributes
215 {
216 private:
217  Coefficient *c;
218  Array<int> active_attr;
219
220 public:
222  { c = &_c; attr.Copy(active_attr); }
223
224  virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
225  { return active_attr[T.Attribute-1] ? c->Eval(T, ip, GetTime()) : 0.0; }
226 };
227
229 {
230 protected:
231  int vdim;
232  double time;
233
234 public:
235  VectorCoefficient(int vd) { vdim = vd; time = 0.; }
236
237  void SetTime(double t) { time = t; }
238  double GetTime() { return time; }
239
240  /// Returns dimension of the vector.
241  int GetVDim() { return vdim; }
242
243  virtual void Eval(Vector &V, ElementTransformation &T,
244  const IntegrationPoint &ip) = 0;
245
246  // General implementation using the Eval method for one IntegrationPoint.
247  // Can be overloaded for more efficient implementation.
248  virtual void Eval(DenseMatrix &M, ElementTransformation &T,
249  const IntegrationRule &ir);
250
251  virtual ~VectorCoefficient() { }
252 };
253
255 {
256 private:
257  Vector vec;
258 public:
260  : VectorCoefficient(v.Size()), vec(v) { }
262  virtual void Eval(Vector &V, ElementTransformation &T,
263  const IntegrationPoint &ip) { V = vec; }
264 };
265
267 {
268 private:
269  void (*Function)(const Vector &, Vector &);
270  void (*TDFunction)(const Vector &, double, Vector &);
271  Coefficient *Q;
272
273 public:
274  /// Construct a time-independent vector coefficient from a C-function
275  VectorFunctionCoefficient(int dim, void (*F)(const Vector &, Vector &),
276  Coefficient *q = NULL)
277  : VectorCoefficient(dim), Q(q)
278  {
279  Function = F;
280  TDFunction = NULL;
281  }
282
283  /// Construct a time-dependent vector coefficient from a C-function
285  void (*TDF)(const Vector &, double, Vector &),
286  Coefficient *q = NULL)
287  : VectorCoefficient(dim), Q(q)
288  {
289  Function = NULL;
290  TDFunction = TDF;
291  }
292
294  virtual void Eval(Vector &V, ElementTransformation &T,
295  const IntegrationPoint &ip);
296
298 };
299
300 /// Vector coefficient defined by an array of scalar coefficients.
302 {
303 private:
304  Array<Coefficient*> Coeff;
305
306 public:
307  /// Construct vector of dim coefficients.
308  explicit VectorArrayCoefficient(int dim);
309
310  /// Returns i'th coefficient.
311  Coefficient &GetCoeff(int i) { return *Coeff[i]; }
312
313  Coefficient **GetCoeffs() { return Coeff; }
314
315  /// Sets coefficient in the vector.
316  void Set(int i, Coefficient *c) { Coeff[i] = c; }
317
318  /// Evaluates i'th component of the vector.
320  { return Coeff[i]->Eval(T, ip, GetTime()); }
321
323  virtual void Eval(Vector &V, ElementTransformation &T,
324  const IntegrationPoint &ip);
325
326  /// Destroys vector coefficient.
327  virtual ~VectorArrayCoefficient();
328 };
329
330 /// Vector coefficient defined by a vector GridFunction
332 {
333 private:
334  GridFunction *GridFunc;
335
336 public:
338
339  virtual void Eval(Vector &V, ElementTransformation &T,
340  const IntegrationPoint &ip);
341
342  virtual void Eval(DenseMatrix &M, ElementTransformation &T,
343  const IntegrationRule &ir);
344
346 };
347
348 /// VectorCoefficient defined on a subset of domain or boundary attributes
350 {
351 private:
353  Array<int> active_attr;
354
355 public:
357  : VectorCoefficient(vc.GetVDim())
358  { c = &vc; attr.Copy(active_attr); }
359
360  virtual void Eval(Vector &V, ElementTransformation &T,
361  const IntegrationPoint &ip);
362
363  virtual void Eval(DenseMatrix &M, ElementTransformation &T,
364  const IntegrationRule &ir);
365 };
366
367
369 {
370 protected:
371  int vdim;
372  double time;
373
374 public:
375  explicit MatrixCoefficient(int dim) { vdim = dim; time = 0.; }
376
377  void SetTime(double t) { time = t; }
378  double GetTime() { return time; }
379
380  int GetVDim() { return vdim; }
381
382  virtual void Eval(DenseMatrix &K, ElementTransformation &T,
383  const IntegrationPoint &ip) = 0;
384
385  virtual ~MatrixCoefficient() { }
386 };
387
389 {
390 private:
391  void (*Function)(const Vector &, DenseMatrix &);
392  void (*TDFunction)(const Vector &, double, DenseMatrix &);
393
394 public:
395  /// Construct a time-independent matrix coefficient from a C-function
396  MatrixFunctionCoefficient(int dim, void (*F)(const Vector &, DenseMatrix &))
397  : MatrixCoefficient(dim)
398  {
399  Function = F;
400  TDFunction = NULL;
401  }
402
403  /// Construct a time-dependent matrix coefficient from a C-function
405  void (*TDF)(const Vector &, double, DenseMatrix &))
406  : MatrixCoefficient(dim)
407  {
408  Function = NULL;
409  TDFunction = TDF;
410  }
411
412  virtual void Eval(DenseMatrix &K, ElementTransformation &T,
413  const IntegrationPoint &ip);
414
416 };
417
419 {
420 private:
421  Array<Coefficient *> Coeff;
422
423 public:
424
425  explicit MatrixArrayCoefficient (int dim);
426
427  Coefficient &GetCoeff (int i, int j) { return *Coeff[i*vdim+j]; }
428
429  void Set(int i, int j, Coefficient * c) { Coeff[i*vdim+j] = c; }
430
431  double Eval(int i, int j, ElementTransformation &T, IntegrationPoint &ip)
432  { return Coeff[i*vdim+j] -> Eval(T, ip, GetTime()); }
433
434  virtual void Eval(DenseMatrix &K, ElementTransformation &T,
435  const IntegrationPoint &ip);
436
437  virtual ~MatrixArrayCoefficient();
438 };
439
440 /// MatrixCoefficient defined on a subset of domain or boundary attributes
442 {
443 private:
445  Array<int> active_attr;
446
447 public:
449  : MatrixCoefficient(mc.GetVDim())
450  { c = &mc; attr.Copy(active_attr); }
451
452  virtual void Eval(DenseMatrix &K, ElementTransformation &T,
453  const IntegrationPoint &ip);
454 };
455
456 /** Compute the Lp norm of a function f.
457  \f$\| f \|_{Lp} = ( \int_\Omega | f |^p d\Omega)^{1/p} \f$ */
458 double ComputeLpNorm(double p, Coefficient &coeff, Mesh &mesh,
459  const IntegrationRule *irs[]);
460
461 /** Compute the Lp norm of a vector function f = {f_i}_i=1...N.
462  \f$\| f \|_{Lp} = ( \sum_i \| f_i \|_{Lp}^p )^{1/p} \f$ */
463 double ComputeLpNorm(double p, VectorCoefficient &coeff, Mesh &mesh,
464  const IntegrationRule *irs[]);
465
466 #ifdef MFEM_USE_MPI
467 /** Compute the global Lp norm of a function f.
468  \f$\| f \|_{Lp} = ( \int_\Omega | f |^p d\Omega)^{1/p} \f$ */
469 double ComputeGlobalLpNorm(double p, Coefficient &coeff, ParMesh &pmesh,
470  const IntegrationRule *irs[]);
471
472 /** Compute the global Lp norm of a vector function f = {f_i}_i=1...N.
473  \f$\| f \|_{Lp} = ( \sum_i \| f_i \|_{Lp}^p )^{1/p} \f$ */
474 double ComputeGlobalLpNorm(double p, VectorCoefficient &coeff, ParMesh &pmesh,
475  const IntegrationRule *irs[]);
476 #endif
477
478 }
479
480 #endif
virtual void Eval(Vector &V, ElementTransformation &T, const IntegrationPoint &ip)
double Eval(ElementTransformation &T, const IntegrationPoint &ip, double t)
Definition: coefficient.hpp:45
void SetTol(double _tol)
Class for an integration rule - an Array of IntegrationPoint.
Definition: intrules.hpp:83
Vector coefficient defined by an array of scalar coefficients.
MatrixFunctionCoefficient(int dim, void(*F)(const Vector &, DenseMatrix &))
Construct a time-independent matrix coefficient from a C-function.
Class for grid function - Vector with associated FE space.
Definition: gridfunc.hpp:27
void SetWeight(Coefficient *w)
Subclass constant coefficient.
Definition: coefficient.hpp:57
VectorGridFunctionCoefficient(GridFunction *gf)
virtual void Eval(Vector &V, ElementTransformation &T, const IntegrationPoint &ip)=0
FunctionCoefficient(double(*tdf)(const Vector &, double))
Define a time-dependent coefficient from a C-function.
void SetSize(int s)
Resize the vector to size s.
Definition: vector.hpp:310
FunctionCoefficient(double(*f)(Vector &))
(DEPRECATED) Define a time-independent coefficient from a C-function
virtual void Eval(DenseMatrix &K, ElementTransformation &T, const IntegrationPoint &ip)=0
void Copy(Array &copy) const
Create a copy of the current array.
Definition: array.hpp:160
Coefficient defined by a GridFunction. This coefficient is mesh dependent.
double & operator()(int i)
Member function to access or modify the value of the i-th constant.
Definition: coefficient.hpp:89
Data type dense matrix using column-major storage.
Definition: densemat.hpp:22
Delta function coefficient.
int Size() const
Returns the size of the vector.
Definition: vector.hpp:106
virtual void Eval(Vector &V, ElementTransformation &T, const IntegrationPoint &ip)
Definition: coefficient.cpp:83
Coefficient & GetCoeff(int i, int j)
void Set(int i, int j, Coefficient *c)
VectorFunctionCoefficient(int dim, void(*TDF)(const Vector &, double, Vector &), Coefficient *q=NULL)
Construct a time-dependent vector coefficient from a C-function.
virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
Definition: coefficient.cpp:55
virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
void SetTime(double t)
DeltaCoefficient(double x, double y, double z, double s)
virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
Evaluate coefficient.
Definition: coefficient.cpp:31
double(* Function)(const Vector &)
virtual void Eval(DenseMatrix &K, ElementTransformation &T, const IntegrationPoint &ip)
VectorCoefficient defined on a subset of domain or boundary attributes.
int dim
Definition: ex3.cpp:47
double ComputeLpNorm(double p, Coefficient &coeff, Mesh &mesh, const IntegrationRule *irs[])
VectorConstantCoefficient(const Vector &v)
ConstantCoefficient(double c=1.0)
c is value of constant function
Definition: coefficient.hpp:63
const double * Center()
Coefficient defined on a subset of domain or boundary attributes.
Coefficient * Weight()
MatrixRestrictedCoefficient(MatrixCoefficient &mc, Array< int > &attr)
double Eval(int i, int j, ElementTransformation &T, IntegrationPoint &ip)
virtual ~VectorArrayCoefficient()
Destroys vector coefficient.
int GetVDim()
Returns dimension of the vector.
void SetTime(double t)
Definition: coefficient.hpp:39
void operator=(double c)
Set domain constants equal to the same constant c.
Definition: coefficient.hpp:92
virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
Evaluate the coefficient.
Definition: coefficient.hpp:66
double Eval(int i, ElementTransformation &T, IntegrationPoint &ip)
Evaluates i&#39;th component of the vector.
Base class Coefficient that may optionally depend on time.
Definition: coefficient.hpp:31
virtual ~Coefficient()
Definition: coefficient.hpp:52
void mfem_error(const char *msg)
Definition: error.cpp:106
FunctionCoefficient(double(*f)(const Vector &))
Define a time-independent coefficient from a C-function.
virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
Evaluate the coefficient function.
Definition: coefficient.cpp:24
void SetTime(double t)
virtual void Eval(Vector &V, ElementTransformation &T, const IntegrationPoint &ip)
double ComputeGlobalLpNorm(double p, Coefficient &coeff, ParMesh &pmesh, const IntegrationRule *irs[])
void SetGridFunction(GridFunction *gf)
PWConstCoefficient(int NumOfSubD=0)
Constructs a piecewise constant coefficient in NumOfSubD subdomains.
Definition: coefficient.hpp:80
MatrixCoefficient defined on a subset of domain or boundary attributes.
virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
Definition: coefficient.cpp:49
class for piecewise constant coefficient
Definition: coefficient.hpp:72
Class for integration point with weight.
Definition: intrules.hpp:25
FunctionCoefficient(double(*tdf)(Vector &, double))
(DEPRECATED) Define a time-dependent coefficient from a C-function
TransformedCoefficient(Coefficient *q, double(*F)(double))
int GetNConst()
Returns the number of constants.
Definition: coefficient.hpp:95
Coefficient & GetCoeff(int i)
Returns i&#39;th coefficient.
DeltaCoefficient(double x, double y, double s)
virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)=0
VectorArrayCoefficient(int dim)
Construct vector of dim coefficients.
VectorFunctionCoefficient(int dim, void(*F)(const Vector &, Vector &), Coefficient *q=NULL)
Construct a time-independent vector coefficient from a C-function.
class for C-function coefficient
MatrixFunctionCoefficient(int dim, void(*TDF)(const Vector &, double, DenseMatrix &))
Construct a time-dependent matrix coefficient from a C-function.
Vector data type.
Definition: vector.hpp:36
TransformedCoefficient(Coefficient *q1, Coefficient *q2, double(*F)(double, double))
Vector coefficient defined by a vector GridFunction.
VectorRestrictedCoefficient(VectorCoefficient &vc, Array< int > &attr)
virtual void Eval(DenseMatrix &K, ElementTransformation &T, const IntegrationPoint &ip)
GridFunctionCoefficient(GridFunction *gf, int comp=1)
virtual void Eval(Vector &V, ElementTransformation &T, const IntegrationPoint &ip)
double(* TDFunction)(const Vector &, double)
void Set(int i, Coefficient *c)
Sets coefficient in the vector.
virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
RestrictedCoefficient(Coefficient &_c, Array< int > &attr)