Skip to content

Commit

Permalink
Merge pull request #27943 from grunerjmeier/TimedSubdomainModifier2
Browse files Browse the repository at this point in the history
TimedSubdomainModifier fails for multiple idential times (closes #27942)
  • Loading branch information
lindsayad authored Jun 19, 2024
2 parents ec4d4b0 + 6cdac73 commit 705cbc2
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 49 deletions.
10 changes: 5 additions & 5 deletions framework/include/userobjects/TimedElementSubdomainModifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ class TimedElementSubdomainModifier : public ElementSubdomainModifier
protected:
/**
* Requests a vector of all times from the inheriting class
* (these do not have to be sorted).
* (these do not have to be sorted and may have duplicates).
* @returns Unsorted vector of times.
*/
virtual std::set<Real> getTimes() = 0;
virtual std::vector<Real> getTimes() = 0;

/// storage for the times including their original index.
struct timeIndexPair
struct TimeIndexPair
{
Real time;
std::size_t index;

bool operator<(const timeIndexPair & a) const
bool operator<(const TimeIndexPair & a) const
{
if (time == a.time)
return index < a.index;
Expand All @@ -47,5 +47,5 @@ class TimedElementSubdomainModifier : public ElementSubdomainModifier
};

/// Times and subdomain changes to make
std::set<timeIndexPair> _times_and_indices;
std::set<TimeIndexPair> _times_and_indices;
};
7 changes: 4 additions & 3 deletions framework/include/userobjects/TimedSubdomainModifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TimedSubdomainModifier : public TimedElementSubdomainModifier
TimedSubdomainModifier(const InputParameters & parameters);

protected:
virtual std::set<Real> getTimes() override { return _times; }
virtual std::vector<Real> getTimes() override { return _times; }

virtual SubdomainID computeSubdomainID() override;

Expand All @@ -33,8 +33,9 @@ class TimedSubdomainModifier : public TimedElementSubdomainModifier
void buildFromFile();

/// Times to change the subdomains on. If the time steps do not align with the times,
/// the subdomain changes will happen at the end of the time step
std::set<Real> _times;
/// the subdomain changes will happen at the end of the time step.
/// The sort order of this vector must match _blocks_from and _blocks_to.
std::vector<Real> _times;

/// Source subdomains to change from
std::vector<SubdomainID> _blocks_from;
Expand Down
13 changes: 3 additions & 10 deletions framework/src/userobjects/TimedElementSubdomainModifier.C
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
InputParameters
TimedElementSubdomainModifier::validParams()
{
InputParameters params = ElementSubdomainModifier::validParams();
params.addParam<std::vector<Real>>("times", "The times of the subdomain modifications.");
return params;
return ElementSubdomainModifier::validParams();
}

TimedElementSubdomainModifier::TimedElementSubdomainModifier(const InputParameters & parameters)
Expand All @@ -31,12 +29,7 @@ TimedElementSubdomainModifier::initialize()
const auto times = getTimes();

// copy data to local storage
unsigned int i = 0;
std::size_t i = 0;
for (const auto time : times)
{
timeIndexPair pair;
pair.time = time;
pair.index = i++;
_times_and_indices.insert(pair);
}
_times_and_indices.insert(TimeIndexPair{time, i++});
}
39 changes: 18 additions & 21 deletions framework/src/userobjects/TimedSubdomainModifier.C
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ TimedSubdomainModifier::validParams()
{
InputParameters params = TimedElementSubdomainModifier::validParams();

// parameters for direct input (additionally to 'times')
// parameters for direct input
params.addParam<std::vector<Real>>("times", "The times of the subdomain modifications.");
params.addParam<std::vector<SubdomainName>>("blocks_from",
"Names or ids of the 'old' block(s), to be renamed.");
params.addParam<std::vector<SubdomainName>>("blocks_to", "Names or ids of the 'new' block.");
Expand All @@ -31,11 +32,11 @@ TimedSubdomainModifier::validParams()
"Indicates whether the file contains a header with the column names");
params.addParam<std::string>("delimiter", ",", "Delimiter used to parse the file");
params.addParam<std::string>("comment", ";", "Comment character used to parse the file");
params.addParam<size_t>(
params.addParam<std::size_t>(
"time_column_index", 0, "Zero-based index of the time column. Default is '0'.");
params.addParam<size_t>(
params.addParam<std::size_t>(
"blocks_from_column_index", 1, "Zero-based index of the blocks_from column. Default is '1'.");
params.addParam<size_t>(
params.addParam<std::size_t>(
"blocks_to_column_index", 2, "Zero-based index of the blocks_to column. Default is '2'.");
params.addParam<std::string>("time_column_text", "Header text of the time column.");
params.addParam<std::string>("blocks_from_column_text", "Header text of the blocks_from column.");
Expand Down Expand Up @@ -113,9 +114,7 @@ TimedSubdomainModifier::TimedSubdomainModifier(const InputParameters & parameter
void
TimedSubdomainModifier::buildFromParameters()
{

const auto times = getParam<std::vector<Real>>("times");
_times = std::set<Real>(times.begin(), times.end());
_times = getParam<std::vector<Real>>("times");
const auto n = _times.size();

const auto raw_from = getParam<std::vector<SubdomainName>>("blocks_from");
Expand All @@ -130,11 +129,10 @@ TimedSubdomainModifier::buildFromParameters()
_blocks_from.resize(n);
_blocks_to.resize(n);

const std::shared_ptr<MooseMesh> _mesh = _app.actionWarehouse().mesh();
for (const auto i : index_range(raw_from))
{
_blocks_from[i] = _mesh->getSubdomainID(raw_from[i]);
_blocks_to[i] = _mesh->getSubdomainID(raw_to[i]);
_blocks_from[i] = _mesh.getSubdomainID(raw_from[i]);
_blocks_to[i] = _mesh.getSubdomainID(raw_to[i]);
}
}

Expand Down Expand Up @@ -171,7 +169,7 @@ TimedSubdomainModifier::buildFromFile()
file.setComment(_comment);
file.read();

size_t _time_column = 0;
std::size_t _time_column = 0;
if (isParamValid("time_column_text"))
{
const auto s = getParam<std::string>("time_column_text");
Expand All @@ -183,10 +181,10 @@ TimedSubdomainModifier::buildFromFile()
}
else if (isParamValid("time_column_index"))
{
_time_column = getParam<size_t>("time_column_index");
_time_column = getParam<std::size_t>("time_column_index");
}

size_t _blocks_from_column = 1;
std::size_t _blocks_from_column = 1;
if (isParamValid("blocks_from_column_text"))
{
const auto s = getParam<std::string>("blocks_from_column_text");
Expand All @@ -198,10 +196,10 @@ TimedSubdomainModifier::buildFromFile()
}
else if (isParamValid("blocks_from_column_index"))
{
_blocks_from_column = getParam<size_t>("blocks_from_column_index");
_blocks_from_column = getParam<std::size_t>("blocks_from_column_index");
}

size_t _blocks_to_column = 2;
std::size_t _blocks_to_column = 2;
if (isParamValid("blocks_to_column_text"))
{
const auto s = getParam<std::string>("blocks_to_column_text");
Expand All @@ -212,7 +210,7 @@ TimedSubdomainModifier::buildFromFile()
_blocks_to_column = std::distance(_names.begin(), it);
}
else if (isParamValid("blocks_to_column_index"))
_blocks_to_column = getParam<size_t>("blocks_to_column_index");
_blocks_to_column = getParam<std::size_t>("blocks_to_column_index");

const auto max_needed_column_index =
std::max({_time_column, _blocks_from_column, _blocks_to_column});
Expand Down Expand Up @@ -253,17 +251,16 @@ TimedSubdomainModifier::buildFromFile()
_blocks_to.resize(n_rows);

// fill the to and from blocks vectors
const std::shared_ptr<MooseMesh> _mesh = _app.actionWarehouse().mesh();
for (const auto & time_str : strTimes)
_times.insert(std::stod(time_str));
_times.push_back(std::stod(time_str));
std::transform(strBlockFrom.begin(),
strBlockFrom.end(),
_blocks_from.begin(),
[_mesh](std::string x) { return _mesh->getSubdomainID(x); });
[this](const std::string & x) { return _mesh.getSubdomainID(x); });
std::transform(strBlockTo.begin(),
strBlockTo.end(),
_blocks_to.begin(),
[_mesh](std::string x) { return _mesh->getSubdomainID(x); });
[this](const std::string & x) { return _mesh.getSubdomainID(x); });
}

SubdomainID
Expand All @@ -274,7 +271,7 @@ TimedSubdomainModifier::computeSubdomainID()

// check for all the subdomain changes that can have been requested between the previous and the
// current time
for (const auto time_pair : _times_and_indices)
for (const auto & time_pair : _times_and_indices)
{
// time of the data point
const auto t = time_pair.time;
Expand Down
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion test/tests/userobjects/element_subdomain_modifier/tests
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
[direct]
type = 'Exodiff'
input = 'tsm_direct.i'
exodiff = 'tsm_direct_out.e-s006'
exodiff = 'tsm_direct_out.e-s004 tsm_direct_out.e-s006'
detail = 'listed in the input file,'
exodiff_opts = '-pedantic'
[]
Expand Down
29 changes: 20 additions & 9 deletions test/tests/userobjects/element_subdomain_modifier/tsm_direct.i
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
material_coverage_check = false
[]

Box2_inactive_id = '3'
Box2_inactive_id = '4'
Box3_inactive_id = '5'
Box2_inactive_name = 'Box2_inactive'
inactive_domain_block_ids = ${Box2_inactive_id}
inactive_domain_block_names = ${Box2_inactive_name}
Box3_inactive_name = 'Box3_inactive'
inactive_domain_block_ids = '${Box2_inactive_id} ${Box3_inactive_id}'
inactive_domain_block_names = '${Box2_inactive_name} ${Box3_inactive_name}'

[Mesh]
[BaseMesh]
type = GeneratedMeshGenerator
elem_type = TET4
dim = 3
nx = 5
nx = 4
ny = 3
nz = 2
xmin = -10
Expand All @@ -41,8 +43,17 @@ inactive_domain_block_names = ${Box2_inactive_name}
input = "Box1"
block_id = 2
location = "INSIDE"
bottom_left = "-2 -2 +2"
top_right = "+2 +2 0"
bottom_left = "-4 -3 +3"
top_right = "0 +3 0"
[]

[Box3]
type = SubdomainBoundingBoxGenerator
input = "Box2"
block_id = 3
location = "INSIDE"
bottom_left = "0 -3 +2"
top_right = "+4 +3 0"
[]

add_subdomain_ids = ${inactive_domain_block_ids}
Expand All @@ -59,9 +70,9 @@ inactive_domain_block_names = ${Box2_inactive_name}
[UserObjects]
[GlobalSubdomainModifier]
type = TimedSubdomainModifier
times = '0.4 0.6'
blocks_from = '2 3'
blocks_to = 'Box2_inactive 2' # Subdomain names are permitted ('Box2_inactive' = 3)
times = ' 0.4 0.6 0.4'
blocks_from = '2 4 3'
blocks_to = ' Box2_inactive 2 Box3_inactive' # Subdomain names are permitted ('Box2_inactive' = 4, etc)
execute_on = 'INITIAL TIMESTEP_BEGIN'
[]
[]
Expand Down

0 comments on commit 705cbc2

Please sign in to comment.