Skip to content

Commit ec392db

Browse files
committed
Emit metadata for legacy particle sections
1 parent 6b50b64 commit ec392db

File tree

5 files changed

+67
-58
lines changed

5 files changed

+67
-58
lines changed

src/global_data/4C_global_data_read.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ Core::IO::InputFile Global::set_up_input_file(MPI_Comm comm)
189189
parameter<int>("_positional_0_id"),
190190
one_of(std::move(all_element_specs)),
191191
});
192+
193+
legacy_partial_specs["legacy_particle_specs"] = PARTICLEENGINE::create_particle_spec();
192194
}
193195

194196
return Core::IO::InputFile{std::move(valid_sections), std::move(legacy_section_names),

src/particle_engine/4C_particle_engine_enums.cpp

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -222,57 +222,26 @@ enum PARTICLEENGINE::ParticleState PARTICLEENGINE::enum_from_state_name(const st
222222
return state;
223223
}
224224

225+
static std::vector<std::string> particle_type_names = {
226+
"phase1", "phase2", "boundaryphase", "rigidphase", "dirichletphase", "neumannphase"};
227+
225228
std::string PARTICLEENGINE::enum_to_type_name(const enum ParticleType& type)
226229
{
227-
std::string name;
228-
229-
switch (type)
230-
{
231-
case Phase1:
232-
name = "phase1";
233-
break;
234-
case Phase2:
235-
name = "phase2";
236-
break;
237-
case BoundaryPhase:
238-
name = "boundaryphase";
239-
break;
240-
case RigidPhase:
241-
name = "rigidphase";
242-
break;
243-
case DirichletPhase:
244-
name = "dirichletphase";
245-
break;
246-
case NeumannPhase:
247-
name = "neumannphase";
248-
break;
249-
default:
250-
FOUR_C_THROW("particle type unknown!");
251-
}
252-
253-
return name;
230+
FOUR_C_ASSERT(type >= 0 and type < static_cast<int>(particle_type_names.size()),
231+
"particle type out of range!");
232+
return particle_type_names[type];
254233
}
255234

256235
enum PARTICLEENGINE::ParticleType PARTICLEENGINE::enum_from_type_name(const std::string& name)
257236
{
258-
enum ParticleType type;
259-
260-
if (name == "phase1")
261-
type = Phase1;
262-
else if (name == "phase2")
263-
type = Phase2;
264-
else if (name == "boundaryphase")
265-
type = BoundaryPhase;
266-
else if (name == "rigidphase")
267-
type = RigidPhase;
268-
else if (name == "dirichletphase")
269-
type = DirichletPhase;
270-
else if (name == "neumannphase")
271-
type = NeumannPhase;
272-
else
273-
FOUR_C_THROW("particle type '{}' unknown!", name.c_str());
237+
auto it = std::ranges::find(particle_type_names, name);
238+
FOUR_C_ASSERT(it != particle_type_names.end(), "particle type '{}' unknown!", name.c_str());
239+
return static_cast<enum ParticleType>(std::distance(particle_type_names.begin(), it));
240+
}
274241

275-
return type;
242+
const std::vector<std::string>& PARTICLEENGINE::get_particle_type_names()
243+
{
244+
return particle_type_names;
276245
}
277246

278247
std::string PARTICLEENGINE::enum_to_status_name(const enum ParticleStatus& status)

src/particle_engine/4C_particle_engine_enums.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include "4C_utils_exceptions.hpp"
1717

18+
#include <vector>
19+
1820
FOUR_C_NAMESPACE_OPEN
1921

2022
/*---------------------------------------------------------------------------*
@@ -177,6 +179,8 @@ namespace PARTICLEENGINE
177179
*/
178180
enum ParticleType enum_from_type_name(const std::string& name);
179181

182+
const std::vector<std::string>& get_particle_type_names();
183+
180184
//! @}
181185

182186
//! \name definition of particle status

src/particle_engine/4C_particle_engine_particlereader.cpp

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99

1010
#include "4C_comm_mpi_utils.hpp"
1111
#include "4C_io_input_file.hpp"
12+
#include "4C_io_input_spec_builders.hpp"
1213
#include "4C_io_pstream.hpp"
1314
#include "4C_io_value_parser.hpp"
1415
#include "4C_particle_engine_enums.hpp"
1516
#include "4C_particle_engine_object.hpp"
1617
#include "4C_particle_engine_typedefs.hpp"
1718

19+
#include <sys/stat.h>
1820
#include <Teuchos_Time.hpp>
1921

2022
FOUR_C_NAMESPACE_OPEN
@@ -60,30 +62,43 @@ void PARTICLEENGINE::read_particles(Core::IO::InputFile& input, const std::strin
6062
PARTICLEENGINE::StateEnum particlestate;
6163
std::vector<double> state;
6264

63-
std::istringstream linestream(std::string(parser.get_unparsed_remainder()));
64-
65-
while (linestream >> statelabel)
65+
while (!parser.at_end())
6666
{
67+
auto next = parser.peek();
6768
// optional particle radius
68-
if (statelabel == "RAD")
69+
if (next == "RAD")
6970
{
7071
particlestate = PARTICLEENGINE::Radius;
71-
state.resize(1);
72-
linestream >> state[0];
72+
parser.consume("RAD");
73+
74+
if (auto val = parser.read<std::optional<double>>())
75+
{
76+
state.resize(1);
77+
state[0] = *val;
78+
}
79+
else
80+
{
81+
continue;
82+
}
7383
}
7484
// optional rigid body color
75-
else if (statelabel == "RIGIDCOLOR")
85+
else if (next == "RIGIDCOLOR")
7686
{
7787
particlestate = PARTICLEENGINE::RigidBodyColor;
78-
state.resize(1);
79-
linestream >> state[0];
88+
parser.consume("RIGIDCOLOR");
89+
90+
if (auto val = parser.read<std::optional<double>>())
91+
{
92+
state.resize(1);
93+
state[0] = *val;
94+
}
95+
else
96+
{
97+
continue;
98+
}
8099
}
81100
else
82-
FOUR_C_THROW("optional particle state with label '{}' unknown!", statelabel.c_str());
83-
84-
if (not linestream)
85-
FOUR_C_THROW("expecting values of state '{}' if label '{}' is set!",
86-
PARTICLEENGINE::enum_to_state_name(particlestate).c_str(), statelabel.c_str());
101+
FOUR_C_THROW("Optional particle state with label '{}' unknown!", statelabel);
87102

88103
// allocate memory to hold optional particle state
89104
if (static_cast<int>(particlestates.size()) < (particlestate + 1))
@@ -113,4 +128,17 @@ void PARTICLEENGINE::read_particles(Core::IO::InputFile& input, const std::strin
113128
}
114129

115130

131+
Core::IO::InputSpec PARTICLEENGINE::create_particle_spec()
132+
{
133+
using namespace Core::IO::InputSpecBuilders;
134+
135+
return all_of({
136+
deprecated_selection<std::string>("TYPE", get_particle_type_names()),
137+
parameter<std::vector<double>>("POS", {.size = 3}),
138+
parameter<std::optional<double>>("RAD"),
139+
parameter<std::optional<double>>("RIGIDCOLOR"),
140+
});
141+
}
142+
143+
116144
FOUR_C_NAMESPACE_CLOSE

src/particle_engine/4C_particle_engine_particlereader.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "4C_config.hpp"
1212

13+
#include "4C_io_input_spec.hpp"
1314
#include "4C_particle_engine_typedefs.hpp"
1415

1516
#include <memory>
@@ -30,6 +31,11 @@ namespace PARTICLEENGINE
3031
void read_particles(Core::IO::InputFile& input, const std::string& section_name,
3132
std::vector<PARTICLEENGINE::ParticleObjShrdPtr>& particles);
3233

34+
/**
35+
* The InputSpec for a single particle line.
36+
*/
37+
[[nodiscard]] Core::IO::InputSpec create_particle_spec();
38+
3339
} // namespace PARTICLEENGINE
3440

3541
FOUR_C_NAMESPACE_CLOSE

0 commit comments

Comments
 (0)