-
Notifications
You must be signed in to change notification settings - Fork 24
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
minimize maxwellian vector capacity somewhat #898
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -44,7 +44,7 @@ class MaxwellianParticleInitializer : public ParticleInitializer<ParticleArray, | |||||||||||||||||||||||||
std::uint32_t const& nbrParticlesPerCell, std::optional<std::size_t> seed = {}, | ||||||||||||||||||||||||||
Basis const basis = Basis::Cartesian, | ||||||||||||||||||||||||||
std::array<InputFunction, 3> const& magneticField = {nullptr, nullptr, nullptr}, | ||||||||||||||||||||||||||
double densityCutOff = 1e-5) | ||||||||||||||||||||||||||
double const densityCutOff = 1e-5, double const over_alloc_factor = .1) | ||||||||||||||||||||||||||
: density_{density} | ||||||||||||||||||||||||||
, bulkVelocity_{bulkVelocity} | ||||||||||||||||||||||||||
, thermalVelocity_{thermalVelocity} | ||||||||||||||||||||||||||
|
@@ -54,6 +54,7 @@ class MaxwellianParticleInitializer : public ParticleInitializer<ParticleArray, | |||||||||||||||||||||||||
, nbrParticlePerCell_{nbrParticlesPerCell} | ||||||||||||||||||||||||||
, basis_{basis} | ||||||||||||||||||||||||||
, rngSeed_{seed} | ||||||||||||||||||||||||||
, over_alloc_factor_{over_alloc_factor} | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
@@ -92,6 +93,7 @@ class MaxwellianParticleInitializer : public ParticleInitializer<ParticleArray, | |||||||||||||||||||||||||
std::uint32_t nbrParticlePerCell_; | ||||||||||||||||||||||||||
Basis basis_; | ||||||||||||||||||||||||||
std::optional<std::size_t> rngSeed_; | ||||||||||||||||||||||||||
double over_alloc_factor_ = .1; | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
@@ -178,6 +180,17 @@ void MaxwellianParticleInitializer<ParticleArray, GridLayout>::loadParticles( | |||||||||||||||||||||||||
auto randGen = getRNG(rngSeed_); | ||||||||||||||||||||||||||
ParticleDeltaDistribution<double> deltaDistrib; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
auto const expected_size = std::accumulate(n, n + ndCellIndices.size(), std::size_t{0}, | ||||||||||||||||||||||||||
[&](auto const& sum, auto const& density_value) { | ||||||||||||||||||||||||||
return (density_value > densityCutOff_) | ||||||||||||||||||||||||||
? sum + nbrParticlePerCell_ | ||||||||||||||||||||||||||
: sum; | ||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||
Comment on lines
+183
to
+188
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pass 'sum' by value in the lambda for 'std::accumulate' In the lambda function used with Apply this diff to adjust the lambda parameter: -auto const expected_size = std::accumulate(n, n + ndCellIndices.size(), std::size_t{0},
- [&](auto const& sum, auto const& density_value) {
+auto const expected_size = std::accumulate(n, n + ndCellIndices.size(), std::size_t{0},
+ [&](auto sum, auto const& density_value) { 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
auto const incoming_estimate | ||||||||||||||||||||||||||
= (layout.AMRBox().surface_cell_count() * (nbrParticlePerCell_ * over_alloc_factor_)); | ||||||||||||||||||||||||||
particles.reserve(expected_size + incoming_estimate); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
for (std::size_t flatCellIdx = 0; flatCellIdx < ndCellIndices.size(); ++flatCellIdx) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
if (n[flatCellIdx] < densityCutOff_) | ||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ NO_DISCARD auto cellAsPoint(Particle const& particle) | |
template<size_t dim> | ||
struct Particle | ||
{ | ||
std::array<std::uint8_t, 3> constexpr static sizes = {52, 64, 76}; | ||
static_assert(dim > 0 and dim < 4, "Only dimensions 1,2,3 are supported."); | ||
static const size_t dimension = dim; | ||
|
||
|
@@ -54,12 +55,11 @@ struct Particle | |
|
||
Particle() = default; | ||
|
||
double weight = 0; | ||
double charge = 0; | ||
|
||
std::array<int, dim> iCell = ConstArray<int, dim>(); | ||
std::array<double, dim> delta = ConstArray<double, dim>(); | ||
std::array<double, 3> v = ConstArray<double, 3>(); | ||
double weight = 0; // 8 | ||
double charge = 0; // 8 | ||
std::array<int, dim> iCell = ConstArray<int, dim>(); // 4 * dim | ||
std::array<double, dim> delta = ConstArray<double, dim>(); // 8 * dim | ||
std::array<double, 3> v = ConstArray<double, 3>(); // 8 * 3 | ||
Comment on lines
+58
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider a more maintainable approach for documenting member sizes. While the added size information is useful, it might become outdated if types change. Consider using Example implementation: static_assert(sizeof(weight) == 8, "Unexpected size for weight");
static_assert(sizeof(charge) == 8, "Unexpected size for charge");
static_assert(sizeof(iCell) == 4 * dim, "Unexpected size for iCell");
static_assert(sizeof(delta) == 8 * dim, "Unexpected size for delta");
static_assert(sizeof(v) == 8 * 3, "Unexpected size for v"); Alternatively, you could use Doxygen-style comments for better documentation: /// @brief Particle weight
/// @details Size: 8 bytes
double weight = 0; |
||
|
||
NO_DISCARD bool operator==(Particle<dim> const& that) const | ||
{ | ||
|
@@ -121,9 +121,9 @@ inline constexpr auto is_phare_particle_type | |
|
||
template<std::size_t dim, template<std::size_t> typename ParticleA, | ||
template<std::size_t> typename ParticleB> | ||
NO_DISCARD typename std::enable_if_t< | ||
is_phare_particle_type<dim, ParticleA<dim>> and is_phare_particle_type<dim, ParticleB<dim>>, | ||
bool> | ||
NO_DISCARD typename std::enable_if_t<is_phare_particle_type<dim, ParticleA<dim>> | ||
and is_phare_particle_type<dim, ParticleB<dim>>, | ||
bool> | ||
operator==(ParticleA<dim> const& particleA, ParticleB<dim> const& particleB) | ||
{ | ||
return particleA.weight == particleB.weight and // | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ template<typename Type, std::size_t dim> | |
struct Box | ||
{ | ||
static const size_t dimension = dim; | ||
|
||
using value_type = Type; | ||
|
||
Point<Type, dim> lower; | ||
Point<Type, dim> upper; | ||
|
@@ -130,9 +130,16 @@ struct Box | |
return iterator{this, {upper[0] + 1, upper[1] + 1, upper[2] + 1}}; | ||
} | ||
} | ||
using value_type = Type; | ||
|
||
|
||
NO_DISCARD auto surface_cell_count() const | ||
{ | ||
// assumes box never smaller than 3 in any direction | ||
auto const shape_ = shape(); | ||
auto const nested = shape_ - 2; | ||
nicolasaunai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return core::product(shape_) - core::product(nested); | ||
} | ||
|
||
Comment on lines
+135
to
+142
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Add runtime validation for box size assumption. The review comment is valid. The codebase shows no existing validation for the minimum box size requirement, and tests reveal that boxes smaller than 3 units are possible. The suggested runtime check is necessary to prevent undefined behavior in surface_cell_count(). 🔗 Analysis chainAdd runtime validation for box size assumption. The function assumes the box is never smaller than 3 in any direction, but this assumption is not enforced. Consider adding a runtime check. NO_DISCARD auto surface_cell_count() const
{
// assumes box never smaller than 3 in any direction
+ for (std::size_t i = 0; i < dim; ++i) {
+ if (shape()[i] < 3) {
+ throw std::invalid_argument("Box must be at least 3 units in each dimension");
+ }
+ }
auto const shape_ = shape();
auto const nested = shape_ - 2;
return core::product(shape_) - core::product(nested);
} 🏁 Scripts executedThe following scripts were executed for the analysis: Error: No shell scripts found Script: #!/bin/bash
# Find box-related files
fd box
# Look for box class definition and size-related code
ast-grep --pattern 'class box'
# Search for any existing size validations in box-related files
rg "size|shape|dimension" -A 3 -B 3 src/core/utilities/box/
Length of output: 8065 Script: #!/bin/bash
# Look for test cases involving box size or surface cells
rg -A 5 -B 5 "surface_cell_count|Box.*[<{].*[>}]" tests/core/utilities/box/test_box.cpp
# Check if there are any small box tests
rg "Box.*[<{].*[>}].*\{.*[123].*\}" tests/core/utilities/box/test_box.cpp
Length of output: 4878 |
||
NO_DISCARD constexpr static std::size_t nbrRemainBoxes() | ||
{ | ||
if constexpr (dim == 1) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specify template arguments for
std::array
declarationThe declaration of
particlesArrays
is missing the required template arguments forstd::array
. Please specify the element type and the size to ensure proper compilation.Apply this diff to correct the declaration:
📝 Committable suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it compiles without the templates, what makes you think it should have the templates?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PhilipDeegan, you're right—since the code compiles and class template argument deduction is available from C++17 onwards, your code is valid as is. I apologize for the confusion.
(_/)
(._.)
✏️ Learnings added