MFEM  v3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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.googlecode.com.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
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 
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 
58 {
59 public:
60  double constant;
61 
63  explicit ConstantCoefficient(double c = 1.0) { constant=c; }
64 
66  virtual double Eval(ElementTransformation &T,
67  const IntegrationPoint &ip)
68  { return(constant); }
69 };
70 
73 {
74 private:
75  Vector constants;
76 
77 public:
78 
80  explicit PWConstCoefficient(int NumOfSubD = 0) : constants(NumOfSubD)
81  { constants = 0.0; }
82 
86  { constants.SetSize(c.Size()); constants=c; }
87 
89  double &operator()(int i) { return constants(i-1); }
90 
92  void operator=(double c) { constants = c; }
93 
95  int GetNConst() { return constants.Size(); }
96 
98  virtual double Eval(ElementTransformation &T,
99  const IntegrationPoint &ip);
100 };
101 
104 {
105 protected:
106  double (*Function)(Vector &);
107  double (*TDFunction)(Vector &, double);
108 
109 public:
111  FunctionCoefficient(double (*f)(Vector &))
112  {
113  Function = f;
114  TDFunction = NULL;
115  }
116 
118  FunctionCoefficient(double (*tdf)(Vector &, double))
119  {
120  Function = NULL;
121  TDFunction = tdf;
122  }
123 
125  virtual double Eval(ElementTransformation &T,
126  const IntegrationPoint &ip);
127 };
128 
129 class GridFunction;
130 
133 {
134 private:
135  GridFunction *GridF;
136  int Component;
137 
138 public:
143  { GridF = gf; Component = comp; }
144 
145  void SetGridFunction(GridFunction *gf) { GridF = gf; }
146 
147  virtual double Eval(ElementTransformation &T,
148  const IntegrationPoint &ip);
149 };
150 
152 {
153 private:
154  Coefficient * Q1;
155  Coefficient * Q2;
156  double (*Transform1)(double);
157  double (*Transform2)(double,double);
158 
159 public:
160  TransformedCoefficient (Coefficient * q,double (*F)(double))
161  : Q1(q), Transform1(F) { Q2 = 0; Transform2 = 0; }
163  double (*F)(double,double))
164  : Q1(q1), Q2(q2), Transform2(F) { Transform1 = 0; }
165 
166  virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip);
167 };
168 
171 {
172 private:
173  double center[3], scale, tol;
174  Coefficient *weight;
175 
176 public:
178  DeltaCoefficient(double x, double y, double s)
179  { center[0] = x; center[1] = y; center[2] = 0.; scale = s; tol = 1e-12;
180  weight = NULL; }
181  DeltaCoefficient(double x, double y, double z, double s)
182  { center[0] = x; center[1] = y; center[2] = z; scale = s; tol = 1e-12;
183  weight = NULL; }
184  void SetTol(double _tol) { tol = _tol; }
185  void SetWeight(Coefficient *w) { weight = w; }
186  const double *Center() { return center; }
187  double Scale() { return scale; }
188  double Tol() { return tol; }
189  Coefficient *Weight() { return weight; }
190  virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
191  { mfem_error("DeltaCoefficient::Eval"); return 0.; }
192  virtual ~DeltaCoefficient() { delete weight; }
193 };
194 
197 {
198 private:
199  Coefficient *c;
200  Array<int> active_attr;
201 
202 public:
204  { c = &_c; attr.Copy(active_attr); }
205 
206  virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
207  { return active_attr[T.Attribute-1] ? c->Eval(T, ip, GetTime()) : 0.0; }
208 };
209 
211 {
212 protected:
213  int vdim;
214  double time;
215 
216 public:
217  VectorCoefficient(int vd) { vdim = vd; time = 0.; }
218 
219  void SetTime(double t) { time = t; }
220  double GetTime() { return time; }
221 
223  int GetVDim() { return vdim; }
224 
225  virtual void Eval(Vector &V, ElementTransformation &T,
226  const IntegrationPoint &ip) = 0;
227 
228  // General implementation using the Eval method for one IntegrationPoint.
229  // Can be overloaded for more efficient implementation.
230  virtual void Eval(DenseMatrix &M, ElementTransformation &T,
231  const IntegrationRule &ir);
232 
233  virtual ~VectorCoefficient() { }
234 };
235 
237 {
238 private:
239  Vector vec;
240 public:
242  : VectorCoefficient(v.Size()), vec(v) { }
244  virtual void Eval(Vector &V, ElementTransformation &T,
245  const IntegrationPoint &ip) { V = vec; }
246 };
247 
249 {
250 private:
251  void (*Function)(const Vector &, Vector &);
252  void (*TDFunction)(const Vector &, double, Vector &);
253  Coefficient *Q;
254 
255 public:
257  VectorFunctionCoefficient(int dim, void (*F)(const Vector &, Vector &),
258  Coefficient *q = NULL)
259  : VectorCoefficient(dim), Q(q)
260  {
261  Function = F;
262  TDFunction = NULL;
263  }
264 
267  void (*TDF)(const Vector &, double, Vector &),
268  Coefficient *q = NULL)
269  : VectorCoefficient(dim), Q(q)
270  {
271  Function = NULL;
272  TDFunction = TDF;
273  }
274 
276  virtual void Eval(Vector &V, ElementTransformation &T,
277  const IntegrationPoint &ip);
278 
280 };
281 
284 {
285 private:
286  Array<Coefficient*> Coeff;
287 
288 public:
290  explicit VectorArrayCoefficient(int dim);
291 
293  Coefficient &GetCoeff(int i) { return *Coeff[i]; }
294 
295  Coefficient **GetCoeffs() { return Coeff; }
296 
298  void Set(int i, Coefficient *c) { Coeff[i] = c; }
299 
302  { return Coeff[i]->Eval(T, ip, GetTime()); }
303 
305  virtual void Eval(Vector &V, ElementTransformation &T,
306  const IntegrationPoint &ip);
307 
309  virtual ~VectorArrayCoefficient();
310 };
311 
314 {
315 private:
316  GridFunction *GridFunc;
317 
318 public:
320 
321  virtual void Eval(Vector &V, ElementTransformation &T,
322  const IntegrationPoint &ip);
323 
324  virtual void Eval(DenseMatrix &M, ElementTransformation &T,
325  const IntegrationRule &ir);
326 
328 };
329 
332 {
333 private:
335  Array<int> active_attr;
336 
337 public:
339  : VectorCoefficient(vc.GetVDim())
340  { c = &vc; attr.Copy(active_attr); }
341 
342  virtual void Eval(Vector &V, ElementTransformation &T,
343  const IntegrationPoint &ip);
344 
345  virtual void Eval(DenseMatrix &M, ElementTransformation &T,
346  const IntegrationRule &ir);
347 };
348 
349 
351 {
352 protected:
353  int vdim;
354  double time;
355 
356 public:
357  explicit MatrixCoefficient(int dim) { vdim = dim; time = 0.; }
358 
359  void SetTime(double t) { time = t; }
360  double GetTime() { return time; }
361 
362  int GetVDim() { return vdim; }
363 
364  virtual void Eval(DenseMatrix &K, ElementTransformation &T,
365  const IntegrationPoint &ip) = 0;
366 
367  virtual ~MatrixCoefficient() { }
368 };
369 
371 {
372 private:
373  void (*Function)(const Vector &, DenseMatrix &);
374  void (*TDFunction)(const Vector &, double, DenseMatrix &);
375 
376 public:
378  MatrixFunctionCoefficient(int dim, void (*F)(const Vector &, DenseMatrix &))
379  : MatrixCoefficient(dim)
380  {
381  Function = F;
382  TDFunction = NULL;
383  }
384 
387  void (*TDF)(const Vector &, double, DenseMatrix &))
388  : MatrixCoefficient(dim)
389  {
390  Function = NULL;
391  TDFunction = TDF;
392  }
393 
394  virtual void Eval(DenseMatrix &K, ElementTransformation &T,
395  const IntegrationPoint &ip);
396 
398 };
399 
401 {
402 private:
403  Array<Coefficient *> Coeff;
404 
405 public:
406 
407  explicit MatrixArrayCoefficient (int dim);
408 
409  Coefficient &GetCoeff (int i, int j) { return *Coeff[i*vdim+j]; }
410 
411  void Set(int i, int j, Coefficient * c) { Coeff[i*vdim+j] = c; }
412 
413  double Eval(int i, int j, ElementTransformation &T, IntegrationPoint &ip)
414  { return Coeff[i*vdim+j] -> Eval(T, ip, GetTime()); }
415 
416  virtual void Eval(DenseMatrix &K, ElementTransformation &T,
417  const IntegrationPoint &ip);
418 
419  virtual ~MatrixArrayCoefficient();
420 };
421 
424 double ComputeLpNorm(double p, Coefficient &coeff, Mesh &mesh,
425  const IntegrationRule *irs[]);
426 
429 double ComputeLpNorm(double p, VectorCoefficient &coeff, Mesh &mesh,
430  const IntegrationRule *irs[]);
431 
432 #ifdef MFEM_USE_MPI
433 
435 double ComputeGlobalLpNorm(double p, Coefficient &coeff, ParMesh &pmesh,
436  const IntegrationRule *irs[]);
437 
440 double ComputeGlobalLpNorm(double p, VectorCoefficient &coeff, ParMesh &pmesh,
441  const IntegrationRule *irs[]);
442 #endif
443 
444 }
445 
446 #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 integration rule.
Definition: intrules.hpp:63
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:26
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
void SetSize(int s)
Resizes the vector if the new size is different.
Definition: vector.hpp:248
FunctionCoefficient(double(*f)(Vector &))
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:155
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.
Definition: densemat.hpp:22
Delta function coefficient.
int Size() const
Returns the size of the vector.
Definition: vector.hpp:76
virtual void Eval(Vector &V, ElementTransformation &T, const IntegrationPoint &ip)
Definition: coefficient.cpp:79
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:51
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
virtual void Eval(DenseMatrix &K, ElementTransformation &T, const IntegrationPoint &ip)
VectorCoefficient defined on a subset of domain or boundary attributes.
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()
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(* Function)(Vector &)
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:23
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
virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
Definition: coefficient.cpp:45
class for piecewise constant coefficient
Definition: coefficient.hpp:72
Class for integration point with weight.
Definition: intrules.hpp:25
FunctionCoefficient(double(*tdf)(Vector &, double))
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.
Definition: coefficient.cpp:96
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:29
TransformedCoefficient(Coefficient *q1, Coefficient *q2, double(*F)(double, double))
Vector coefficient defined by a vector GridFunction.
VectorRestrictedCoefficient(VectorCoefficient &vc, Array< int > &attr)
GridFunctionCoefficient(GridFunction *gf, int comp=1)
virtual void Eval(Vector &V, ElementTransformation &T, const IntegrationPoint &ip)
void Set(int i, Coefficient *c)
Sets coefficient in the vector.
virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
double(* TDFunction)(Vector &, double)
RestrictedCoefficient(Coefficient &_c, Array< int > &attr)