Skip to content

Update source_cell module#7625

Open
mohanchen wants to merge 14 commits into
deepmodeling:developfrom
mohanchen:2026-07-12
Open

Update source_cell module#7625
mohanchen wants to merge 14 commits into
deepmodeling:developfrom
mohanchen:2026-07-12

Conversation

@mohanchen

Copy link
Copy Markdown
Collaborator

Update source_cell module

@mohanchen mohanchen added the Refactor Refactor ABACUS codes label Jul 12, 2026
@mohanchen mohanchen requested a review from Critsium-xy July 13, 2026 07:30
abacus_fixer 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Refactor Refactor ABACUS codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants