Skip to content

Commit

Permalink
Testing Brezzi Formulation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroMunozManterola committed Apr 18, 2022
1 parent c34a1b4 commit d0b992f
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 113 deletions.
17 changes: 9 additions & 8 deletions src/maxwell1D/BilinearIntegrators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ static void PADGTraceSetup2D(const int Q1D,
const double v0 = const_v ? V(0,0,0) : V(0,q,f);
const double v1 = const_v ? V(1,0,0) : V(1,q,f);
const double dot = n(q,0,f) * v0 + n(q,1,f) * v1;
const double negdot = n(q, 0, f) * v0 - n(q, 1, f) * v1;
const double abs = dot > 0.0 ? dot : -dot;
const double w = W[q] * r * d(q,f);
qd(q,0,0,f) = w * (alpha / 2 * dot + gamma * dot);
Expand Down Expand Up @@ -1290,7 +1291,7 @@ void MaxwellDGTraceIntegrator::AssembleFaceMatrix(const FiniteElement& el1,
{
int dim, ndof1, ndof2;

double un, a, b, g, w;
double un, a, b, w;

dim = el1.GetDim();
ndof1 = el1.GetDof();
Expand Down Expand Up @@ -1353,11 +1354,12 @@ void MaxwellDGTraceIntegrator::AssembleFaceMatrix(const FiniteElement& el1,
{
CalcOrtho(Trans.Jacobian(), nor);
}


vu(0) = 1.0;
un = vu * nor;

a = 0.5 * alpha * un;
//b = beta * fabs(un);
g = gamma * un;
b = beta;
// note: if |alpha/2|==|beta| then |a|==|b|, i.e. (a==b) or (a==-b)
// and therefore two blocks in the element matrix contribution
// (from the current quadrature point) are 0
Expand All @@ -1374,11 +1376,10 @@ void MaxwellDGTraceIntegrator::AssembleFaceMatrix(const FiniteElement& el1,
rho_p = rho->Eval(*Trans.Elem1, eip1);
}
a *= rho_p;
//b *= rho_p;
g *= rho_p;
b *= rho_p;
}

w = ip.weight * (a + g);
w = ip.weight * (a + b);
if (w != 0.0)
{
for (int i = 0; i < ndof1; i++)
Expand All @@ -1399,7 +1400,7 @@ void MaxwellDGTraceIntegrator::AssembleFaceMatrix(const FiniteElement& el1,
elmat(ndof1 + i, j) -= w * shape2_(i) * shape1_(j);
}

w = ip.weight * (g - a);
w = ip.weight * (b - a);
if (w != 0.0)
{
for (int i = 0; i < ndof2; i++)
Expand Down
16 changes: 2 additions & 14 deletions src/maxwell1D/BilinearIntegrators.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,12 @@ class MaxwellDGTraceIntegrator : public BilinearFormIntegrator
//When explicitly undeclared, rho = 1.0;
MaxwellDGTraceIntegrator(VectorCoefficient& u_, double a)
{
rho = NULL; u = &u_; alpha = a; beta = 0.5 * a; gamma = 0.0;
rho = NULL; u = &u_; alpha = a; beta = 0.5 * a;
}

MaxwellDGTraceIntegrator(VectorCoefficient& u_, double a, double b)
{
rho = NULL; u = &u_; alpha = a; beta = b; gamma = 0.0;
}

MaxwellDGTraceIntegrator(VectorCoefficient& u_, double a, double b, double g)
{
rho = NULL; u = &u_; alpha = a; beta = b; gamma = g;
rho = NULL; u = &u_; alpha = a; beta = b;
}

MaxwellDGTraceIntegrator(Coefficient& rho_, VectorCoefficient& u_,
Expand All @@ -34,13 +29,6 @@ class MaxwellDGTraceIntegrator : public BilinearFormIntegrator
rho = &rho_; u = &u_; alpha = a; beta = b;
}

MaxwellDGTraceIntegrator(Coefficient& rho_, VectorCoefficient& u_,
double a, double b, double g)
{
rho = &rho_; u = &u_; alpha = a; beta = b; gamma = g;
}

using BilinearFormIntegrator::AssembleFaceMatrix;
virtual void AssembleFaceMatrix(const FiniteElement& el1,
const FiniteElement& el2,
FaceElementTransformations& Trans,
Expand Down
107 changes: 66 additions & 41 deletions src/maxwell1D/FE_Evolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ void FE_Evolution::addFluxOperator(
switch (fluxType) {
case Upwind:
form->AddInteriorFaceIntegrator(
new MaxwellDGTraceIntegrator(n[d], abgFace[Alpha], abgFace[Beta], abgFace[Gamma]));
new MaxwellDGTraceIntegrator(n[d], abgFace[Alpha], abgFace[Beta]));
form->AddBdrFaceIntegrator(
new MaxwellDGTraceIntegrator(n[d], abgBdr[Alpha], abgBdr[Beta], abgBdr[Gamma]));
new MaxwellDGTraceIntegrator(n[d], abgBdr[Alpha], abgBdr[Beta]));
break;

case Centered:
Expand All @@ -64,13 +64,6 @@ void FE_Evolution::addFluxOperator(
break;

}
form->Assemble();
form->Finalize();
}


void FE_Evolution::finalizeBilinearForm(std::unique_ptr<BilinearForm>& form) const
{
form->Assemble(0);
form->Finalize(0);
}
Expand All @@ -79,12 +72,11 @@ void FE_Evolution::initializeBilinearForms()
{
double alpha = 0.0;
double beta = 0.0;
double gamma = 0.0;
ConstantCoefficient coeff;
Vector abgIntFaceE({ alpha, beta, gamma });
Vector abgBdrFaceE({ alpha, beta, gamma });
Vector abgIntFaceH({ alpha, beta, gamma });
Vector abgBdrFaceH({ alpha, beta, gamma });
Vector abIntFaceE({alpha, beta});
Vector abBdrFaceE({alpha, beta});
Vector abIntFaceH({alpha, beta});
Vector abBdrFaceH({alpha, beta});

KEE_ = std::make_unique<BilinearForm>(fes_);
KEH_ = std::make_unique<BilinearForm>(fes_);
Expand All @@ -94,53 +86,86 @@ void FE_Evolution::initializeBilinearForms()
switch (fluxType) {
case Upwind:

abgIntFaceE[Gamma] = 1.0;

addFluxOperator(KEE_, X, abgIntFaceE, abgBdrFaceE);

addFluxOperator(KHH_, X, abgIntFaceH, abgBdrFaceH);
switch (bdrCond) {
case PEC:
abIntFaceE[Alpha] = 0.0;
abIntFaceH[Alpha] = 1.0;
abBdrFaceE[Alpha] = 0.0;
abBdrFaceH[Alpha] = 1.0;
abIntFaceE[Beta] = 0.5;
abIntFaceH[Beta] = 0.0;
abBdrFaceE[Beta] = 0.5;
abBdrFaceH[Beta] = 0.0;
break;
default:
abIntFaceE[Beta] = -1.0;
abIntFaceH[Beta] = -1.0;
abBdrFaceE[Beta] = -1.0;
abBdrFaceH[Beta] = -1.0;
break;
}

abgIntFaceE[Gamma] = 1.0 * 0.5;
coeff = ConstantCoefficient(-1.0);

addFluxOperator(KEE_, X, abIntFaceE, abBdrFaceE);

addDerivativeOperator(KEH_, X, coeff);
addFluxOperator(KEH_, X, abgIntFaceE, abgBdrFaceE);
addFluxOperator(KEH_, X, abIntFaceE, abBdrFaceE);

switch (bdrCond) {
case PEC:
abIntFaceE[Alpha] = 1.0;
abIntFaceH[Alpha] = 0.0;
abBdrFaceE[Alpha] = 1.0;
abBdrFaceH[Alpha] = 0.0;
abIntFaceE[Beta] = 0.0;
abIntFaceH[Beta] = 0.5;
abBdrFaceE[Beta] = 0.0;
abBdrFaceH[Beta] = 0.5;
break;
default:
throw std::exception("Input a valid Boundary Condition.");
break;
}

addFluxOperator(KHH_, X, abIntFaceH, abBdrFaceH);

addDerivativeOperator(KHE_, X, coeff);
addFluxOperator(KHE_, X, abgIntFaceH, abgBdrFaceH);
addFluxOperator(KHE_, X, abIntFaceH, abBdrFaceH);

break;

case Centered:

switch (bdrCond) {
case PEC:
abgIntFaceE[Alpha] = -1.0;
abgIntFaceH[Alpha] = -1.0;
abgBdrFaceE[Alpha] = 0.0;
abgBdrFaceH[Alpha] = -2.0;
abIntFaceE[Alpha] = -1.0;
abIntFaceH[Alpha] = -1.0;
abBdrFaceE[Alpha] = 0.0;
abBdrFaceH[Alpha] = -2.0;
break;
case PMC:
abgIntFaceE[Alpha] = -1.0;
abgIntFaceH[Alpha] = -1.0;
abgBdrFaceE[Alpha] = -2.0;
abgBdrFaceH[Alpha] = 0.0;
abIntFaceE[Alpha] = -1.0;
abIntFaceH[Alpha] = -1.0;
abBdrFaceE[Alpha] = -2.0;
abBdrFaceH[Alpha] = 0.0;
break;
case SMA:
throw std::exception("SMA not available for Centered Flux.");
default:
abgIntFaceE[Alpha] = -1.0;
abgIntFaceH[Alpha] = -1.0;
abgBdrFaceE[Alpha] = -1.0;
abgBdrFaceH[Alpha] = -1.0;
throw std::exception("Input a valid Boundary Condition.");
break;
}

coeff = ConstantCoefficient(1.0);

addDerivativeOperator(KEH_, X, coeff);
addFluxOperator(KEH_, X, abgIntFaceE, abgBdrFaceE);
addFluxOperator(KEH_, X, abIntFaceE, abBdrFaceE);

addDerivativeOperator(KHE_, X, coeff);
addFluxOperator(KHE_, X, abgIntFaceH, abgBdrFaceH);
addFluxOperator(KHE_, X, abIntFaceH, abBdrFaceH);

break;
}
}

Expand All @@ -159,17 +184,17 @@ void FE_Evolution::Mult(const Vector& x, Vector& y) const
switch (fluxType) {
case Upwind:

// Update E. dE/dt = M^{-1} * (-S * H + 0.5 * ([H] - [E])).
// Update E. dE/dt = M^{-1} * (-S * H + nx * {H} + 0.5 * [E])).
KEH_->Mult(hOld, auxRHS);
KEE_->Mult(eOld, auxFluxdE);
auxRHS.Add(-1.0, auxFluxdE);
auxRHS.Add(1.0, auxFluxdE);
MInv_->Mult(auxRHS, eNew);

// Update H. dH/dt = M^{-1} * (-S * E + 0.5 * ([E] - [H])).
// Update H. dH/dt = M^{-1} * (-S * E + nx * {E} + 0.5 * [H])).
KHE_->Mult(eOld, auxRHS);
KHH_->Mult(hOld, auxFluxdH);
auxRHS.Add(-1.0, auxFluxdH);
MInv_->Mult(auxRHS, eNew);
auxRHS.Add(1.0, auxFluxdH);
MInv_->Mult(auxRHS, hNew);

break;

Expand Down
7 changes: 2 additions & 5 deletions src/maxwell1D/FE_Evolution.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const FluxType Upwind = 1; //Strong form

const Factor Alpha = 0;
const Factor Beta = 1;
const Factor Gamma = 2;

const BdrCond PEC = 0;
const BdrCond PMC = 1;
Expand All @@ -29,7 +28,7 @@ class FE_Evolution : public TimeDependentOperator {
public:

static const std::size_t numberOfFieldComponents = 2;
FluxType fluxType = Centered;
FluxType fluxType = Upwind;
BdrCond bdrCond = PEC;

FE_Evolution(FiniteElementSpace* fes);
Expand All @@ -41,7 +40,7 @@ class FE_Evolution : public TimeDependentOperator {
FiniteElementSpace* fes_;

std::unique_ptr<BilinearForm> MInv_;
std::unique_ptr<BilinearForm> KEE_;
std::unique_ptr<BilinearForm> KEE_; //K - Time Diff. (E) - Applied on (E)
std::unique_ptr<BilinearForm> KEH_;
std::unique_ptr<BilinearForm> KHE_;
std::unique_ptr<BilinearForm> KHH_;
Expand All @@ -53,8 +52,6 @@ class FE_Evolution : public TimeDependentOperator {
void addFluxOperator(
std::unique_ptr<BilinearForm>& form,
const Direction& d,const Vector& abgFace,const Vector& abgBdr) const;
void FE_Evolution::finalizeBilinearForm(
std::unique_ptr<BilinearForm>& form) const;
void FE_Evolution::initializeBilinearForms();

};
Expand Down
82 changes: 41 additions & 41 deletions test/maxwell1D/TestBilinearIntegrators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,44 @@ class TestMaxwellDGTrace : public ::testing::Test {

};

TEST_F(TestMaxwellDGTrace, DGTraces)
{
const int order = 1;
const int dimension = 1;
const double tol = 1e-3;
Mesh mesh = Mesh::MakeCartesian1D(10);
FiniteElementCollection* fec = new DG_FECollection(order, dimension, BasisType::GaussLobatto);
FiniteElementSpace* fes = new FiniteElementSpace(&mesh, fec);
BilinearForm oldDGTrace(fes);
BilinearForm maxwellDGTrace(fes);

std::vector<VectorConstantCoefficient> n = {
VectorConstantCoefficient(Vector({1.0})),
};

const double alpha = 1.0;
const double beta = 0.0;
const double gamma = 0.0;

oldDGTrace.AddInteriorFaceIntegrator(new DGTraceIntegrator(n[0], alpha, beta));
oldDGTrace.AddBdrFaceIntegrator(new DGTraceIntegrator(n[0], alpha, beta));
oldDGTrace.Assemble();
oldDGTrace.Finalize();

maxwellDGTrace.AddInteriorFaceIntegrator(new MaxwellDGTraceIntegrator(n[0], alpha, beta, gamma));
maxwellDGTrace.AddBdrFaceIntegrator(new MaxwellDGTraceIntegrator(n[0], alpha, beta, gamma));
maxwellDGTrace.Assemble();
maxwellDGTrace.Finalize();

auto oldDGMatrix = oldDGTrace.SpMat().ToDenseMatrix();
auto maxwellDGMatrix = maxwellDGTrace.SpMat().ToDenseMatrix();

ASSERT_EQ(oldDGMatrix->Height(), maxwellDGMatrix->Width());
ASSERT_EQ(oldDGMatrix->Height(), maxwellDGMatrix->Height());

for (int i = 0; i < oldDGMatrix->Width(); i++) {
for (int j = 0; j < oldDGMatrix->Height(); j++){
EXPECT_NEAR(maxwellDGMatrix->Elem(i, j), oldDGMatrix->Elem(i, j), tol);
}
}
}
//TEST_F(TestMaxwellDGTrace, DGTraces)
//{
// const int order = 1;
// const int dimension = 1;
// const double tol = 1e-3;
// Mesh mesh = Mesh::MakeCartesian1D(10);
// FiniteElementCollection* fec = new DG_FECollection(order, dimension, BasisType::GaussLobatto);
// FiniteElementSpace* fes = new FiniteElementSpace(&mesh, fec);
// BilinearForm oldDGTrace(fes);
// BilinearForm maxwellDGTrace(fes);
//
// std::vector<VectorConstantCoefficient> n = {
// VectorConstantCoefficient(Vector({1.0})),
// };
//
// const double alpha = 1.0;
// const double beta = 0.0;
// const double gamma = 0.0;
//
// oldDGTrace.AddInteriorFaceIntegrator(new DGTraceIntegrator(n[0], alpha, beta));
// oldDGTrace.AddBdrFaceIntegrator(new DGTraceIntegrator(n[0], alpha, beta));
// oldDGTrace.Assemble();
// oldDGTrace.Finalize();
//
// maxwellDGTrace.AddInteriorFaceIntegrator(new MaxwellDGTraceIntegrator(n[0], alpha, beta, gamma));
// maxwellDGTrace.AddBdrFaceIntegrator(new MaxwellDGTraceIntegrator(n[0], alpha, beta, gamma));
// maxwellDGTrace.Assemble();
// maxwellDGTrace.Finalize();
//
// auto oldDGMatrix = oldDGTrace.SpMat().ToDenseMatrix();
// auto maxwellDGMatrix = maxwellDGTrace.SpMat().ToDenseMatrix();
//
// ASSERT_EQ(oldDGMatrix->Height(), maxwellDGMatrix->Width());
// ASSERT_EQ(oldDGMatrix->Height(), maxwellDGMatrix->Height());
//
// for (int i = 0; i < oldDGMatrix->Width(); i++) {
// for (int j = 0; j < oldDGMatrix->Height(); j++){
// EXPECT_NEAR(maxwellDGMatrix->Elem(i, j), oldDGMatrix->Elem(i, j), tol);
// }
// }
//}
Loading

0 comments on commit d0b992f

Please sign in to comment.