how to realize hybrid boundary conditions ( Dirichlet and Neumann B.C.)? #2991
-
Hi, I am a newer to mfem lib. I want to use two B.C.s in 1D differential problem. However, I can not get the right solution. I post my problem and code. Can anyone help me to resolve the problem? #include "mfem.hpp" using namespace std; int main(int argc, char *argv[]) int order = 1; double sx = 1.0; H1_FECollection fec(order, mesh.Dimension()); cout << "Number of finite element unknowns: " << fespace.GetTrueVSize() << endl; Array ess_tdof_list; if( mesh.bdr_attributes.Size()) LinearForm b(&fespace); ConstantCoefficient one(1.0); b.AddDomainIntegrator(new DomainLFIntegrator(one)); cout << "after adding boundary condition .... " << endl; b1.Assemble(); GridFunction x(&fespace); // need to satisfy BC BilinearForm a(&fespace); OperatorPtr A; cout << "Size of linear system : " << A->Height() << endl; GSSmoother M( (SparseMatrix&) (*A)); PCG(*A,M,B,X,1, 200, 1e-12,0.0); return 0; |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi @jsfeng-fudan, in order to specify different kinds of BC on different parts of the boundary you need to use bdr_attributes. Please see below for the solution to the minimal example you posted above #include "mfem.hpp"
using namespace std;
using namespace mfem;
double y_exact(const Vector & x)
{
return -x(0)*x(0)/2.0+1;
}
int main(int argc, char *argv[])
{
int order = 1;
double sx = 1.0;
Mesh mesh = Mesh::MakeCartesian1D(10, sx);
H1_FECollection fec(order, mesh.Dimension());
FiniteElementSpace fespace(&mesh, &fec);
cout << "Number of finite element unknowns: " << fespace.GetTrueVSize() << endl;
Array<int> ess_tdof_list;
Array<int> ess_bdr;
Array<int> newmann_bdr;
if( mesh.bdr_attributes.Size())
{
ess_bdr.SetSize(mesh.bdr_attributes.Max());
newmann_bdr.SetSize(mesh.bdr_attributes.Max());
ess_bdr[0] = 1;
ess_bdr[1] = 0;
newmann_bdr[0] = 0;
newmann_bdr[1] = 1;
fespace.GetEssentialTrueDofs(ess_bdr, ess_tdof_list);
}
LinearForm b(&fespace);
ConstantCoefficient one(1.0);
ConstantCoefficient negone(-1.0);
b.AddDomainIntegrator(new DomainLFIntegrator(one));
b.AddBoundaryIntegrator(new BoundaryLFIntegrator(negone),newmann_bdr);
b.Assemble();
GridFunction x(&fespace);
x = 0.0;
x.ProjectBdrCoefficient(one,ess_bdr);
BilinearForm a(&fespace);
a.AddDomainIntegrator(new DiffusionIntegrator(one));
a.Assemble();
OperatorPtr A;
Vector B, X;
a.FormLinearSystem(ess_tdof_list, x, b, A, X, B);
cout << "Size of linear system : " << A->Height() << endl;
GSSmoother M( (SparseMatrix&) (*A));
PCG(*A,M,B,X,1, 200, 1e-12,0.0);
a.RecoverFEMSolution(X,b,x);
char vishost[] = "localhost";
int visport = 19916;
socketstream sol_sock(vishost, visport);
sol_sock.precision(8);
sol_sock << "solution\n" << mesh << x << flush;
// exact solution
GridFunction y_ex(&fespace);
FunctionCoefficient Ex(y_exact);
y_ex.ProjectCoefficient(Ex);
socketstream exact_sock(vishost, visport);
exact_sock.precision(8);
exact_sock << "solution\n" << mesh << y_ex << flush;
return 0;
}
I hope this helps. Best, Socratis |
Beta Was this translation helpful? Give feedback.
-
please see #3121 |
Beta Was this translation helpful? Give feedback.
please see #3121