Skip to content

Commit

Permalink
enable p refinement idaholab#24141
Browse files Browse the repository at this point in the history
  • Loading branch information
YaqiWang committed Apr 26, 2023
1 parent 00beed9 commit 107b880
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 0 deletions.
7 changes: 7 additions & 0 deletions framework/include/base/Adaptivity.h
Expand Up @@ -128,6 +128,11 @@ class Adaptivity : public ConsoleStreamInterface,
*/
void setRecomputeMarkersFlag(const bool flag) { _recompute_markers_during_cycles = flag; }

/**
* Switch from h-refinement to p-refinement
*/
void switchHToPRefinement();

/**
* Adapts the mesh based on the error estimator used
*
Expand Down Expand Up @@ -302,6 +307,8 @@ class Adaptivity : public ConsoleStreamInterface,

/// Stores pointers to ErrorVectors associated with indicator field names
std::map<std::string, std::unique_ptr<ErrorVector>> _indicator_field_to_error_vector;

bool _p_refinement_flag = false;
};

template <typename T>
Expand Down
29 changes: 29 additions & 0 deletions framework/include/markers/SubdomainMarker.h
@@ -0,0 +1,29 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#pragma once

#include "Marker.h"

class SubdomainMarker : public Marker
{
public:
static InputParameters validParams();

SubdomainMarker(const InputParameters & parameters);

protected:
virtual MarkerValue computeElementMarker() override;

MarkerValue _inside;
MarkerValue _outside;

std::vector<SubdomainName> _blocks;
std::set<SubdomainID> _blk_ids;
};
3 changes: 3 additions & 0 deletions framework/src/actions/AdaptivityAction.C
Expand Up @@ -79,6 +79,7 @@ AdaptivityAction::validParams()
"show_initial_progress", true, "Show the progress of the initial adaptivity");
params.addParam<bool>(
"recompute_markers_during_cycles", false, "Recompute markers during adaptivity cycles");
params.addParam<bool>("switch_h_to_p_refinement", false, "True to perform p-refinement");
return params;
}

Expand Down Expand Up @@ -202,6 +203,8 @@ AdaptivityAction::act()

adapt.setTimeActive(getParam<Real>("start_time"), getParam<Real>("stop_time"));
adapt.setInterval(getParam<unsigned int>("interval"));
if (getParam<bool>("switch_h_to_p_refinement"))
adapt.switchHToPRefinement();
}
}

Expand Down
3 changes: 3 additions & 0 deletions framework/src/actions/SetAdaptivityOptionsAction.C
Expand Up @@ -49,6 +49,7 @@ SetAdaptivityOptionsAction::validParams()
"The number of adaptive steps to use when on each timestep during a Transient simulation.");
params.addParam<bool>(
"recompute_markers_during_cycles", false, "Recompute markers during adaptivity cycles");
params.addParam<bool>("switch_h_to_p_refinement", false, "True to perform p-refinement");
return params;
}

Expand Down Expand Up @@ -137,5 +138,7 @@ SetAdaptivityOptionsAction::act()
adapt.setInterval(getParam<unsigned int>("interval"));

adapt.setRecomputeMarkersFlag(getParam<bool>("recompute_markers_during_cycles"));
if (getParam<bool>("switch_h_to_p_refinement"))
adapt.switchHToPRefinement();
}
}
13 changes: 13 additions & 0 deletions framework/src/base/Adaptivity.C
Expand Up @@ -220,6 +220,9 @@ Adaptivity::adaptMesh(std::string marker_name /*=std::string()*/)
if (distributed_adaptivity)
_mesh_refinement->make_flags_parallel_consistent();

if (_p_refinement_flag)
_mesh_refinement->switch_h_to_p_refinement();

// Perform refinement and coarsening
mesh_changed = _mesh_refinement->refine_and_coarsen_elements();

Expand All @@ -229,6 +232,10 @@ Adaptivity::adaptMesh(std::string marker_name /*=std::string()*/)
// we sync them here.
if (distributed_adaptivity)
_displaced_mesh_refinement->make_flags_parallel_consistent();

if (_p_refinement_flag)
_displaced_mesh_refinement->switch_h_to_p_refinement();

#ifndef NDEBUG
bool displaced_mesh_changed =
#endif
Expand Down Expand Up @@ -378,4 +385,10 @@ Adaptivity::isAdaptivityDue()
return _mesh_refinement_on && (_start_time <= _t && _t < _stop_time) && _step % _interval == 0;
}

void
Adaptivity::switchHToPRefinement()
{
_p_refinement_flag = true;
}

#endif // LIBMESH_ENABLE_AMR
53 changes: 53 additions & 0 deletions framework/src/markers/SubdomainMarker.C
@@ -0,0 +1,53 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "SubdomainMarker.h"

registerMooseObject("MooseApp", SubdomainMarker);

InputParameters
SubdomainMarker::validParams()
{
InputParameters params = Marker::validParams();
params.addRequiredParam<std::vector<SubdomainName>>("block",
"The subdomain this marker is to be marked.");

MooseEnum marker_states = Marker::markerStates();

params.addRequiredParam<MooseEnum>(
"inside", marker_states, "How to mark elements inside the subdomain.");
params.addRequiredParam<MooseEnum>(
"outside", marker_states, "How to mark elements outside the subdomain.");

params.addClassDescription(
"Marks the region inside and outside of a subdomain for refinement or coarsening.");
return params;
}

SubdomainMarker::SubdomainMarker(const InputParameters & parameters)
: Marker(parameters),
_inside(parameters.get<MooseEnum>("inside").getEnum<MarkerValue>()),
_outside(parameters.get<MooseEnum>("outside").getEnum<MarkerValue>()),
_blocks(getParam<std::vector<SubdomainName>>("block"))
{
auto blocks = _mesh.getSubdomainIDs(_blocks);
for (const auto & block : blocks)
_blk_ids.insert(block);
}

Marker::MarkerValue
SubdomainMarker::computeElementMarker()
{
auto block_id = _current_elem->subdomain_id();

if (_blk_ids.count(block_id))
return _inside;

return _outside;
}
9 changes: 9 additions & 0 deletions test/tests/dgkernels/2d_diffusion_dg/tests
Expand Up @@ -9,6 +9,15 @@
requirement = "The system shall support solving 2D diffusion using the discontinuous Galerkin "
"method."
[]
[p_refinement_test]
type = 'Exodiff'
input = '2d_diffusion_dg_test.i'
cli_args = 'Outputs/file_base=p_refinement Executioner/Adaptivity/switch_h_to_p_refinement=true'
exodiff = 'p_refinement.e-s003'
max_parallel = 1
requirement = "The system shall support solving 2D diffusion using the discontinuous Galerkin "
"method with p-refinement."
[]
[constMonomial]
type = 'Exodiff'
input = '2d_diffusion_dg_test.i'
Expand Down

0 comments on commit 107b880

Please sign in to comment.