Skip to content

Particle: accessor for all state data - #2228

Open
jeremylt wants to merge 4 commits into
4C-multiphysics:mainfrom
jeremylt:jeremy/null-state
Open

Particle: accessor for all state data#2228
jeremylt wants to merge 4 commits into
4C-multiphysics:mainfrom
jeremylt:jeremy/null-state

Conversation

@jeremylt

@jeremylt jeremylt commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Description and Context

We use *get_ptr_to_state* to do two different tasks - accessing the particle state for a single particle and accessing the particle state for all particles. This works fine until we try to access the state for an empty container and get an error when 4C is built with assertions (see #2224).

This PR adds overloaded versions of *get_ptr_to_state* that drops the index argument. In these, if the container is empty, then a nullptr is returned. This should be perfectly fine, as in these cases the array should be accessed in a loop over the number of particles in the container, so this nullptr should never be dereferenced. This matches with conventions I've seen in other projects. The alternative would be to insert checks at every callsite for *get_ptr_to_state* that is accessing the state for all particles and prevent the call if the container is empty. That is a lot of places in #2224, so I think this solution is much better.

  • ToDo: I need to add some tests

Related Issues and Pull Requests

#2148 #2224

Disclosure of AI assistance

None

@jeremylt jeremylt added status: under review Issues undergoing review during a pull request team: particle labels Aug 27, 2026
@ppraegla

Copy link
Copy Markdown
Member

The error you are seeing in #2224 is not due to an empty particle container. I looked at the following output from the log

PROC 0 ERROR in /__w/4C/4C/src/particle/src/engine/4C_particle_engine_container.cpp:243:
particle state 'density' not stored in container!
------------------
 0# void FourC::Core::Internal::format_and_throw_error<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::source_location const&, std::basic_format_string<char, std::type_identity<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::type>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&) in /__w/4C/4C/build/lib4C.so
 1# FourC::Particle::ParticleContainer::get_ptr_to_state(FourC::Particle::ParticleState, int) const in /__w/4C/4C/build/lib4C.so
 2# FourC::Particle::ParticleContainerBundle::get_ptrs_to_state(FourC::Particle::ParticleState, std::optional<std::set<FourC::Particle::ParticleType, std::less<FourC::Particle::ParticleType>, std::allocator<FourC::Particle::ParticleType> > >, std::optional<FourC::Particle::ParticleStatus>) const in /__w/4C/4C/build/lib4C.so
 3# FourC::Particle::SPHBoundaryParticleAdami::init_boundary_particle_states(std::vector<double, std::allocator<double> >&) in /__w/4C/4C/build/lib4C.so
 4# FourC::Particle::ParticleInteractionSPH::evaluate_interactions() in /__w/4C/4C/build/lib4C.so

density is a state that does not exist for all types; e.g., BoundaryPhase and RigidPhase do not store density in the state vector because it is constant for all particles. So, when you build the ConstParticleContainerBundleStatePtrs for density, you loop over all types and try to access the density state. But the density state does not exist for some types, hence the error.

@jeremylt

Copy link
Copy Markdown
Contributor Author

No, I am seeing the error I am talking about here. It's just not the only error:

[6](https://github.com/4C-multiphysics/4C/actions/runs/32969496598/job/98184226173?pr=2224#step:7:6617)
MPI_ABORT was invoked on rank 1 in communicator MPI_COMM_WORLD
with errorcode 1.

NOTE: invoking MPI_ABORT causes Open MPI to kill all MPI processes.
You may or may not see output from other processes, depending on
exactly when Open MPI kills them.
--------------------------------------------------------------------------
PROC 0 ERROR in /__w/4C/4C/src/particle/src/engine/4C_particle_engine_container.cpp:246:
can not return pointer to state of particle as index 0 out of bounds!
------------------
 0# void FourC::Core::Internal::format_and_throw_error<int&>(std::source_location const&, std::basic_format_string<char, std::type_identity<int&>::type>, int&) in /__w/4C/4C/build/lib4C.so
 1# FourC::Particle::ParticleContainer::get_ptr_to_state(FourC::Particle::ParticleState, int) const in /__w/4C/4C/build/lib4C.so
 2# FourC::Particle::ParticleContainerBundle::get_ptrs_to_state(FourC::Particle::ParticleState, std::optional<std::set<FourC::Particle::ParticleType, std::less<FourC::Particle::ParticleType>, std::allocator<FourC::Particle::ParticleType> > >, std::optional<FourC::Particle::ParticleStatus>) const in /__w/4C/4C/build/lib4C.so
 3# FourC::Particle::SPHNeighborPairs::evaluate_particle_pairs() in /__w/4C/4C/build/lib4C.so
 4# FourC::Particle::SPHNeighborPairs::evaluate_neighbor_pairs() in /__w/4C/4C/build/lib4C.so
 5# FourC::Particle::ParticleInteractionSPH::evaluate_interactions() in /__w/4C/4C/build/lib4C.so
 6# FourC::Particle::ParticleAlgorithm::evaluate_time_step() in /__w/4C/4C/build/lib4C.so
 7# FourC::Particle::ParticleAlgorithm::setup_initial_states() in /__w/4C/4C/build/lib4C.so
 8# FourC::Particle::ParticleAlgorithm::setup() in /__w/4C/4C/build/lib4C.so
 9# FourC::PaSI::PartitionedAlgo::setup() in /__w/4C/4C/build/lib4C.so
10# FourC::PaSI::PasiPartTwoWayCoup::setup() in /__w/4C/4C/build/lib4C.so
11# FourC::pasi_dyn() in /__w/4C/4C/build/lib4C.so
12# entrypoint_switch() in /__w/4C/4C/build/4C
13# run(FourC::CommandlineArguments&, FourC::Core::Communication::Communicators&) in /__w/4C/4C/build/4C
14# main in /__w/4C/4C/build/4C
15# 0x00007F601444E1CA in /lib/x86_64-linux-gnu/libc.so.6
16# __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6
17# _start in /__w/4C/4C/build/4C

@ppraegla ppraegla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to distinguish between access to state vector and access to state vector for a specific particle.

You missed one call of get_ptr...(...,0) in SPHPeridynamic::compute_acceleration()

Comment thread src/particle/src/algorithm/4C_particle_algorithm_result_test.cpp Outdated
Comment thread src/particle/tests/4C_particle_container_test.cpp Outdated
@jeremylt

Copy link
Copy Markdown
Contributor Author

Note - this PR is draft because I'm still working on it. I am currently eating lunch

@jeremylt

Copy link
Copy Markdown
Contributor Author

Ok, I think this is doing what I need now, but I'm waiting for the extra checks in CI to verify my local test suite runs for #2224. Then I'll swap this PR out of draft (though that PR will have more work in addition to the changes in this branch)

@jeremylt
jeremylt marked this pull request as ready for review August 28, 2026 10:10
@jeremylt

Copy link
Copy Markdown
Contributor Author

Ok, looks like this now does what I want

@jeremylt
jeremylt requested a review from ppraegla August 31, 2026 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: under review Issues undergoing review during a pull request team: particle

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants