Skip to content

Commit f42ad14

Browse files
author
Fei Yang
committed
refactor(md): initialize MDCell explicitly
1 parent 7c5362c commit f42ad14

13 files changed

Lines changed: 96 additions & 65 deletions

source/source_cell/distributed_mdcell_reader.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -356,18 +356,19 @@ MDCell DistributedMDCellReader::read_stru(const std::string& stru_file,
356356
std::int64_t nat = 0;
357357
const std::vector<LocalAtom> owned_atoms = read_owned_atoms(ifs, metadata, primitive_latvec, primitive_gt,
358358
cell_replica, cutoff, skin, nat, communication_domain);
359-
MDCell mdcell(metadata.latvec,
360-
metadata.gt,
361-
metadata.lat0,
362-
metadata.omega,
363-
nat,
364-
owned_atoms,
365-
metadata.labels,
366-
metadata.masses,
367-
metadata.type_atom_counts,
368-
cutoff,
369-
skin,
370-
communication_domain);
359+
MDCell mdcell;
360+
mdcell.initialize_from_owned_atoms(metadata.latvec,
361+
metadata.gt,
362+
metadata.lat0,
363+
metadata.omega,
364+
nat,
365+
owned_atoms,
366+
metadata.labels,
367+
metadata.masses,
368+
metadata.type_atom_counts,
369+
cutoff,
370+
skin,
371+
communication_domain);
371372
stru_metadata = metadata.stru_file_metadata;
372373
return mdcell;
373374
}

source/source_cell/md_cell.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <cmath>
99
#include <stdexcept>
1010

11+
MDCell::MDCell() = default;
1112
MDCell::~MDCell() = default;
1213
MDCell::MDCell(MDCell&&) = default;
1314
MDCell& MDCell::operator=(MDCell&&) = default;
@@ -176,10 +177,10 @@ void MDCell::initialize_from_owned_atoms_(double cutoff, double skin)
176177
#endif
177178

178179

179-
MDCell::MDCell(UnitCell& ucell,
180-
double cutoff,
181-
double skin,
182-
const ModuleBase::CommunicationDomain& communication_domain)
180+
void MDCell::initialize_from_unitcell(UnitCell& ucell,
181+
double cutoff,
182+
double skin,
183+
const ModuleBase::CommunicationDomain& communication_domain)
183184
{
184185
#ifdef __MPI
185186
initialize_from_ucell_(ucell, communication_domain.communicator(), cutoff, skin);
@@ -189,18 +190,18 @@ MDCell::MDCell(UnitCell& ucell,
189190
#endif
190191
}
191192

192-
MDCell::MDCell(const ModuleBase::Matrix3& latvec,
193-
const ModuleBase::Matrix3& gt,
194-
double lat0,
195-
double omega,
196-
std::int64_t nat,
197-
const std::vector<LocalAtom>& owned_atoms,
198-
const std::vector<std::string>& type_labels,
199-
const std::vector<double>& type_masses,
200-
const std::vector<std::int64_t>& type_atom_counts,
201-
double cutoff,
202-
double skin,
203-
const ModuleBase::CommunicationDomain& communication_domain)
193+
void MDCell::initialize_from_owned_atoms(const ModuleBase::Matrix3& latvec,
194+
const ModuleBase::Matrix3& gt,
195+
double lat0,
196+
double omega,
197+
std::int64_t nat,
198+
const std::vector<LocalAtom>& owned_atoms,
199+
const std::vector<std::string>& type_labels,
200+
const std::vector<double>& type_masses,
201+
const std::vector<std::int64_t>& type_atom_counts,
202+
double cutoff,
203+
double skin,
204+
const ModuleBase::CommunicationDomain& communication_domain)
204205
{
205206
latvec_ = latvec;
206207
gt_ = gt;

source/source_cell/md_cell.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,29 @@ class CommunicationDomain;
2323
class MDCell : public BaseCell
2424
{
2525
public:
26+
MDCell();
2627
~MDCell();
2728
MDCell(const MDCell&) = delete;
2829
MDCell& operator=(const MDCell&) = delete;
2930
MDCell(MDCell&&);
3031
MDCell& operator=(MDCell&&);
3132

32-
MDCell(UnitCell& ucell,
33-
double cutoff,
34-
double skin,
35-
const ModuleBase::CommunicationDomain& communication_domain);
36-
MDCell(const ModuleBase::Matrix3& latvec,
37-
const ModuleBase::Matrix3& gt,
38-
double lat0,
39-
double omega,
40-
std::int64_t nat,
41-
const std::vector<LocalAtom>& owned_atoms,
42-
const std::vector<std::string>& type_labels,
43-
const std::vector<double>& type_masses,
44-
const std::vector<std::int64_t>& type_atom_counts,
45-
double cutoff,
46-
double skin,
47-
const ModuleBase::CommunicationDomain& communication_domain);
33+
void initialize_from_unitcell(UnitCell& ucell,
34+
double cutoff,
35+
double skin,
36+
const ModuleBase::CommunicationDomain& communication_domain);
37+
void initialize_from_owned_atoms(const ModuleBase::Matrix3& latvec,
38+
const ModuleBase::Matrix3& gt,
39+
double lat0,
40+
double omega,
41+
std::int64_t nat,
42+
const std::vector<LocalAtom>& owned_atoms,
43+
const std::vector<std::string>& type_labels,
44+
const std::vector<double>& type_masses,
45+
const std::vector<std::int64_t>& type_atom_counts,
46+
double cutoff,
47+
double skin,
48+
const ModuleBase::CommunicationDomain& communication_domain);
4849

4950
#ifdef __MPI
5051
int mpi_rank() const;

source/source_cell/module_neighlist/test/distributed_mdcell_reader_test.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
static_assert(!std::is_copy_constructible<MDCell>::value, "MDCell must not be copy constructible.");
2121
static_assert(!std::is_copy_assignable<MDCell>::value, "MDCell must not be copy assignable.");
22+
static_assert(std::is_default_constructible<MDCell>::value, "MDCell must be default constructible.");
2223
static_assert(std::is_move_constructible<MDCell>::value, "MDCell must be move constructible.");
2324

2425
namespace
@@ -213,7 +214,8 @@ TEST(DistributedMDCellReaderTest, RestartStruPreservesAtomRecordsAcrossRanks)
213214
lattice.e11 = 20.0;
214215
lattice.e22 = 20.0;
215216
lattice.e33 = 20.0;
216-
MDCell mdcell(lattice,
217+
MDCell mdcell;
218+
mdcell.initialize_from_owned_atoms(lattice,
217219
lattice.Inverse(),
218220
1.0,
219221
1.0,

source/source_cell/module_neighlist/test/md_cell_migrate_mpi_test.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ TEST(MdCellMigrateMpiTest, AtomCrossingDomainMigratesToNewOwner)
5656
rank,
5757
rank));
5858
}
59-
MDCell mdcell(latvec,
59+
MDCell mdcell;
60+
mdcell.initialize_from_owned_atoms(latvec,
6061
latvec.Inverse(),
6162
1.0,
6263
1.0,
@@ -123,7 +124,8 @@ TEST(MdCellMigrateMpiTest, GhostForcesReturnToOwners)
123124
0,
124125
rank,
125126
rank));
126-
MDCell mdcell(latvec,
127+
MDCell mdcell;
128+
mdcell.initialize_from_owned_atoms(latvec,
127129
latvec.Inverse(),
128130
1.0,
129131
1.0,
@@ -178,7 +180,8 @@ TEST(MdCellMigrateMpiTest, SkinUpdatesFixedGhostLayoutBeforeRebuild)
178180
0,
179181
rank,
180182
rank));
181-
MDCell mdcell(latvec,
183+
MDCell mdcell;
184+
mdcell.initialize_from_owned_atoms(latvec,
182185
latvec.Inverse(),
183186
1.0,
184187
1.0,

source/source_main/driver_run.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ void Driver::driver_run()
132132
else
133133
{
134134
initialize_ucell();
135-
MDCell mdcell(ucell, 0.0, 0.0, communication_domain);
135+
MDCell mdcell;
136+
mdcell.initialize_from_unitcell(ucell, 0.0, 0.0, communication_domain);
136137
const MdStruFileMetadata stru_metadata = unitcell::make_md_stru_file_metadata(ucell);
137138
p_esolver->before_all_runners(ucell, PARAM.inp);
138139
Run_MD::md_line(mdcell, p_esolver, PARAM, stru_metadata);

source/source_md/test/fire_test.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ class FIREtest : public testing::Test
5050
Setcell::parameters(param_in.input);
5151

5252
p_esolver = new ModuleESolver::ESolver_LJ();
53-
mdcell = new MDCell(ucell, 8.5 * ModuleBase::ANGSTROM_AU, 0.0,
54-
ModuleBase::world_communication_domain());
53+
mdcell = new MDCell;
54+
mdcell->initialize_from_unitcell(ucell,
55+
8.5 * ModuleBase::ANGSTROM_AU,
56+
0.0,
57+
ModuleBase::world_communication_domain());
5558
p_esolver->before_all_runners(*mdcell, param_in.inp);
5659
mdrun = new FIRE(param_in, *mdcell);
5760
mdrun->setup(p_esolver, PARAM.sys.global_readin_dir);

source/source_md/test/langevin_test.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ class Langevin_test : public testing::Test
5050
Setcell::parameters(param_in.input);
5151

5252
p_esolver = new ModuleESolver::ESolver_LJ();
53-
mdcell = new MDCell(ucell, 8.5 * ModuleBase::ANGSTROM_AU, 0.0,
54-
ModuleBase::world_communication_domain());
53+
mdcell = new MDCell;
54+
mdcell->initialize_from_unitcell(ucell,
55+
8.5 * ModuleBase::ANGSTROM_AU,
56+
0.0,
57+
ModuleBase::world_communication_domain());
5558
p_esolver->before_all_runners(*mdcell, param_in.inp);
5659
mdrun = new Langevin(param_in, *mdcell);
5760
mdrun->setup(p_esolver, PARAM.sys.global_readin_dir);

source/source_md/test/md_func_test.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ TEST_F(MD_func_test, RescaleVel)
105105
TEST_F(MD_func_test, compute_stress)
106106
{
107107
const ModuleBase::Vector3<double> test_velocity(0.1, 0.2, 0.3);
108-
MDCell mdcell(ucell, 0.0, 0.0, ModuleBase::world_communication_domain());
108+
MDCell mdcell;
109+
mdcell.initialize_from_unitcell(ucell, 0.0, 0.0, ModuleBase::world_communication_domain());
109110
for (LocalAtom& atom : mdcell.mutable_owned_atoms())
110111
{
111112
atom.vel = test_velocity;
@@ -124,7 +125,8 @@ TEST_F(MD_func_test, compute_stress)
124125

125126
TEST_F(MD_func_test, dump_info)
126127
{
127-
MDCell mdcell(ucell, 0.0, 0.0, ModuleBase::world_communication_domain());
128+
MDCell mdcell;
129+
mdcell.initialize_from_unitcell(ucell, 0.0, 0.0, ModuleBase::world_communication_domain());
128130
for (LocalAtom& atom : mdcell.mutable_owned_atoms())
129131
{
130132
atom.vel = ModuleBase::Vector3<double>(0.0, 0.0, 0.0);
@@ -309,7 +311,8 @@ TEST_F(MD_func_test, current_md_info_mdcell_accepts_step_only_restart)
309311
file << 123;
310312
file.close();
311313

312-
MDCell mdcell(ucell, 0.0, 0.0, ModuleBase::world_communication_domain());
314+
MDCell mdcell;
315+
mdcell.initialize_from_unitcell(ucell, 0.0, 0.0, ModuleBase::world_communication_domain());
313316
int istep = -1;
314317
double temperature = 0.0;
315318
MD_func::current_md_info(mdcell, "./", istep, temperature);
@@ -321,7 +324,8 @@ TEST_F(MD_func_test, current_md_info_mdcell_accepts_step_only_restart)
321324

322325
TEST_F(MD_func_test, global_dof_mdcell)
323326
{
324-
MDCell mdcell(ucell, 0.0, 0.0, ModuleBase::world_communication_domain());
327+
MDCell mdcell;
328+
mdcell.initialize_from_unitcell(ucell, 0.0, 0.0, ModuleBase::world_communication_domain());
325329
EXPECT_EQ(MD_func::global_dof(mdcell), 9);
326330

327331
for (LocalAtom& atom : mdcell.mutable_owned_atoms())
@@ -333,7 +337,8 @@ TEST_F(MD_func_test, global_dof_mdcell)
333337

334338
TEST_F(MD_func_test, current_step_warning)
335339
{
336-
MDCell mdcell(ucell, 0.0, 0.0, ModuleBase::world_communication_domain());
340+
MDCell mdcell;
341+
mdcell.initialize_from_unitcell(ucell, 0.0, 0.0, ModuleBase::world_communication_domain());
337342
int istep = 0;
338343
double temperature = 0.0;
339344
EXPECT_EXIT(MD_func::current_md_info(mdcell, "./", istep, temperature), ::testing::ExitedWithCode(1), "");

source/source_md/test/msst_test.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ class MSST_test : public testing::Test
5050
Setcell::parameters(param_in.input);
5151

5252
p_esolver = new ModuleESolver::ESolver_LJ();
53-
mdcell = new MDCell(ucell, 8.5 * ModuleBase::ANGSTROM_AU, 0.0,
54-
ModuleBase::world_communication_domain());
53+
mdcell = new MDCell;
54+
mdcell->initialize_from_unitcell(ucell,
55+
8.5 * ModuleBase::ANGSTROM_AU,
56+
0.0,
57+
ModuleBase::world_communication_domain());
5558
p_esolver->before_all_runners(*mdcell, param_in.inp);
5659
mdrun = new MSST(param_in, *mdcell);
5760
mdrun->setup(p_esolver, PARAM.sys.global_readin_dir);

0 commit comments

Comments
 (0)