Skip to content

Commit 96903b8

Browse files
committed
refactor: parameterize symmetry in module_cell
1 parent 39760f5 commit 96903b8

12 files changed

Lines changed: 57 additions & 49 deletions

File tree

python/pyabacus/src/ModuleDriver/py_driver.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,8 @@ CalculationResult PyDriver::run(
428428
impl_->ucell_->setup_cell(PARAM.globalv.global_in_stru, GlobalV::ofs_running, PARAM.inp.symmetry_prec, PARAM.inp.dfthalf_type, PARAM.inp.pseudo_dir, PARAM.inp.nspin,
429429
PARAM.inp.basis_type, PARAM.inp.orbital_dir, PARAM.inp.init_wfc,
430430
PARAM.inp.onsite_radius, PARAM.globalv.deepks_setorb, PARAM.inp.rpa,
431-
PARAM.inp.fixed_atoms, PARAM.inp.noncolin, PARAM.inp.calculation, PARAM.inp.esolver_type);
431+
PARAM.inp.fixed_atoms, PARAM.inp.noncolin, PARAM.inp.calculation, PARAM.inp.esolver_type,
432+
std::stoi(PARAM.inp.symmetry));
432433

433434
// Check atomic structure
434435
unitcell::check_atomic_stru(*impl_->ucell_, PARAM.inp.min_dist_coef);

source/source_cell/read_atoms.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ bool unitcell::read_atom_positions(UnitCell& ucell,
2525
const bool fixed_atoms,
2626
const bool noncolin,
2727
const std::string& calculation,
28-
const std::string& esolver_type)
28+
const std::string& esolver_type,
29+
const int symmetry)
2930
{
3031
ModuleBase::TITLE("UnitCell","read_atom_positions");
3132

@@ -123,8 +124,21 @@ bool unitcell::read_atom_positions(UnitCell& ucell,
123124
}
124125
} // end for ntype
125126

126-
// Auto-set magnetization if needed
127-
unitcell::autoset_magnetization(ucell, nspin, ofs_running);
127+
// Auto-set magnetization if needed.
128+
// symmetry=1 means "analyze and preserve the symmetry of the initial magnetic
129+
// moment"; an all-zero moment is a legitimate nonmagnetic choice under the full
130+
// point group, so do not override it with an autoset seed. Warn instead.
131+
if (symmetry == 1)
132+
{
133+
ofs_running << "\n WARNING: initial magmom is all zero and symmetry=1; "
134+
<< "autoset magnetism is SKIPPED to preserve the symmetric (nonmagnetic) state.\n"
135+
<< " If spontaneous magnetism is expected, set magmom explicitly "
136+
<< "in STRU, or use symmetry=-1." << std::endl;
137+
}
138+
else
139+
{
140+
unitcell::autoset_magnetization(ucell, nspin, ofs_running);
141+
}
128142
} // end scan_begin
129143

130144
// Final validation and output

source/source_cell/read_atoms_helper.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "read_stru.h"
66
#include "print_cell.h"
77
#include "read_orb.h"
8-
#include "module_symmetry/symmetry.h" // for Symmetry::symm_flag (avoids depending on module_parameter)
98
#include <cmath>
109
#include <iostream>
1110
#include <sstream>
@@ -101,17 +100,6 @@ void autoset_magnetization(UnitCell& ucell, int nspin,
101100

102101
if (autoset_mag)
103102
{
104-
// symmetry=1 means "analyze and preserve the symmetry of the initial magnetic
105-
// moment"; an all-zero moment is a legitimate nonmagnetic choice under the full
106-
// point group, so do not override it with an autoset seed. Warn instead.
107-
if (ModuleSymmetry::Symmetry::symm_flag == 1)
108-
{
109-
ofs_running << "\n WARNING: initial magmom is all zero and symmetry=1; "
110-
<< "autoset magnetism is SKIPPED to preserve the symmetric (nonmagnetic) state.\n"
111-
<< " If spontaneous magnetism is expected, set magmom explicitly "
112-
<< "in STRU, or use symmetry=-1." << std::endl;
113-
return;
114-
}
115103
if(nspin==4)
116104
{
117105
for (int it = 0; it < ntype; it++)

source/source_cell/read_stru.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace unitcell
4242
const bool fixed_atoms,
4343
const bool noncolin,
4444
const std::string& calculation,
45-
const std::string& esolver_type);
45+
const std::string& esolver_type,
46+
const int symmetry);
4647
}
4748
#endif // READ_STRU_H

source/source_cell/test/support/mock_unitcell.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ void UnitCell::set_iat2itia() {}
2727
void UnitCell::setup_cell(const std::string& fn, std::ofstream& log, const double symmetry_prec, const int dfthalf_type, const std::string& pseudo_dir, const int nspin,
2828
const std::string& basis_type, const std::string& orbital_dir, const std::string& init_wfc,
2929
const double onsite_radius, const bool deepks_setorb, const bool rpa,
30-
const bool fixed_atoms, const bool noncolin, const std::string& calculation, const std::string& esolver_type) {}
30+
const bool fixed_atoms, const bool noncolin, const std::string& calculation, const std::string& esolver_type,
31+
const int symmetry) {}
3132

3233
bool UnitCell::if_atoms_can_move() const { return true; }
3334

source/source_cell/test/unitcell_test.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsS1)
12711271
unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
12721272
basis_type, orbital_dir, init_wfc,
12731273
onsite_radius, fixed_atoms, noncolin,
1274-
calculation, esolver_type);
1274+
calculation, esolver_type, 0);
12751275
ofs_running.close();
12761276
ofs_warning.close();
12771277
ifa.close();
@@ -1312,7 +1312,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsS2)
13121312
unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
13131313
basis_type, orbital_dir, init_wfc,
13141314
onsite_radius, fixed_atoms, noncolin,
1315-
calculation, esolver_type);
1315+
calculation, esolver_type, 0);
13161316
ofs_running.close();
13171317
ofs_warning.close();
13181318
ifa.close();
@@ -1353,7 +1353,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsS4Noncolin)
13531353
unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
13541354
basis_type, orbital_dir, init_wfc,
13551355
onsite_radius, fixed_atoms, noncolin,
1356-
calculation, esolver_type);
1356+
calculation, esolver_type, 0);
13571357
ofs_running.close();
13581358
ofs_warning.close();
13591359
ifa.close();
@@ -1394,7 +1394,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsS4Colin)
13941394
unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
13951395
basis_type, orbital_dir, init_wfc,
13961396
onsite_radius, fixed_atoms, noncolin,
1397-
calculation, esolver_type);
1397+
calculation, esolver_type, 0);
13981398
ofs_running.close();
13991399
ofs_warning.close();
14001400
ifa.close();
@@ -1435,7 +1435,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsC)
14351435
unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
14361436
basis_type, orbital_dir, init_wfc,
14371437
onsite_radius, fixed_atoms, noncolin,
1438-
calculation, esolver_type);
1438+
calculation, esolver_type, 0);
14391439
ofs_running.close();
14401440
ofs_warning.close();
14411441
ifa.close();
@@ -1476,7 +1476,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsCA)
14761476
unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
14771477
basis_type, orbital_dir, init_wfc,
14781478
onsite_radius, fixed_atoms, noncolin,
1479-
calculation, esolver_type);
1479+
calculation, esolver_type, 0);
14801480
ofs_running.close();
14811481
ofs_warning.close();
14821482
ifa.close();
@@ -1517,7 +1517,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsCACXY)
15171517
unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
15181518
basis_type, orbital_dir, init_wfc,
15191519
onsite_radius, fixed_atoms, noncolin,
1520-
calculation, esolver_type);
1520+
calculation, esolver_type, 0);
15211521
ofs_running.close();
15221522
ofs_warning.close();
15231523
ifa.close();
@@ -1558,7 +1558,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsCACXZ)
15581558
unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
15591559
basis_type, orbital_dir, init_wfc,
15601560
onsite_radius, fixed_atoms, noncolin,
1561-
calculation, esolver_type);
1561+
calculation, esolver_type, 0);
15621562
ofs_running.close();
15631563
ofs_warning.close();
15641564
ifa.close();
@@ -1599,7 +1599,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsCACYZ)
15991599
unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
16001600
basis_type, orbital_dir, init_wfc,
16011601
onsite_radius, fixed_atoms, noncolin,
1602-
calculation, esolver_type);
1602+
calculation, esolver_type, 0);
16031603
ofs_running.close();
16041604
ofs_warning.close();
16051605
ifa.close();
@@ -1640,7 +1640,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsCACXYZ)
16401640
unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
16411641
basis_type, orbital_dir, init_wfc,
16421642
onsite_radius, fixed_atoms, noncolin,
1643-
calculation, esolver_type);
1643+
calculation, esolver_type, 0);
16441644
ofs_running.close();
16451645
ofs_warning.close();
16461646
ifa.close();
@@ -1681,7 +1681,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsCAU)
16811681
unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
16821682
basis_type, orbital_dir, init_wfc,
16831683
onsite_radius, fixed_atoms, noncolin,
1684-
calculation, esolver_type);
1684+
calculation, esolver_type, 0);
16851685
ofs_running.close();
16861686
ofs_warning.close();
16871687
ifa.close();
@@ -1722,7 +1722,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsAutosetMag)
17221722
unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
17231723
basis_type, orbital_dir, init_wfc,
17241724
onsite_radius, fixed_atoms, noncolin,
1725-
calculation, esolver_type);
1725+
calculation, esolver_type, 0);
17261726
for (int it = 0; it < ucell->ntype; it++)
17271727
{
17281728
for (int ia = 0; ia < ucell->atoms[it].na; ia++)
@@ -1736,7 +1736,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsAutosetMag)
17361736
unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
17371737
basis_type, orbital_dir, init_wfc,
17381738
onsite_radius, fixed_atoms, noncolin,
1739-
calculation, esolver_type);
1739+
calculation, esolver_type, 0);
17401740
for (int it = 0; it < ucell->ntype; it++)
17411741
{
17421742
for (int ia = 0; ia < ucell->atoms[it].na; ia++)
@@ -1787,7 +1787,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsWarning1)
17871787
EXPECT_NO_THROW(unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
17881788
basis_type, orbital_dir, init_wfc,
17891789
onsite_radius, fixed_atoms, noncolin,
1790-
calculation, esolver_type));
1790+
calculation, esolver_type, 0));
17911791
ofs_running.close();
17921792
ofs_warning.close();
17931793
ifa.close();
@@ -1841,7 +1841,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsWarning2)
18411841
EXPECT_NO_THROW(unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
18421842
basis_type, orbital_dir, init_wfc,
18431843
onsite_radius, fixed_atoms, noncolin,
1844-
calculation, esolver_type));
1844+
calculation, esolver_type, 0));
18451845
ofs_running.close();
18461846
ofs_warning.close();
18471847
ifa.close();
@@ -1888,7 +1888,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsWarning3)
18881888
EXPECT_NO_THROW(unitcell::read_atom_positions(*ucell, ifa, ofs_running, GlobalV::ofs_warning, nspin,
18891889
basis_type, orbital_dir, init_wfc,
18901890
onsite_radius, fixed_atoms, noncolin,
1891-
calculation, esolver_type));
1891+
calculation, esolver_type, 0));
18921892
ofs_running.close();
18931893
GlobalV::ofs_warning.close();
18941894
ifa.close();
@@ -1937,7 +1937,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsWarning4)
19371937
EXPECT_EXIT(unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
19381938
basis_type, orbital_dir, init_wfc,
19391939
onsite_radius, fixed_atoms, noncolin,
1940-
calculation, esolver_type), ::testing::ExitedWithCode(1), "");
1940+
calculation, esolver_type, 0), ::testing::ExitedWithCode(1), "");
19411941
output = testing::internal::GetCapturedStdout();
19421942
EXPECT_THAT(output, testing::HasSubstr("read_atom_positions, mismatch in atom number for atom type: Mg"));
19431943
ofs_running.close();
@@ -1979,7 +1979,7 @@ TEST_F(UcellTestReadStru, ReadAtomPositionsWarning5)
19791979
EXPECT_NO_THROW(unitcell::read_atom_positions(*ucell, ifa, ofs_running, GlobalV::ofs_warning, nspin,
19801980
basis_type, orbital_dir, init_wfc,
19811981
onsite_radius, fixed_atoms, noncolin,
1982-
calculation, esolver_type));
1982+
calculation, esolver_type, 0));
19831983
ofs_running.close();
19841984
GlobalV::ofs_warning.close();
19851985
ifa.close();

source/source_cell/test/unitcell_test_setupcell.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ TEST_F(UcellTest,SetupCellS1)
8484

8585
ucell->setup_cell(fn, ofs_running, symmetry_prec, dfthalf_type, pseudo_dir, nspin,
8686
basis_type, orbital_dir, init_wfc, onsite_radius, deepks_setorb, rpa,
87-
fixed_atoms, noncolin, calculation, esolver_type);
87+
fixed_atoms, noncolin, calculation, esolver_type, 0);
8888
ofs_running.close();
8989
remove("setup_cell.tmp");
9090
}
@@ -98,7 +98,7 @@ TEST_F(UcellTest,SetupCellS2)
9898

9999
ucell->setup_cell(fn, ofs_running, symmetry_prec, dfthalf_type, pseudo_dir, nspin,
100100
basis_type, orbital_dir, init_wfc, onsite_radius, deepks_setorb, rpa,
101-
fixed_atoms, noncolin, calculation, esolver_type);
101+
fixed_atoms, noncolin, calculation, esolver_type, 0);
102102
ofs_running.close();
103103
remove("setup_cell.tmp");
104104
}
@@ -112,7 +112,7 @@ TEST_F(UcellTest,SetupCellS4)
112112

113113
ucell->setup_cell(fn, ofs_running, symmetry_prec, dfthalf_type, pseudo_dir, nspin,
114114
basis_type, orbital_dir, init_wfc, onsite_radius, deepks_setorb, rpa,
115-
fixed_atoms, noncolin, calculation, esolver_type);
115+
fixed_atoms, noncolin, calculation, esolver_type, 0);
116116
ofs_running.close();
117117
remove("setup_cell.tmp");
118118
}
@@ -127,7 +127,7 @@ TEST_F(UcellDeathTest,SetupCellWarning1)
127127
const int nspin = 1;
128128
EXPECT_EXIT(ucell->setup_cell(fn, ofs_running, symmetry_prec, dfthalf_type, pseudo_dir, nspin,
129129
basis_type, orbital_dir, init_wfc, onsite_radius, deepks_setorb, rpa,
130-
fixed_atoms, noncolin, calculation, esolver_type), ::testing::ExitedWithCode(1), "");
130+
fixed_atoms, noncolin, calculation, esolver_type, 0), ::testing::ExitedWithCode(1), "");
131131
output = testing::internal::GetCapturedStdout();
132132
EXPECT_THAT(output,testing::HasSubstr("Can not find the file containing atom positions.!"));
133133
ofs_running.close();
@@ -144,7 +144,7 @@ TEST_F(UcellDeathTest,SetupCellWarning2)
144144
const int nspin = 1;
145145
EXPECT_EXIT(ucell->setup_cell(fn, ofs_running, symmetry_prec, dfthalf_type, pseudo_dir, nspin,
146146
basis_type, orbital_dir, init_wfc, onsite_radius, deepks_setorb, rpa,
147-
fixed_atoms, noncolin, calculation, esolver_type), ::testing::ExitedWithCode(1), "");
147+
fixed_atoms, noncolin, calculation, esolver_type, 0), ::testing::ExitedWithCode(1), "");
148148
output = testing::internal::GetCapturedStdout();
149149
EXPECT_THAT(output,testing::HasSubstr("Something wrong during read_atom_positions"));
150150
ofs_running.close();
@@ -160,7 +160,7 @@ TEST_F(UcellTest,SetupCellAfterVC)
160160

161161
ucell->setup_cell(fn, ofs_running, symmetry_prec, dfthalf_type, pseudo_dir, nspin,
162162
basis_type, orbital_dir, init_wfc, onsite_radius, deepks_setorb, rpa,
163-
fixed_atoms, noncolin, calculation, esolver_type);
163+
fixed_atoms, noncolin, calculation, esolver_type, 0);
164164
ucell->lat0 = 1.0;
165165
ucell->latvec.Zero();
166166
ucell->latvec.e11 = 10.0;

source/source_cell/test_pw/unitcell_test_pw.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ if(GlobalV::MY_RANK==0)
116116
//call read_atom_positions
117117
EXPECT_NO_THROW(unitcell::read_atom_positions(*ucell, ifa, ofs_running, ofs_warning, nspin,
118118
basis_type, orbital_dir, init_wfc, onsite_radius, fixed_atoms, noncolin,
119-
calculation, esolver_type));
119+
calculation, esolver_type, 0));
120120
ofs_running.close();
121121
ofs_warning.close();
122122
ifa.close();
@@ -135,7 +135,7 @@ TEST_F(UcellTest,SetupCell)
135135
const int nspin = 1;
136136
ucell->setup_cell(fn, ofs_running, symmetry_prec, dfthalf_type, pseudo_dir, nspin,
137137
basis_type, orbital_dir, init_wfc, onsite_radius, deepks_setorb, rpa,
138-
fixed_atoms, noncolin, calculation, esolver_type);
138+
fixed_atoms, noncolin, calculation, esolver_type, 0);
139139
ofs_running.close();
140140
remove("setup_cell.tmp");
141141
}

source/source_cell/unitcell.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ std::vector<ModuleBase::Vector3<int>> UnitCell::get_constrain() const
182182
void UnitCell::setup_cell(const std::string& fn, std::ofstream& log, const double symmetry_prec, const int dfthalf_type, const std::string& pseudo_dir, const int nspin,
183183
const std::string& basis_type, const std::string& orbital_dir, const std::string& init_wfc,
184184
const double onsite_radius, const bool deepks_setorb, const bool rpa,
185-
const bool fixed_atoms, const bool noncolin, const std::string& calculation, const std::string& esolver_type)
185+
const bool fixed_atoms, const bool noncolin, const std::string& calculation, const std::string& esolver_type,
186+
const int symmetry)
186187
{
187188
ModuleBase::TITLE("UnitCell", "setup_cell");
188189

@@ -257,7 +258,7 @@ void UnitCell::setup_cell(const std::string& fn, std::ofstream& log, const doubl
257258
//==========================
258259
ok2 = unitcell::read_atom_positions(*this, ifa, log, GlobalV::ofs_warning, nspin,
259260
basis_type, orbital_dir, init_wfc, onsite_radius, fixed_atoms, noncolin,
260-
calculation, esolver_type);
261+
calculation, esolver_type, symmetry);
261262
}
262263
}
263264
#ifdef __MPI

source/source_cell/unitcell.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ class UnitCell : public AtomProvider {
241241
void setup_cell(const std::string& fn, std::ofstream& log, const double symmetry_prec, const int dfthalf_type, const std::string& pseudo_dir, const int nspin,
242242
const std::string& basis_type, const std::string& orbital_dir, const std::string& init_wfc,
243243
const double onsite_radius, const bool deepks_setorb, const bool rpa,
244-
const bool fixed_atoms, const bool noncolin, const std::string& calculation, const std::string& esolver_type);
244+
const bool fixed_atoms, const bool noncolin, const std::string& calculation, const std::string& esolver_type,
245+
const int symmetry);
245246

246247
/**
247248
* @brief Pointer to non-local pseudopotential information.

0 commit comments

Comments
 (0)