MFEM v2.0
coefficient.hpp
Go to the documentation of this file.
00001 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
00002 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
00003 // reserved. See file COPYRIGHT for details.
00004 //
00005 // This file is part of the MFEM library. For more information and source code
00006 // availability see http://mfem.googlecode.com.
00007 //
00008 // MFEM is free software; you can redistribute it and/or modify it under the
00009 // terms of the GNU Lesser General Public License (as published by the Free
00010 // Software Foundation) version 2.1 dated February 1999.
00011 
00012 #ifndef MFEM_COEFFICIENT
00013 #define MFEM_COEFFICIENT
00014 
00016 class Coefficient
00017 {
00018 public:
00019 
00020    virtual double Eval(ElementTransformation &T,
00021                        const IntegrationPoint &ip) = 0;
00022    virtual void Read(istream &in) = 0;
00023 
00024    virtual ~Coefficient() { };
00025 };
00026 
00027 
00029 class ConstantCoefficient : public Coefficient
00030 {
00031 public:
00032 
00033    double constant;
00034 
00036    explicit ConstantCoefficient(double c = 1.0) { constant=c; };
00037 
00039    virtual double Eval(ElementTransformation &T,
00040                        const IntegrationPoint &ip)
00041    { return(constant); };
00042 
00043    virtual void Read(istream &in) { in >> constant; };
00044 };
00045 
00047 class PWConstCoefficient : public Coefficient
00048 {
00049 private:
00050    Vector constants;
00051 
00052 public:
00053 
00055    explicit PWConstCoefficient(int NumOfSubD = 0) : constants(NumOfSubD)
00056    { constants = 0.0; };
00057 
00060    PWConstCoefficient(Vector &c)
00061    { constants.SetSize(c.Size()); constants=c; };
00062 
00064    double &operator()(int i) { return constants(i-1); };
00065 
00067    void operator=(double c) { constants = c; };
00068 
00070    int GetNConst() { return constants.Size(); };
00071 
00073    virtual double Eval(ElementTransformation &T,
00074                        const IntegrationPoint &ip);
00075 
00076    virtual void Read(istream &in);
00077 };
00078 
00080 class FunctionCoefficient : public Coefficient
00081 {
00082 private:
00083    double (*Function)(Vector &);
00084 
00085 public:
00086 
00088    FunctionCoefficient( double (*f)(Vector &) ) { Function=f; };
00089 
00091    virtual double Eval(ElementTransformation &T,
00092                        const IntegrationPoint &ip);
00093 
00094    virtual void Read(istream &in) { };
00095 };
00096 
00097 class GridFunction;
00098 
00101 class GridFunctionCoefficient : public Coefficient
00102 {
00103 private:
00104    GridFunction *GridF;
00105    int Component;
00106 
00107 public:
00111    GridFunctionCoefficient (GridFunction *gf, int comp = 1)
00112    { GridF = gf; Component = comp; }
00113 
00114    virtual double Eval(ElementTransformation &T,
00115                        const IntegrationPoint &ip);
00116 
00117    virtual void Read(istream &in)
00118    { mfem_error ("GridFunctionCoefficient::Read()"); }
00119 };
00120 
00121 class TransformedCoefficient : public Coefficient
00122 {
00123 private:
00124    Coefficient * Q1;
00125    Coefficient * Q2;
00126    double (*Transform1)(double);
00127    double (*Transform2)(double,double);
00128 
00129 public:
00130    TransformedCoefficient (Coefficient * q,double (*F)(double))
00131       : Q1(q), Transform1(F) { Q2 = 0; Transform2 = 0; }
00132    TransformedCoefficient (Coefficient * q1,Coefficient * q2,
00133                            double (*F)(double,double))
00134       : Q1(q1), Q2(q2), Transform2(F) { Transform1 = 0; }
00135 
00136    virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
00137    { if (Q2) return (*Transform2)(Q1 -> Eval(T,ip),Q2 -> Eval(T,ip));
00138       else return (*Transform1)(Q1 -> Eval(T,ip)); }
00139 
00140    virtual void Read(istream &in) { };
00141 };
00142 
00144 class DeltaCoefficient : public Coefficient
00145 {
00146 private:
00147    double center[3], scale, tol;
00148    Coefficient *weight;
00149 
00150 public:
00151    DeltaCoefficient();
00152    DeltaCoefficient(double x, double y, double s)
00153    { center[0] = x; center[1] = y; center[2] = 0.; scale = s; tol = 1e-12;
00154       weight = NULL; }
00155    DeltaCoefficient(double x, double y, double z, double s)
00156    { center[0] = x; center[1] = y; center[2] = z; scale = s; tol = 1e-12;
00157       weight = NULL; }
00158    void SetTol(double _tol) { tol = _tol; }
00159    void SetWeight(Coefficient *w) { weight = w; }
00160    const double *Center() { return center; }
00161    double Scale() { return scale; }
00162    double Tol() { return tol; }
00163    Coefficient *Weight() { return weight; }
00164    virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
00165    { mfem_error("DeltaCoefficient::Eval"); return 0.; }
00166    virtual void Read(istream &in) { }
00167    virtual ~DeltaCoefficient() { delete weight; }
00168 };
00169 
00171 class RestrictedCoefficient : public Coefficient
00172 {
00173 private:
00174    Coefficient *c;
00175    Array<int> active_attr;
00176 
00177 public:
00178    RestrictedCoefficient(Coefficient &_c, Array<int> &attr)
00179    { c = &_c; attr.Copy(active_attr); }
00180 
00181    virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
00182    { return active_attr[T.Attribute-1] ? c->Eval(T, ip) : 0.0; }
00183 
00184    virtual void Read(istream &in) { }
00185 };
00186 
00187 class VectorCoefficient
00188 {
00189 protected:
00190    int vdim;
00191 
00192 public:
00193 
00194    VectorCoefficient (int vd) { vdim = vd; };
00195 
00197    int GetVDim() { return vdim; };
00198 
00199    void Eval(DenseMatrix &M, ElementTransformation &T,
00200              const IntegrationRule &ir);
00201 
00202    virtual void Eval (Vector &V, ElementTransformation &T,
00203                       const IntegrationPoint &ip) = 0;
00204 
00205    virtual ~VectorCoefficient() { };
00206 };
00207 
00208 class VectorConstantCoefficient : public VectorCoefficient
00209 {
00210 private:
00211    Vector vec;
00212 public:
00213    VectorConstantCoefficient(const Vector &v)
00214       : VectorCoefficient(v.Size()), vec(v) { }
00215    virtual void Eval(Vector &V, ElementTransformation &T,
00216                      const IntegrationPoint &ip) { V = vec; }
00217 };
00218 
00219 class VectorFunctionCoefficient : public VectorCoefficient
00220 {
00221 private:
00222    void (*Function)(const Vector &, Vector &);
00223    Coefficient *Q;
00224 
00225 public:
00226    VectorFunctionCoefficient (
00227       int dim, void (*F)(const Vector &, Vector &), Coefficient *q = NULL)
00228       : VectorCoefficient (dim), Q(q) { Function = F; };
00229 
00230    virtual void Eval (Vector &V, ElementTransformation &T,
00231                       const IntegrationPoint &ip);
00232 
00233    virtual ~VectorFunctionCoefficient() { };
00234 };
00235 
00237 class VectorArrayCoefficient : public VectorCoefficient
00238 {
00239 private:
00240    Array<Coefficient*> Coeff;
00241 
00242 public:
00244    explicit VectorArrayCoefficient (int dim);
00245 
00247    Coefficient & GetCoeff (int i) { return *Coeff[i]; }
00248 
00249    Coefficient ** GetCoeffs () { return Coeff; }
00250 
00252    void Set (int i, Coefficient * c) { Coeff[i] = c; }
00253 
00255    double Eval (int i, ElementTransformation &T, IntegrationPoint &ip)
00256    { return Coeff[i] -> Eval(T,ip); }
00257 
00258    virtual void Eval (Vector &V, ElementTransformation &T,
00259                       const IntegrationPoint &ip);
00260 
00262    void Read (int i, istream &in)  { Coeff[i] -> Read(in); }
00263 
00265    virtual ~VectorArrayCoefficient();
00266 };
00267 
00269 class VectorGridFunctionCoefficient : public VectorCoefficient
00270 {
00271 private:
00272    GridFunction *GridFunc;
00273 
00274 public:
00275    VectorGridFunctionCoefficient (GridFunction *gf);
00276 
00277    virtual void Eval (Vector &V, ElementTransformation &T,
00278                       const IntegrationPoint &ip);
00279 
00280    virtual ~VectorGridFunctionCoefficient() { };
00281 };
00282 
00284 class VectorRestrictedCoefficient : public VectorCoefficient
00285 {
00286 private:
00287    VectorCoefficient *c;
00288    Array<int> active_attr;
00289 
00290 public:
00291    VectorRestrictedCoefficient(VectorCoefficient &vc, Array<int> &attr)
00292       : VectorCoefficient(vc.GetVDim())
00293    { c = &vc; attr.Copy(active_attr); }
00294 
00295    virtual void Eval(Vector &V, ElementTransformation &T,
00296                      const IntegrationPoint &ip);
00297 };
00298 
00299 
00300 class MatrixCoefficient
00301 {
00302 protected:
00303    int vdim;
00304 
00305 public:
00306    explicit MatrixCoefficient (int dim) { vdim = dim; };
00307 
00308    int GetVDim() { return vdim; };
00309 
00310    virtual void Eval (DenseMatrix &K, ElementTransformation &T,
00311                       const IntegrationPoint &ip) = 0;
00312 
00313    virtual ~MatrixCoefficient() { };
00314 };
00315 
00316 class MatrixFunctionCoefficient : public MatrixCoefficient
00317 {
00318 private:
00319    void (*Function)(const Vector &, DenseMatrix &);
00320 
00321 public:
00322    MatrixFunctionCoefficient (int dim, void (*F)(const Vector &,
00323                                                  DenseMatrix &))
00324       : MatrixCoefficient (dim) { Function = F; }
00325 
00326    virtual void Eval (DenseMatrix &K, ElementTransformation &T,
00327                       const IntegrationPoint &ip);
00328 
00329    virtual ~MatrixFunctionCoefficient() { }
00330 };
00331 
00332 class MatrixArrayCoefficient : public MatrixCoefficient
00333 {
00334 private:
00335    Array<Coefficient *> Coeff;
00336 
00337 public:
00338 
00339    explicit MatrixArrayCoefficient (int dim);
00340 
00341    Coefficient & GetCoeff (int i, int j) { return *Coeff[i*vdim+j]; }
00342 
00343    void Set (int i, int j, Coefficient * c) { Coeff[i*vdim+j] = c; }
00344 
00345    double Eval (int i, int j, ElementTransformation &T,
00346                 IntegrationPoint &ip)
00347    { return Coeff[i*vdim+j] -> Eval(T, ip); }
00348 
00349    virtual void Eval (DenseMatrix &K, ElementTransformation &T,
00350                       const IntegrationPoint &ip);
00351 
00352    virtual ~MatrixArrayCoefficient();
00353 };
00354 
00355 double ComputeLpNorm(double p, Coefficient &coeff, Mesh &mesh,
00356                      const IntegrationRule *irs[]);
00357 
00358 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines