6 #ifndef SWARM_STATE_H_INC_ 7 #define SWARM_STATE_H_INC_ 18 namespace Portage {
namespace Meshfree {
42 : num_local_points_(swarm.num_owned_particles())
50 explicit SwarmState(
int size) : num_local_points_(size) {}
62 template<
typename State>
63 SwarmState(State
const& state, Wonton::Entity_kind kind) {
66 auto const field_names = state.names();
69 for (
auto&& name : field_names) {
70 if (state.get_entity(name) == kind) {
71 num_entities = state.get_data_size(kind, name);
76 num_local_points_ = num_entities;
79 for (
auto&& name : field_names) {
80 if (state.get_entity(name) == kind) {
83 state.mesh_get_data(kind, name, &values);
84 assert(values !=
nullptr);
87 auto& field = fields_dbl_[name];
88 field.resize(num_entities);
89 std::copy(values, values + num_entities, field.begin());
108 template<
typename State>
109 SwarmState(std::vector<State*>
const& states, Wonton::Entity_kind kind) {
111 auto const& names = states[0]->names();
112 int const num_fields = names.size();
113 int const num_states = states.size();
116 for (
auto&& state : states) {
117 auto current = state->names();
118 if (current.size() == unsigned(num_fields)) {
119 for (
int i = 0; i < num_fields; ++i)
120 if (names[i] != current[i])
121 throw std::runtime_error(
"field names do not match");
123 throw std::runtime_error(
"field names do not match");
127 std::vector<int> sizes[num_fields];
129 for (
int i = 0; i < num_fields; ++i) {
130 auto const& name = names[i];
131 for (
int j = 0; j < num_states; ++j) {
132 auto const& state = *(states[j]);
133 if (state.get_entity(name) == kind) {
134 sizes[i].emplace_back(state.get_data_size(kind, name));
140 for (
int i = 0; i < num_fields; ++i) {
141 for (
int j = 0; j < num_states; ++j) {
142 assert(sizes[0][j] > 0);
143 if (sizes[i][j] != sizes[0][j])
144 throw std::runtime_error(
"field sizes do not match");
149 int num_entities = 0;
150 int offset[num_states];
151 for (
int i = 0; i < num_states; i++) {
152 offset[i] = num_entities;
153 num_entities += sizes[0][i];
156 num_local_points_ = num_entities;
159 for (
auto&& name : names) {
161 auto& field = fields_dbl_[name];
162 field.resize(num_entities);
164 for (
int i = 0; i < num_states; ++i) {
166 double* values =
nullptr;
167 states[i]->mesh_get_data(kind, name, &values);
168 assert(values !=
nullptr);
170 std::copy(values, values + sizes[0][i], field.begin() + offset[i]);
188 template<
typename T =
double>
191 static_assert(std::is_arithmetic<T>::value,
"only numeric fields");
193 assert(value.size() == unsigned(num_local_points_));
195 if (std::is_integral<T>::value) {
196 auto& field = fields_int_[name];
197 field.resize(value.size());
198 std::copy(value.begin(), value.end(), field.begin());
200 auto& field = fields_dbl_[name];
201 field.resize(value.size());
202 std::copy(value.begin(), value.end(), field.begin());
206 #ifdef PORTAGE_ENABLE_THRUST 214 template<
typename T =
double>
215 void add_field(std::string name, std::vector<T>
const& value) {
217 static_assert(std::is_arithmetic<T>::value,
"only numeric fields");
219 assert(value.size() == unsigned(num_local_points_));
221 if (std::is_integral<T>::value) {
222 auto& field = fields_int_[name];
223 field.resize(value.size());
224 std::copy(value.begin(), value.end(), field.begin());
226 auto& field = fields_dbl_[name];
227 field.resize(value.size());
228 std::copy(value.begin(), value.end(), field.begin());
240 template<
typename T =
double>
241 void add_field(std::string name,
const T*
const value) {
243 static_assert(std::is_arithmetic<T>::value,
"only numeric fields");
245 assert(value !=
nullptr);
247 if (std::is_integral<T>::value) {
248 auto& field = fields_int_[name];
249 field.resize(num_local_points_);
250 std::copy(value, value + num_local_points_, field.begin());
252 auto& field = fields_dbl_[name];
253 field.resize(num_local_points_);
254 std::copy(value, value + num_local_points_, field.begin());
265 template<
typename T=
double>
268 static_assert(std::is_arithmetic<T>::value,
"only numeric fields");
270 if (std::is_integral<T>::value) {
271 auto& field = fields_int_[name];
272 field.resize(num_local_points_, value);
274 auto& field = fields_dbl_[name];
275 field.resize(num_local_points_, value);
290 assert(fields_int_.count(name));
292 return const_cast<T&
>(fields_int_.at(name));
306 assert(fields_dbl_.count(name));
308 return const_cast<T&
>(fields_dbl_.at(name));
326 template<
typename T =
double>
329 static_assert(std::is_arithmetic<T>::value,
"only numeric fields");
330 assert(value !=
nullptr);
332 if (std::is_integral<T>::value) {
333 assert(fields_int_.count(name));
334 auto& field = fields_int_[name];
335 std::copy(field.begin(), field.end(), value);
337 assert(fields_dbl_.count(name));
338 auto& field = fields_dbl_[name];
339 std::copy(field.begin(), field.end(), value);
356 template<
typename T =
double>
359 static_assert(std::is_arithmetic<T>::value,
"only numeric fields");
361 std::vector<std::string> list;
362 if (std::is_integral<T>::value) {
363 for (
auto&& field : fields_int_)
364 list.emplace_back(field.first);
366 for (
auto&& field : fields_dbl_)
367 list.emplace_back(field.first);
383 static_assert(std::is_arithmetic<T>::value,
"only numeric fields");
385 if (std::is_integral<T>::value) {
386 assert(fields_int_.count(name));
387 auto& field = fields_int_[name];
388 field.insert(field.begin(), values.begin(), values.end());
390 assert(fields_dbl_.count(name));
391 auto& field = fields_dbl_[name];
392 field.insert(field.end(), values.begin(), values.end());
398 int num_local_points_ = 0;
401 std::map<std::string, Portage::vector<int>> fields_int_ {};
402 std::map<std::string, Portage::vector<double>> fields_dbl_ {};
407 #endif // SWARM_STATE_H_INC_ void copy_field(std::string name, T *value)
Retrieve the specified field.
Definition: swarm_state.h:327
std::vector< T > vector
Definition: portage.h:238
int get_size()
Get number of particles.
Definition: swarm_state.h:348
void add_field(std::string name, const T *const value)
Set a field on the swarm.
Definition: swarm_state.h:241
Particle field state class.
Definition: swarm_state.h:28
SwarmState(int size)
Initialize with a field size.
Definition: swarm_state.h:50
SwarmState(Swarm< dim > const &swarm)
Initialize from a reference swarm.
Definition: swarm_state.h:41
~SwarmState()=default
Destructor.
Portage::vector< double > & get_field(std::string name) const
Definition: swarm_state.h:316
Definition: coredriver.h:42
SwarmState()=default
Create an empty state.
An effective "mesh" class for a collection disconnected points (particles).
Definition: swarm.h:35
std::vector< std::string > get_field_names()
Retrieve the list of field names.
Definition: swarm_state.h:357
void extend_field(std::string name, Portage::vector< T > const &values)
Extend the field with the given subfield.
Definition: swarm_state.h:381
Portage::vector< int > & get_field_int(std::string name) const
Retrieve a specified integer field.
Definition: swarm_state.h:289
void add_field(std::string name, T value)
Set an empty field on the swarm.
Definition: swarm_state.h:266
Portage::vector< double > & get_field_dbl(std::string name) const
Retrieve a specified real field.
Definition: swarm_state.h:305
SwarmState(std::vector< State *> const &states, Wonton::Entity_kind kind)
Create the swarm state from set of mesh states wrappers.
Definition: swarm_state.h:109
void add_field(std::string name, Portage::vector< T > const &value)
Set a field on the swarm.
Definition: swarm_state.h:189
SwarmState(State const &state, Wonton::Entity_kind kind)
Create the swarm state from a given mesh state wrapper.
Definition: swarm_state.h:63