Update source_cell module#7625
Open
mohanchen wants to merge 14 commits into
Open
Conversation
added 3 commits
July 13, 2026 15:50
…up() and read_atom_positions()
### Why
Previously `Magnetism::start_mag` had no well-defined single owner for memory
allocation. Even though `UnitCell::setup_cell()` happened to resize it, any
caller that invoked `unitcell::read_atom_positions()` directly (as most unit
tests did) was required to *externally* call
`ucell->magnet.start_mag.resize(ucell->ntype)` first. This undocumented
implicit contract produced dozens of scattered, copy-paste pre-conditions
throughout the test harness and made the API easy to misuse.
### What
1. Proactive initialization in `UnitCell::setup()`
- As soon as `ntype` is set, resize `start_mag` to `ntype_in` with value 0.0.
- This covers every test helper that already goes through `setup()` so the
callers no longer need to manually size and zero-initialize the vector.
2. Defensive auto-resize inside `unitcell::read_atom_positions()`
- Before any access to `start_mag[it]`, check that its size matches
`ucell.ntype` and resize if needed.
- This guards the remaining code paths that set `ntype` by hand and
guarantees the consumer owns its pre-conditions.
3. Removed the now-redundant explicit resize in `UnitCell::setup_cell()`.
Kept the `assert(ntype > 0)` validity check; renumbered the following
step comment from `(2)` to `(1)`.
4. Cleaned up ~20+ copies of the redundant preparatory lines across the
test suites:
- 7 `prepare_unitcell.h` helpers (source_cell, source_cell/module_neighbor,
source_estate, source_estate/module_dm, source_lcao/module_hcontainer,
source_io, source_io/test_serial): removed
`ucell->magnet.start_mag.resize(ntype)` and the loop body
`start_mag[it] = 0.0`.
- Unit tests `unitcell_test.cpp`, `unitcell_test_pw.cpp` and
`unitcell_test_setupcell.cpp`: removed the now-unnecessary "mandatory
preliminaries" resize block before `read_atom_positions`.
### Files touched (12)
source/source_cell/unitcell.cpp
source/source_cell/read_atoms.cpp
source/source_cell/module_neighbor/test/prepare_unitcell.h
source/source_cell/test/prepare_unitcell.h
source/source_cell/test/unitcell_test.cpp
source/source_cell/test/unitcell_test_setupcell.cpp
source/source_cell/test_pw/unitcell_test_pw.cpp
source/source_estate/test/prepare_unitcell.h
source/source_estate/module_dm/test/prepare_unitcell.h
source/source_io/test/prepare_unitcell.h
source/source_io/test_serial/prepare_unitcell.h
source/source_lcao/module_hcontainer/test/prepare_unitcell.h
Behavior change: none; resize is idempotent and every removed site defaulted
to the same zero value the new helpers already use.
Critsium-xy
approved these changes
Jul 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Update source_cell module