-
|
Following example creates coarse and fine boxes around origin and one particle at origin. Iterating over particles before #define AMREX_SPACEDIM 3
#include "AMReX.H"
#include "AMReX_MultiFab.H"
#include "AMReX_ParticleContainer.H"
#include "AMReX_ParticleInterpolators.H"
using namespace std;
using namespace amrex;
using ParticleContainer_ = ParticleContainer<3, 0, 0, 0>;
using Particle_ = ParticleContainer_::ParticleType;
int main(int argc, char* argv[]) {
Initialize(argc, argv); {
Box
box_coarse{IntVect{0, 0, 0}, IntVect{2, 2, 2}},
box_fine{IntVect{2, 2, 2}, IntVect{3, 3, 3}};
Geometry
geom_c{box_coarse, RealBox({-1.5,-1.5,-1.5}, {1.5,1.5,1.5}),
CoordSys::cartesian, Array<int, 3>{0, 0, 0}},
geom_f{box_fine, RealBox({-0.5,-0.5,-0.5}, {0.5,0.5,0.5}),
CoordSys::cartesian, Array<int, 3>{0, 0, 0}};
geom_f.refine(IntVect{2});
BoxArray ba_c, ba_f;
ba_c.define(box_coarse); ba_f.define(box_fine);
DistributionMapping dm_c{ba_c}, dm_f{ba_f};
MultiFab mf_c{ba_c, dm_c, 3, 0}, mf_f{ba_f, dm_f, 3, 0};
ParticleContainer_ particle_container(
{geom_c, geom_f}, {dm_c, dm_f},
{ba_c, ba_f}, Vector<int>{2});
for (auto iter = particle_container.MakeMFIter(0); iter.isValid(); ++iter) {
auto& particles = particle_container.GetParticles(0)[
std::make_pair(iter.index(), iter.LocalTileIndex())];
Particle_ particle;
for (int i = 0; i < 3; i++) {
particle.pos(i) = particle.rdata(i) = 0;
}
particles.push_back(particle);
}
for (
ParticleContainer_::ParIterType iter(particle_container, 0);
iter.isValid(); ++iter
) {
const auto& particles = iter.GetArrayOfStructs();
for (const auto& particle: particles) {
cout << "before:";
for (int i = 0; i < 3; i++) {
cout<<particle.pos(i)<<","<<particle.rdata(i)<<",";
} cout<<endl;
}
}
particle_container.Redistribute();
for (
ParticleContainer_::ParIterType iter(particle_container, 0);
iter.isValid(); ++iter
) {
const auto& particles = iter.GetArrayOfStructs();
for (const auto& particle: particles) {
cout << "after:";
for (int i = 0; i < 3; i++) {
cout<<particle.pos(i)<<","<<particle.rdata(i)<<",";
} cout<<endl;
}
}
} Finalize(); return 0;
} |
Beta Was this translation helpful? Give feedback.
Answered by
atmyers
Jan 30, 2026
Replies: 1 comment 1 reply
-
|
I think you need to set the id() and cpu() to something valid, or Redistribute will remove the particle. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
iljah
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you need to set the id() and cpu() to something valid, or Redistribute will remove the particle.