This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Converting NonlinearVariationalProblem with periodic BC, from fenics v1.6 to v2017.1 in C++

0 votes

I am struggling how to convert my NonlinearVariationalProblem that uses periodic boundary condtiions from my version 1.6 code to the latest 2017 version. The constructor for NonlinearVariationalProblem requires DirichletBC, but this was not needed in 1.6:

//version 1.6:
class PeriodicBC : public SubDomain ...
    bool inside(const Array<double>& x, bool on_boundary) const
    void map(const Array<double>& x, Array<double>& y) const
...
auto PBC1   = PeriodicBC();
jpcPhi::FunctionSpace   V_Phi(mesh, PBC1);
jpcPhi::ResidualForm    F1(V_Phi);
jpcPhi::JacobianForm    J1(V_Phi,V_Phi);
Function                phi(V_Phi);

// # Create Nonlinear Problem and Solver
auto ProblemPhi     = NonlinearVariationalProblem(F1, phi, J1);
auto SolverPhi      = NonlinearVariationalSolver(ProblemPhi);

It is now necessary to pass a DirichletBC for the constructor for NonlinearVariationalProblem:

//version 2017.1:
NonlinearVariationalProblem(std::shared_ptr<const Form> F, 
    std::shared_ptr<Function> u, 
    std::vector<std::shared_ptr<const DirichletBC>> bcs, 
    std::shared_ptr<const Form> J = nullptr)

It is not obvious to me, however, how to do this. I only have periodic BC on my 2D grid (a torus) and I did not need to use a DirichletBC class in v1.6 for this project. I have tried various DirichletBC(...) constructions to pass to NonlinearVariationalProblem, ... no success. I have used limited verisons of Fenics code thus far, so I am probably missing something obvious. Any help on how to properly construct the DirichletBC to pass to NonlinearVariationalProblem?

Thanks for any help!

asked Jun 18, 2017 by jwinkle FEniCS Novice (450 points)
edited Jun 19, 2017 by jwinkle

Maybe the demos contact-vi-snes or auto-adaptive-navier-stokes can help you.

Thanks...this does get me through the syntax issue I was having. However, I do not understand if this is semantically correct, and indeed my code now zeros-out the grid on the first time-step. Here is my DirichletBC input that fixes the syntax issue:

auto bcp = std::make_shared<DirichletBC>(V_Phi, phi, PBC1);
std::vector<std::shared_ptr<const DirichletBC>> dbcPhi {bcp};
auto ProblemPhi = std::make_shared<NonlinearVariationalProblem>
    (F1, phi, dbcPhi, J1);

Since I do not actually have DirichletBC (I have periodic BC as implemented in PBC1 SubDomain subclass instance of PeriodicBC), I do not understand if this is the correct formulation using what seems like a "dummy DirichletBC."

Fenics v1.6 had a constructor for NonlinearVariationalProblem that did not require a DirichletBC object, so in upgrading to v2017.1, I am not sure if the above is everything that needs to be done. As above, PBC1 implements inside() and map(). Any ideas here? Thx..JW

1 Answer

0 votes
 
Best answer

Issue Solved:

There is no "shared_ptr version" constructor for NonlinearVariationalProblem without need to pass a vector of DirichletBC. Previously, v1.6 had non-shared_ptr versions, but v2017.1 has only such versions. The solution is to simply pass an empty vector for the DirichletBC:

//empty vector:
std::vector<std::shared_ptr<const DirichletBC>> dbcPhi;
auto ProblemPhi = std::make_shared<NonlinearVariationalProblem>
    (F1, phi, dbcPhi, J1);

The issue I had with over-writing the grid was a pointer de-referencing problem, which was probable after having to add une centaine de std::shared_ptr<> throughout my code to convert to 2017.1! Thanks for the nudge in the right direction! --JW

answered Jun 19, 2017 by jwinkle FEniCS Novice (450 points)
...