Skip to content

Commit

Permalink
Make fem::Form stateless (#1168)
Browse files Browse the repository at this point in the history
* Remove Form setters

* Interface updates

* Doc fixes

* Comment out some methods

* More updates

* Form constructor updates

* Add missing line

* Work around

* Disable test

* Remove orig coeff code

* Comment unused code

* More removals

* Sort out form coefficients by name

* Remove FormCoefficients

* Fix doc

* Remove commented code

* Test updates

* Update demo

* Simplify constants in forms

* Remove commented code

* Doc fixes

* Pass mesh to Form constructor

* Make Form integrals stateless

* Flake8 fixes

* Syntax fixes

* Pass subdomain to Form constructor

* Update demo

* Test update

* Pass subdomains to form constructor

* Flake8 fix

* Doc fix

* Update demos

* Simplifications

* Remove FormIntegrals

* Update demo

* Add some comments

* Small simplifications

* Remove some weird spaces

* Fix year

* Flake8 white space fix

* Formatting fixes

* Fix some code comments

* Remove a Form member function

* Fix code string

* Improve coefficient and constant form checking

* Simplify Form constructor (pybind11)

* Add and use move constructors

* Undo form constructor change (pybind11)

* Remove stray character

* Minor doc improvements
  • Loading branch information
garth-wells committed Oct 21, 2020
1 parent f9654e2 commit f70aacd
Show file tree
Hide file tree
Showing 23 changed files with 758 additions and 1,007 deletions.
16 changes: 7 additions & 9 deletions cpp/demo/hyperelasticity/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class HyperElasticProblem : public nls::NonlinearProblem
std::shared_ptr<fem::Form<PetscScalar>> J,
std::vector<std::shared_ptr<const fem::DirichletBC<PetscScalar>>> bcs)
: _u(u), _l(L), _j(J), _bcs(bcs),
_b(L->function_space(0)->dofmap()->index_map),
_b(L->function_spaces()[0]->dofmap()->index_map),
_matA(fem::create_matrix(*J))
{
auto map = L->function_space(0)->dofmap()->index_map;
auto map = L->function_spaces()[0]->dofmap()->index_map;
const int bs = map->block_size();
std::int32_t size_local = bs * map->size_local();
std::int32_t num_ghosts = bs * map->num_ghosts();
Expand Down Expand Up @@ -82,7 +82,7 @@ class HyperElasticProblem : public nls::NonlinearProblem
MatZeroEntries(_matA.mat());
fem::assemble_matrix(la::PETScMatrix::add_fn(_matA.mat()), *_j, _bcs);
fem::add_diagonal(la::PETScMatrix::add_fn(_matA.mat()),
*_j->function_space(0), _bcs);
*_j->function_spaces()[0], _bcs);
_matA.apply(la::PETScMatrix::AssemblyType::FINAL);
return _matA.mat();
}
Expand Down Expand Up @@ -125,9 +125,10 @@ int main(int argc, char* argv[])
// Define solution function
auto u = std::make_shared<function::Function<PetscScalar>>(V);

auto a
= fem::create_form<PetscScalar>(create_form_hyperelasticity_J, {V, V});
auto L = fem::create_form<PetscScalar>(create_form_hyperelasticity_F, {V});
auto a = fem::create_form<PetscScalar>(create_form_hyperelasticity_J,
{V, V}, {{"u", u}}, {}, {});
auto L = fem::create_form<PetscScalar>(create_form_hyperelasticity_F, {V},
{{"u", u}}, {}, {});

auto u_rotation = std::make_shared<function::Function<PetscScalar>>(V);
u_rotation->interpolate([](auto& x) {
Expand Down Expand Up @@ -165,9 +166,6 @@ int main(int argc, char* argv[])
Eigen::RowMajor>::Zero(3, x.cols());
});

L->set_coefficients({{"u", u}});
a->set_coefficients({{"u", u}});

// Create Dirichlet boundary conditions
auto u0 = std::make_shared<function::Function<PetscScalar>>(V);

Expand Down
18 changes: 9 additions & 9 deletions cpp/demo/poisson/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,18 @@ int main(int argc, char* argv[])
//
// .. code-block:: cpp

// Define variational forms
auto a = fem::create_form<PetscScalar>(create_form_poisson_a, {V, V});
auto L = fem::create_form<PetscScalar>(create_form_poisson_L, {V});
// Prepare and set Constants for the bilinear form
auto kappa = std::make_shared<function::Constant<PetscScalar>>(2.0);

auto f = std::make_shared<function::Function<PetscScalar>>(V);
auto g = std::make_shared<function::Function<PetscScalar>>(V);

// Define variational forms
auto a = fem::create_form<PetscScalar>(create_form_poisson_a, {V, V}, {},
{{"kappa", kappa}}, {});
auto L = fem::create_form<PetscScalar>(create_form_poisson_L, {V},
{{"f", f}, {"g", g}}, {}, {});

// Now, the Dirichlet boundary condition (:math:`u = 0`) can be created
// using the class :cpp:class:`DirichletBC`. A :cpp:class:`DirichletBC`
// takes two arguments: the value of the boundary condition,
Expand Down Expand Up @@ -173,11 +178,6 @@ int main(int argc, char* argv[])
});

g->interpolate([](auto& x) { return Eigen::sin(5 * x.row(0)); });
L->set_coefficients({{"f", f}, {"g", g}});

// Prepare and set Constants for the bilinear form
auto kappa = std::make_shared<function::Constant<PetscScalar>>(2.0);
a->set_constants({{"kappa", kappa}});

// Now, we have specified the variational forms and can consider the
// solution of the variational problem. First, we need to define a
Expand All @@ -191,7 +191,7 @@ int main(int argc, char* argv[])
// Compute solution
function::Function<PetscScalar> u(V);
la::PETScMatrix A = fem::create_matrix(*a);
la::PETScVector b(*L->function_space(0)->dofmap()->index_map);
la::PETScVector b(*L->function_spaces()[0]->dofmap()->index_map);

MatZeroEntries(A.mat());
fem::assemble_matrix(la::PETScMatrix::add_fn(A.mat()), *a, bc);
Expand Down
2 changes: 0 additions & 2 deletions cpp/dolfinx/fem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ set(HEADERS_fem
${CMAKE_CURRENT_SOURCE_DIR}/utils.h
${CMAKE_CURRENT_SOURCE_DIR}/FiniteElement.h
${CMAKE_CURRENT_SOURCE_DIR}/Form.h
${CMAKE_CURRENT_SOURCE_DIR}/FormCoefficients.h
${CMAKE_CURRENT_SOURCE_DIR}/FormIntegrals.h
${CMAKE_CURRENT_SOURCE_DIR}/ReferenceCellGeometry.h
${CMAKE_CURRENT_SOURCE_DIR}/SparsityPatternBuilder.h
PARENT_SCOPE)
Expand Down
12 changes: 12 additions & 0 deletions cpp/dolfinx/fem/FiniteElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,21 @@ class FiniteElement
/// @param[in] ufc_element UFC finite element
explicit FiniteElement(const ufc_finite_element& ufc_element);

/// Copy constructor
FiniteElement(const FiniteElement& element) = default;

/// Move constructor
FiniteElement(FiniteElement&& element) = default;

/// Destructor
virtual ~FiniteElement() = default;

/// Copy assignment
FiniteElement& operator=(const FiniteElement& element) = default;

/// Move assignment
FiniteElement& operator=(FiniteElement&& element) = default;

/// String identifying the finite element
/// @return Element signature
std::string signature() const;
Expand Down
Loading

0 comments on commit f70aacd

Please sign in to comment.