@@ -82,8 +82,9 @@ void MDCell::sync_backing_unitcell_owned_atoms_()
8282 }
8383}
8484
85- #ifdef __MPI
86- void MDCell::initialize_from_ucell_ (UnitCell& ucell, MPI_Comm comm, double cutoff, double skin)
85+ void MDCell::initialize_from_unitcell (UnitCell& ucell,
86+ double skin,
87+ const ModuleBase::CommunicationDomain& comm_domain)
8788{
8889 backing_unitcell_ = &ucell;
8990 nat_ = ucell.nat ;
@@ -100,55 +101,21 @@ void MDCell::initialize_from_ucell_(UnitCell& ucell, MPI_Comm comm, double cutof
100101 type_masses_[static_cast <std::size_t >(it)] = ucell.atoms [it].mass ;
101102 type_atom_counts_[static_cast <std::size_t >(it)] = ucell.atoms [it].na ;
102103 }
103- comm_ = comm;
104- cutoff_ = cutoff;
104+ cutoff_ = 0.0 ;
105105 skin_ = skin;
106-
106+ neighbor_search_.reset ();
107+ neighbor_layout_valid_ = false ;
107108 owned_atoms_.clear ();
108109 ghost_atoms_.clear ();
109- MPI_Comm_rank (comm_, &rank_);
110- MPI_Comm_size (comm_, &size_);
111110
112- decomp_.init (comm_, latvec_, lat0_, cutoff_, skin_);
113- decomp_.split_owned_atoms_from_ucell (ucell, owned_atoms_);
114- clear_forces_ (owned_atoms_);
115- exchange_ghost_atoms ();
116- }
117-
118- void MDCell::initialize_from_owned_atoms_ (MPI_Comm comm, double cutoff, double skin)
119- {
120- comm_ = comm;
121- cutoff_ = cutoff;
122- skin_ = skin;
111+ #ifdef __MPI
112+ comm_ = comm_domain.communicator ();
123113 MPI_Comm_rank (comm_, &rank_);
124114 MPI_Comm_size (comm_, &size_);
125- decomp_.init (comm_, latvec_, lat0_, cutoff_, skin_);
126- clear_forces_ (owned_atoms_);
127- exchange_ghost_atoms ();
128- }
115+ decomp_.init (comm_, latvec_, lat0_, 0.0 , 0.0 );
116+ decomp_.split_owned_atoms_from_ucell (ucell, owned_atoms_);
129117#else
130- void MDCell::initialize_from_ucell_ (UnitCell& ucell, double cutoff, double skin)
131- {
132- backing_unitcell_ = &ucell;
133- nat_ = ucell.nat ;
134- lat0_ = ucell.lat0 ;
135- omega_ = ucell.omega ;
136- latvec_ = ucell.latvec ;
137- gt_ = ucell.GT ;
138- type_labels_.resize (static_cast <std::size_t >(ucell.ntype ));
139- type_masses_.resize (static_cast <std::size_t >(ucell.ntype ));
140- type_atom_counts_.resize (static_cast <std::size_t >(ucell.ntype ));
141- for (int it = 0 ; it < ucell.ntype ; ++it)
142- {
143- type_labels_[static_cast <std::size_t >(it)] = ucell.atoms [it].label ;
144- type_masses_[static_cast <std::size_t >(it)] = ucell.atoms [it].mass ;
145- type_atom_counts_[static_cast <std::size_t >(it)] = ucell.atoms [it].na ;
146- }
147- cutoff_ = cutoff;
148- skin_ = skin;
149- owned_atoms_.clear ();
150- ghost_atoms_.clear ();
151-
118+ static_cast <void >(comm_domain);
152119 for (int it = 0 ; it < ucell.ntype ; ++it)
153120 {
154121 for (int ia = 0 ; ia < ucell.atoms [it].na ; ++ia)
@@ -164,30 +131,9 @@ void MDCell::initialize_from_ucell_(UnitCell& ucell, double cutoff, double skin)
164131 0 ));
165132 }
166133 }
167- exchange_ghost_atoms ();
168- }
169-
170- void MDCell::initialize_from_owned_atoms_ (double cutoff, double skin)
171- {
172- cutoff_ = cutoff;
173- skin_ = skin;
174- clear_forces_ (owned_atoms_);
175- exchange_ghost_atoms ();
176- }
177134#endif
178135
179-
180- void MDCell::initialize_from_unitcell (UnitCell& ucell,
181- double cutoff,
182- double skin,
183- const ModuleBase::CommunicationDomain& comm_domain)
184- {
185- #ifdef __MPI
186- initialize_from_ucell_ (ucell, comm_domain.communicator (), cutoff, skin);
187- #else
188- static_cast <void >(comm_domain);
189- initialize_from_ucell_ (ucell, cutoff, skin);
190- #endif
136+ clear_forces_ (owned_atoms_);
191137}
192138
193139void MDCell::initialize_from_owned_atoms (const ModuleBase::Matrix3& latvec,
@@ -199,7 +145,6 @@ void MDCell::initialize_from_owned_atoms(const ModuleBase::Matrix3& latvec,
199145 const std::vector<std::string>& type_labels,
200146 const std::vector<double >& type_masses,
201147 const std::vector<std::int64_t >& type_atom_counts,
202- double cutoff,
203148 double skin,
204149 const ModuleBase::CommunicationDomain& comm_domain)
205150{
@@ -212,12 +157,42 @@ void MDCell::initialize_from_owned_atoms(const ModuleBase::Matrix3& latvec,
212157 type_labels_ = type_labels;
213158 type_masses_ = type_masses;
214159 type_atom_counts_ = type_atom_counts;
160+ backing_unitcell_ = nullptr ;
161+ cutoff_ = 0.0 ;
162+ skin_ = skin;
163+ neighbor_search_.reset ();
164+ neighbor_layout_valid_ = false ;
165+ ghost_atoms_.clear ();
215166#ifdef __MPI
216- initialize_from_owned_atoms_ (comm_domain.communicator (), cutoff, skin);
167+ comm_ = comm_domain.communicator ();
168+ MPI_Comm_rank (comm_, &rank_);
169+ MPI_Comm_size (comm_, &size_);
217170#else
218171 static_cast <void >(comm_domain);
219- initialize_from_owned_atoms_ (cutoff, skin);
220172#endif
173+ clear_forces_ (owned_atoms_);
174+ }
175+
176+ void MDCell::initialize_neighbors (double cutoff)
177+ {
178+ if (cutoff <= 0.0 )
179+ {
180+ throw std::runtime_error (" MDCell neighbor cutoff must be positive." );
181+ }
182+
183+ cutoff_ = cutoff;
184+ neighbor_search_.reset ();
185+ neighbor_layout_valid_ = false ;
186+
187+ #ifdef __MPI
188+ if (comm_ == MPI_COMM_NULL )
189+ {
190+ throw std::runtime_error (" MDCell communication domain is not initialized." );
191+ }
192+ decomp_.init (comm_, latvec_, lat0_, cutoff_, skin_);
193+ #endif
194+
195+ migrate_owned_atoms ();
221196}
222197
223198#ifdef __MPI
@@ -341,6 +316,11 @@ void MDCell::migrate_owned_atoms()
341316
342317void MDCell::prepare_neighbors ()
343318{
319+ if (cutoff_ <= 0.0 )
320+ {
321+ throw std::runtime_error (" MDCell neighbors must be initialized before use." );
322+ }
323+
344324 bool rebuild = !neighbor_layout_valid_ || neighbor_reference_frac_.size () != owned_atoms_.size ();
345325 double local_max_displacement = 0.0 ;
346326 if (!rebuild)
0 commit comments