Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to disable caching in FE::reinit() #4119

Open
wants to merge 6 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 111 additions & 32 deletions doc/html/examples/vector_fe_ex3.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

Mesh Information:
elem_dimensions()={2}
elem_default_orders()={SECOND}
supported_nodal_order()=2
spatial_dimension()=2
n_nodes()=1681
n_local_nodes()=1681
Expand Down Expand Up @@ -91,11 +93,11 @@
Assembling the System
Nonlinear Residual: 23.6078
Linear solve starting, tolerance 1e-12
Linear solve finished, step 1, residual 1.0869e-13
Linear solve finished, step 1, residual 9.76637e-14
Trying full Newton step
Current Residual: 1.5625e-13
Nonlinear solver converged, step 0, residual reduction 6.61855e-15 < 1e-06
Nonlinear solver relative step size inf > 1e-06
Current Residual: 1.61568e-13
Nonlinear solver converged, step 0, residual reduction 6.84384e-15 < 1e-06
Nonlinear solver relative step size 1 > 1e-06
L2-Error is: 0.128366
HCurl semi-norm error is: 0.655952
HCurl-Error is: 0.668394
Expand All @@ -111,6 +113,8 @@

Mesh Information:
elem_dimensions()={2}
elem_default_orders()={THIRD}
supported_nodal_order()=3
spatial_dimension()=2
n_nodes()=2481
n_local_nodes()=2481
Expand Down Expand Up @@ -152,11 +156,11 @@
Assembling the System
Nonlinear Residual: 23.6078
Linear solve starting, tolerance 1e-12
Linear solve finished, step 1, residual 1.07153e-13
Linear solve finished, step 1, residual 1.00933e-13
Trying full Newton step
Current Residual: 1.57019e-13
Nonlinear solver converged, step 0, residual reduction 6.65114e-15 < 1e-06
Nonlinear solver relative step size inf > 1e-06
Current Residual: 1.64799e-13
Nonlinear solver converged, step 0, residual reduction 6.98069e-15 < 1e-06
Nonlinear solver relative step size 1 > 1e-06
L2-Error is: 0.128366
HCurl semi-norm error is: 0.655952
HCurl-Error is: 0.668394
Expand All @@ -172,6 +176,8 @@

Mesh Information:
elem_dimensions()={2}
elem_default_orders()={SECOND}
supported_nodal_order()=2
spatial_dimension()=2
n_nodes()=1281
n_local_nodes()=1281
Expand Down Expand Up @@ -213,11 +219,11 @@
Assembling the System
Nonlinear Residual: 28.9779
Linear solve starting, tolerance 1e-12
Linear solve finished, step 1, residual 3.95978e-14
Linear solve finished, step 1, residual 4.08457e-14
Trying full Newton step
Current Residual: 7.02729e-14
Nonlinear solver converged, step 0, residual reduction 2.42505e-15 < 1e-06
Nonlinear solver relative step size inf > 1e-06
Current Residual: 6.89384e-14
Nonlinear solver converged, step 0, residual reduction 2.379e-15 < 1e-06
Nonlinear solver relative step size 1 > 1e-06
L2-Error is: 0.12862
HCurl semi-norm error is: 0.802879
HCurl-Error is: 0.813116
Expand All @@ -233,6 +239,8 @@

Mesh Information:
elem_dimensions()={2}
elem_default_orders()={SECOND}
supported_nodal_order()=2
spatial_dimension()=2
n_nodes()=1681
n_local_nodes()=1681
Expand Down Expand Up @@ -274,11 +282,11 @@
Assembling the System
Nonlinear Residual: 28.9779
Linear solve starting, tolerance 1e-12
Linear solve finished, step 1, residual 3.99601e-14
Linear solve finished, step 1, residual 4.17423e-14
Trying full Newton step
Current Residual: 7.14395e-14
Nonlinear solver converged, step 0, residual reduction 2.46531e-15 < 1e-06
Nonlinear solver relative step size inf > 1e-06
Current Residual: 7.0449e-14
Nonlinear solver converged, step 0, residual reduction 2.43113e-15 < 1e-06
Nonlinear solver relative step size 1 > 1e-06
L2-Error is: 0.12862
HCurl semi-norm error is: 0.802879
HCurl-Error is: 0.813116
Expand All @@ -289,11 +297,76 @@
***************************************************************
***************************************************************
* Running Example vector_fe_ex3:
* ./example-opt element_type=QUAD9 grid_size=5 refine=2 -pc_type lu --disable-caching
***************************************************************

Mesh Information:
elem_dimensions()={2}
elem_default_orders()={SECOND}
supported_nodal_order()=2
spatial_dimension()=2
n_nodes()=1681
n_local_nodes()=1681
n_elem()=525
n_local_elem()=525
n_active_elem()=400
n_subdomains()=1
n_elemsets()=0
n_partitions()=1
n_processors()=1
n_threads()=1
processor_id()=0
is_prepared()=true
is_replicated()=true

EquationSystems
n_systems()=1
System #0, "CurlCurl"
Type "Implicit"
Variables="u"
Finite Element Types="NEDELEC_ONE"
Approximation Orders="FIRST"
n_dofs()=840
n_local_dofs()=840
max(n_local_dofs())=840
n_constrained_dofs()=0
n_local_constrained_dofs()=0
max(local unconstrained dofs)=840
n_vectors()=1
n_matrices()=1
DofMap Sparsity
Average On-Processor Bandwidth <= 6.71429
Average Off-Processor Bandwidth <= 0
Maximum On-Processor Bandwidth <= 7
Maximum Off-Processor Bandwidth <= 0
DofMap Constraints
Number of DoF Constraints = 0

Assembling the System
Nonlinear Residual: 28.9779
Linear solve starting, tolerance 1e-12
Linear solve finished, step 1, residual 4.29645e-14
Trying full Newton step
Current Residual: 6.93223e-14
Nonlinear solver converged, step 0, residual reduction 2.39225e-15 < 1e-06
Nonlinear solver relative step size 1 > 1e-06
L2-Error is: 0.12862
HCurl semi-norm error is: 0.802879
HCurl-Error is: 0.813116

***************************************************************
* Done Running Example vector_fe_ex3:
* ./example-opt element_type=QUAD9 grid_size=5 refine=2 -pc_type lu --disable-caching
***************************************************************
***************************************************************
* Running Example vector_fe_ex3:
* ./example-opt order=2 element_type=TRI6 -pc_type lu
***************************************************************

Mesh Information:
elem_dimensions()={2}
elem_default_orders()={SECOND}
supported_nodal_order()=2
spatial_dimension()=2
n_nodes()=1681
n_local_nodes()=1681
Expand Down Expand Up @@ -335,11 +408,11 @@
Assembling the System
Nonlinear Residual: 47.591
Linear solve starting, tolerance 1e-12
Linear solve finished, step 1, residual 7.29357e-13
Linear solve finished, step 1, residual 6.83715e-13
Trying full Newton step
Current Residual: 1.77284e-12
Nonlinear solver converged, step 0, residual reduction 3.72517e-14 < 1e-06
Nonlinear solver relative step size inf > 1e-06
Current Residual: 1.81644e-12
Nonlinear solver converged, step 0, residual reduction 3.81677e-14 < 1e-06
Nonlinear solver relative step size 1 > 1e-06
L2-Error is: 0.00575821
HCurl semi-norm error is: 0.0398913
HCurl-Error is: 0.0403047
Expand All @@ -355,6 +428,8 @@

Mesh Information:
elem_dimensions()={2}
elem_default_orders()={THIRD}
supported_nodal_order()=3
spatial_dimension()=2
n_nodes()=2481
n_local_nodes()=2481
Expand Down Expand Up @@ -396,11 +471,11 @@
Assembling the System
Nonlinear Residual: 47.591
Linear solve starting, tolerance 1e-12
Linear solve finished, step 1, residual 6.88575e-13
Linear solve finished, step 1, residual 6.96023e-13
Trying full Newton step
Current Residual: 1.77577e-12
Nonlinear solver converged, step 0, residual reduction 3.73131e-14 < 1e-06
Nonlinear solver relative step size inf > 1e-06
Current Residual: 1.7454e-12
Nonlinear solver converged, step 0, residual reduction 3.66751e-14 < 1e-06
Nonlinear solver relative step size 1 > 1e-06
L2-Error is: 0.00575821
HCurl semi-norm error is: 0.0398913
HCurl-Error is: 0.0403047
Expand All @@ -416,6 +491,8 @@

Mesh Information:
elem_dimensions()={2}
elem_default_orders()={SECOND}
supported_nodal_order()=2
spatial_dimension()=2
n_nodes()=1281
n_local_nodes()=1281
Expand Down Expand Up @@ -457,11 +534,11 @@
Assembling the System
Nonlinear Residual: 20.864
Linear solve starting, tolerance 1e-12
Linear solve finished, step 1, residual 6.17245e-13
Linear solve finished, step 1, residual 6.11995e-13
Trying full Newton step
Current Residual: 1.26787e-12
Nonlinear solver converged, step 0, residual reduction 6.07684e-14 < 1e-06
Nonlinear solver relative step size inf > 1e-06
Current Residual: 1.29679e-12
Nonlinear solver converged, step 0, residual reduction 6.21542e-14 < 1e-06
Nonlinear solver relative step size 1 > 1e-06
L2-Error is: 0.00520051
HCurl semi-norm error is: 0.0326373
HCurl-Error is: 0.0330491
Expand All @@ -477,6 +554,8 @@

Mesh Information:
elem_dimensions()={2}
elem_default_orders()={SECOND}
supported_nodal_order()=2
spatial_dimension()=2
n_nodes()=1681
n_local_nodes()=1681
Expand Down Expand Up @@ -518,11 +597,11 @@
Assembling the System
Nonlinear Residual: 20.864
Linear solve starting, tolerance 1e-12
Linear solve finished, step 1, residual 6.17982e-13
Linear solve finished, step 1, residual 6.2441e-13
Trying full Newton step
Current Residual: 1.30813e-12
Nonlinear solver converged, step 0, residual reduction 6.2698e-14 < 1e-06
Nonlinear solver relative step size inf > 1e-06
Current Residual: 1.28321e-12
Nonlinear solver converged, step 0, residual reduction 6.15035e-14 < 1e-06
Nonlinear solver relative step size 1 > 1e-06
L2-Error is: 0.00520051
HCurl semi-norm error is: 0.0326373
HCurl-Error is: 0.0330491
Expand Down
3 changes: 3 additions & 0 deletions examples/vector_fe/vector_fe_ex3/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ run_example_no_extra_options "$example_name" "$options"
options="element_type=QUAD9 -pc_type lu"
run_example_no_extra_options "$example_name" "$options"

options="element_type=QUAD9 grid_size=5 refine=2 -pc_type lu --disable-caching"
run_example_no_extra_options "$example_name" "$options"

options="order=2 element_type=TRI6 -pc_type lu"
run_example_no_extra_options "$example_name" "$options"

Expand Down
5 changes: 5 additions & 0 deletions examples/vector_fe/vector_fe_ex3/vector_fe_ex3.C
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "libmesh/mesh.h"
#include "libmesh/mesh_generation.h"
#include "libmesh/mesh_modification.h"
#include "libmesh/mesh_refinement.h"
#include "libmesh/exact_solution.h"
#include "libmesh/string_to_enum.h"
#include "libmesh/enum_solver_package.h"
Expand Down Expand Up @@ -91,6 +92,10 @@ int main (int argc, char ** argv)
// Make sure the code is robust against nodal reorderings.
MeshTools::Modification::permute_elements(mesh);

// Make sure the code is robust against mesh refinements.
MeshRefinement mesh_refinement(mesh);
mesh_refinement.uniformly_refine(infile("refine", 0));

// Print information about the mesh to the screen.
mesh.print_info();

Expand Down
48 changes: 25 additions & 23 deletions src/fe/fe.C
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "libmesh/tensor_value.h"
#include "libmesh/enum_elem_type.h"
#include "libmesh/quadrature_gauss.h"
#include "libmesh/libmesh_singleton.h"

namespace {
// Put this outside a templated class, so we only get 1 warning
Expand All @@ -40,6 +41,18 @@ namespace {

namespace libMesh
{
// ------------------------------------------------------------
// Whether we cache the node locations on the last element we computed on
// to try to avoid calling init_shape_functions and compute_shape_functions
static const bool * caching = nullptr;

class CachingSetup: public Singleton::Setup
{
private:
void setup() { caching = new bool(!on_command_line("--disable-caching")); }
public:
~CachingSetup() { caching = nullptr; }
} caching_setup;


// ------------------------------------------------------------
Expand Down Expand Up @@ -207,43 +220,32 @@ void FE<Dim,T>::reinit(const Elem * elem,
this->_fe_map->template init_reference_to_physical_map<Dim>
(this->qrule->get_points(), elem);
this->init_shape_functions (this->qrule->get_points(), elem);

if (this->shapes_need_reinit())
{
cached_nodes.resize(elem->n_nodes());
for (auto n : elem->node_index_range())
cached_nodes[n] = elem->point(n);
}
}
else
{
// libmesh_assert_greater (elem->n_nodes(), 1);

cached_nodes_still_fit = true;
if (cached_nodes.size() != elem->n_nodes())
cached_nodes_still_fit = false;
else
for (auto n : make_range(1u, elem->n_nodes()))
{
if (!(elem->point(n) - elem->point(0)).relative_fuzzy_equals
((cached_nodes[n] - cached_nodes[0]), 1e-13))
{
cached_nodes_still_fit = false;
break;
}
}
cached_nodes_still_fit = cached_nodes.size() == elem->n_nodes();
for (unsigned n = 1; cached_nodes_still_fit && n < elem->n_nodes(); n++)
cached_nodes_still_fit =
((elem->point(n) - elem->point(0)).relative_fuzzy_equals
((cached_nodes[n] - cached_nodes[0]), 1e-13));

if (this->shapes_need_reinit() && !cached_nodes_still_fit)
{
this->_fe_map->template init_reference_to_physical_map<Dim>
(this->qrule->get_points(), elem);
this->init_shape_functions (this->qrule->get_points(), elem);
cached_nodes.resize(elem->n_nodes());
for (auto n : elem->node_index_range())
cached_nodes[n] = elem->point(n);
}
}

if (this->shapes_need_reinit() && *caching && !cached_nodes_still_fit)
{
cached_nodes.resize(elem->n_nodes());
for (auto n : elem->node_index_range())
cached_nodes[n] = elem->point(n);
}

// The shape functions correspond to the qrule
this->shapes_on_quadrature = true;
}
Expand Down