Skip to content

Commit b95f433

Browse files
authored
Fix(input): validate contradictory final parameters (#7731)
* Fix(input): validate contradictory final parameters * Fix(input): tighten stochastic band validation * Fix(input): validate complete parallel configuration * Fix(input): relax stochastic band limit
1 parent 7ef087d commit b95f433

58 files changed

Lines changed: 257 additions & 77 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/advanced/input_files/input-main.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@
653653
### bndpar
654654

655655
- **Type**: Integer
656-
- **Description**: Divide all processors into bndpar groups, and bands (only stochastic orbitals now) will be distributed among each group. It should be larger than 0.
656+
- **Description**: Divide all processors into bndpar groups for SDFT or the BPCG solver. bndpar must be positive, no greater than the number of MPI processes, and kpar * bndpar must divide the number of MPI processes exactly.
657657
- **Default**: 1
658658

659659
### latname
@@ -1265,7 +1265,7 @@
12651265
- **Description**: The number of spin components of wave functions.
12661266
- 1: Spin degeneracy
12671267
- 2: Collinear spin polarized.
1268-
- 4: For the case of noncollinear polarized, nspin will be automatically set to 4 without being specified by the user.
1268+
- 4: Noncollinear or spin-orbit calculations. Set nspin to 4 explicitly when noncolin or lspinorb is enabled.
12691269
- **Default**: 1
12701270

12711271
### smearing_method
@@ -1469,7 +1469,7 @@
14691469
- **Type**: Boolean
14701470
- **Description**: Whether to consider spin-orbit coupling (SOC) effect in the calculation.
14711471
- True: Consider spin-orbit coupling effect. When enabled:
1472-
- nspin is automatically set to 4 (noncollinear spin representation)
1472+
- nspin must be explicitly set to 4 (noncollinear spin representation)
14731473
- Symmetry is automatically disabled (SOC breaks inversion symmetry)
14741474
- Requires full-relativistic pseudopotentials with has_so=true in the UPF header
14751475
- False: Do not consider spin-orbit coupling effect.
@@ -1481,7 +1481,7 @@
14811481
- **Type**: Boolean
14821482
- **Description**: Whether to allow non-collinear magnetic moments, where magnetization can point in arbitrary directions (x, y, z components) rather than being constrained to the z-axis.
14831483
- True: Allow non-collinear polarization. When enabled:
1484-
- nspin is automatically set to 4
1484+
- nspin must be explicitly set to 4
14851485
- Wave function dimension is doubled (npol=2), and the number of occupied states is doubled
14861486
- Charge density has 4 components (Pauli spin matrices)
14871487
- Cannot be used with gamma_only=true
@@ -1535,8 +1535,8 @@
15351535
- **Type**: Integer or string
15361536
- **Availability**: *esolver_type = sdft*
15371537
- **Description**: The number of stochastic orbitals
1538-
- > 0: Perform stochastic DFT. Increasing the number of bands improves accuracy and reduces stochastic errors; To perform mixed stochastic-deterministic DFT, you should set nbands, which represents the number of KS orbitals.
1539-
- 0: Perform Kohn-Sham DFT.
1538+
- 1-1000000: Perform stochastic DFT. Increasing the number of bands improves accuracy and reduces stochastic errors; To perform mixed stochastic-deterministic DFT, you should set nbands, which represents the number of KS orbitals.
1539+
- 0: Invalid. Use all for the complete-basis SDFT mode.
15401540
- all: All complete basis sets are used to replace stochastic orbitals with the Chebyshev method (CT), resulting in the same results as KSDFT without stochastic errors.
15411541
- **Default**: 256
15421542

docs/advanced/scf/spin.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ If **"nupdown"** is set to non-zero, number of spin-up and spin-down electrons w
3030

3131
## Noncollinear Spin Polarized Calculations
3232
The spin non-collinear polarization calculation corresponds to setting **"noncolin 1"**, in which case the coupling between spin up and spin down will be taken into account.
33-
In this case, nspin is automatically set to 4, which is usually not required to be specified manually.
33+
In this case, **"nspin 4"** must also be specified. ABACUS reports an input error instead of silently changing an incompatible or omitted nspin value.
3434
The weight of each band will not change, but the number of occupied states will be double.
3535
If the nbands parameter is set manually, it is generally set to twice what it would be when nspin<4.
3636

37-
In general, non-collinear magnetic moment settings are often used in calculations considering [SOC effects](#soc-effects). When **"lspinorb 1"** in INPUT file, "nspin" is also automatically set to 4.
37+
In general, non-collinear magnetic moment settings are often used in calculations considering [SOC effects](#soc-effects). When **"lspinorb 1"** is set in INPUT, **"nspin 4"** is also required.
3838

3939
Note: different settings for "noncolin" and "lspinorb" correspond to different calculations:
4040

@@ -119,22 +119,22 @@ Example from a full-relativistic UPF file:
119119
- **PseudoDOJO**: Provides both scalar and full-relativistic versions
120120
- **ABACUS official**: [abacus.ustc.edu.cn](http://abacus.ustc.edu.cn/pseudo/list.htm)
121121

122-
## Automatic Parameter Settings
122+
## Parameter Requirements and Automatic Settings
123123

124-
When using SOC or non-collinear calculations, ABACUS automatically adjusts several parameters:
124+
When using SOC or non-collinear calculations, set the required spin representation explicitly. ABACUS still derives internal spin state and some related settings after validating the input:
125125

126126
### When `lspinorb=true`:
127-
1. **nspin**: Automatically set to 4 (noncollinear spin representation)
127+
1. **nspin**: Must be explicitly set to 4 (noncollinear spin representation)
128128
2. **Symmetry**: Automatically disabled (`symm_flag=-1`) because SOC breaks inversion symmetry
129129
3. **Magnetization**: NOT automatically set when `noncolin=0` (implies non-magnetic material with SOC)
130130

131131
### When `noncolin=true`:
132-
1. **nspin**: Automatically set to 4
132+
1. **nspin**: Must be explicitly set to 4
133133
2. **npol**: Set to 2 (wave function has two spinor components)
134134
3. **Magnetization**: Automatically set if user provides zero values (unless `lspinorb=1` and `noncolin=0`)
135135

136136
### Important Notes:
137-
- You do NOT need to manually set `nspin=4` when using `lspinorb=1` or `noncolin=1`
137+
- You must set `nspin=4` when using `lspinorb=1` or `noncolin=1`; missing or incompatible values are rejected during input validation
138138
- Symmetry operations are incompatible with SOC, so they are automatically turned off
139139
- For `lspinorb=1, noncolin=0`: This is a special case for non-magnetic materials with SOC, where magnetization is not initialized
140140

@@ -172,7 +172,7 @@ basis_type pw
172172
ecutwfc 50
173173
lspinorb 1 # Enable SOC
174174
noncolin 0 # No non-collinear magnetism
175-
# nspin will be automatically set to 4
175+
nspin 4 # Required spinor representation
176176
# symmetry will be automatically disabled
177177
```
178178

@@ -185,7 +185,7 @@ calculation scf
185185
basis_type lcao
186186
lspinorb 0 # No SOC
187187
noncolin 1 # Enable non-collinear magnetism
188-
# nspin will be automatically set to 4
188+
nspin 4 # Required spinor representation
189189
# Magnetization directions should be specified in STRU file
190190
```
191191

@@ -199,7 +199,7 @@ basis_type pw
199199
ecutwfc 60
200200
lspinorb 1 # Enable SOC
201201
noncolin 1 # Enable non-collinear magnetism
202-
# nspin will be automatically set to 4
202+
nspin 4 # Required spinor representation
203203
# symmetry will be automatically disabled
204204
# Magnetization directions should be specified in STRU file
205205
```
@@ -213,6 +213,7 @@ calculation scf
213213
basis_type pw
214214
ecutwfc 50
215215
lspinorb 1 # Enable SOC
216+
nspin 4 # Required spinor representation
216217
soc_lambda 0.5 # 50% SOC strength
217218
# Useful when full SOC overestimates or underestimates experimental results
218219
```

docs/community/faq.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ To perform SOC calculations in ABACUS, follow these steps:
5050
2. **Use full-relativistic pseudopotentials**: SOC calculations require pseudopotentials with `has_so=true` in the UPF header
5151
- Download full-relativistic versions of SG15_ONCV pseudopotentials from [quantum-simulation.org](http://quantum-simulation.org/potentials/sg15_oncv/upf/)
5252
- Check the UPF file header for `relativistic="full"` and `has_so="T"`
53-
3. **Verify automatic settings**: When `lspinorb=1` is set, `nspin` is automatically set to 4 and symmetry is automatically disabled
53+
3. **Set the spin representation**: When `lspinorb=1` is set, explicitly set `nspin=4`; symmetry is automatically disabled
5454

5555
**Basis set support**: Both `basis_type=pw` (plane wave) and `basis_type=lcao` (numerical atomic orbitals) support SOC calculations for both SCF and NSCF.
5656

docs/parameters.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ parameters:
108108
category: System variables
109109
type: Integer
110110
description: |
111-
Divide all processors into bndpar groups, and bands (only stochastic orbitals now) will be distributed among each group. It should be larger than 0.
111+
Divide all processors into bndpar groups for SDFT or the BPCG solver. bndpar must be positive, no greater than the number of MPI processes, and kpar * bndpar must divide the number of MPI processes exactly.
112112
default_value: "1"
113113
unit: ""
114114
availability: ""
@@ -661,7 +661,7 @@ parameters:
661661
The number of spin components of wave functions.
662662
* 1: Spin degeneracy
663663
* 2: Collinear spin polarized.
664-
* 4: For the case of noncollinear polarized, nspin will be automatically set to 4 without being specified by the user.
664+
* 4: Noncollinear or spin-orbit calculations. Set nspin to 4 explicitly when noncolin or lspinorb is enabled.
665665
default_value: "1"
666666
unit: ""
667667
availability: ""
@@ -906,7 +906,7 @@ parameters:
906906
description: |
907907
Whether to consider spin-orbit coupling (SOC) effect in the calculation.
908908
* True: Consider spin-orbit coupling effect. When enabled:
909-
* nspin is automatically set to 4 (noncollinear spin representation)
909+
* nspin must be explicitly set to 4 (noncollinear spin representation)
910910
* Symmetry is automatically disabled (SOC breaks inversion symmetry)
911911
* Requires full-relativistic pseudopotentials with has_so=true in the UPF header
912912
* False: Do not consider spin-orbit coupling effect.
@@ -920,7 +920,7 @@ parameters:
920920
description: |
921921
Whether to allow non-collinear magnetic moments, where magnetization can point in arbitrary directions (x, y, z components) rather than being constrained to the z-axis.
922922
* True: Allow non-collinear polarization. When enabled:
923-
* nspin is automatically set to 4
923+
* nspin must be explicitly set to 4
924924
* Wave function dimension is doubled (npol=2), and the number of occupied states is doubled
925925
* Charge density has 4 components (Pauli spin matrices)
926926
* Cannot be used with gamma_only=true
@@ -2198,8 +2198,8 @@ parameters:
21982198
type: Integer or string
21992199
description: |
22002200
The number of stochastic orbitals
2201-
* > 0: Perform stochastic DFT. Increasing the number of bands improves accuracy and reduces stochastic errors; To perform mixed stochastic-deterministic DFT, you should set nbands, which represents the number of KS orbitals.
2202-
* 0: Perform Kohn-Sham DFT.
2201+
* 1-1000000: Perform stochastic DFT. Increasing the number of bands improves accuracy and reduces stochastic errors; To perform mixed stochastic-deterministic DFT, you should set nbands, which represents the number of KS orbitals.
2202+
* 0: Invalid. Use all for the complete-basis SDFT mode.
22032203
* all: All complete basis sets are used to replace stochastic orbitals with the Chebyshev method (CT), resulting in the same results as KSDFT without stochastic errors.
22042204
default_value: "256"
22052205
unit: ""

examples/21_deepks/03_lcao_CsPbI3/INPUT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ deepks_model model.ptg
2929

3030
#Parameters (7.SOC)
3131
lspinorb 1
32+
nspin 4
3233

3334

3435

source/source_io/module_parameter/read_input_item_elec_stru.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -482,22 +482,20 @@ The other way is only available when compiling with LIBXC, and it allows for sup
482482
item.description = R"(The number of spin components of wave functions.
483483
* 1: Spin degeneracy
484484
* 2: Collinear spin polarized.
485-
* 4: For the case of noncollinear polarized, nspin will be automatically set to 4 without being specified by the user.)";
485+
* 4: Noncollinear or spin-orbit calculations. Set nspin to 4 explicitly when noncolin or lspinorb is enabled.)";
486486
item.default_value = "1";
487487
item.unit = "";
488488
item.availability = "";
489489
read_sync_int(input.nspin);
490-
item.reset_value = [](const Input_Item& item, Parameter& para) {
491-
if (para.input.noncolin || para.input.lspinorb)
492-
{
493-
para.input.nspin = 4;
494-
}
495-
};
496490
item.check_value = [](const Input_Item& item, const Parameter& para) {
497491
if (para.input.nspin != 1 && para.input.nspin != 2 && para.input.nspin != 4)
498492
{
499493
ModuleBase::WARNING_QUIT("ReadInput", "nspin should be 1, 2 or 4.");
500494
}
495+
if ((para.input.noncolin || para.input.lspinorb) && para.input.nspin != 4)
496+
{
497+
ModuleBase::WARNING_QUIT("ReadInput", "nspin must be 4 when noncolin or lspinorb is enabled.");
498+
}
501499
};
502500
this->add_item(item);
503501
}
@@ -986,7 +984,7 @@ Note: If gamma_only is set to 1, the KPT file will be overwritten. So make sure
986984
item.type = "Boolean";
987985
item.description = R"(Whether to consider spin-orbit coupling (SOC) effect in the calculation.
988986
* True: Consider spin-orbit coupling effect. When enabled:
989-
* nspin is automatically set to 4 (noncollinear spin representation)
987+
* nspin must be explicitly set to 4 (noncollinear spin representation)
990988
* Symmetry is automatically disabled (SOC breaks inversion symmetry)
991989
* Requires full-relativistic pseudopotentials with has_so=true in the UPF header
992990
* False: Do not consider spin-orbit coupling effect.
@@ -1004,7 +1002,7 @@ Note: If gamma_only is set to 1, the KPT file will be overwritten. So make sure
10041002
item.type = "Boolean";
10051003
item.description = R"(Whether to allow non-collinear magnetic moments, where magnetization can point in arbitrary directions (x, y, z components) rather than being constrained to the z-axis.
10061004
* True: Allow non-collinear polarization. When enabled:
1007-
* nspin is automatically set to 4
1005+
* nspin must be explicitly set to 4
10081006
* Wave function dimension is doubled (npol=2), and the number of occupied states is doubled
10091007
* Charge density has 4 components (Pauli spin matrices)
10101008
* Cannot be used with gamma_only=true

source/source_io/module_parameter/read_input_item_sdft.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
#include "source_base/global_function.h"
21
#include "source_base/tool_quit.h"
32
#include "read_input.h"
43
#include "read_input_tool.h"
54

5+
#include <exception>
6+
67
namespace ModuleIO
78
{
89
void ReadInput::item_sdft()
@@ -37,8 +38,8 @@ void ReadInput::item_sdft()
3738
item.category = "Electronic structure (SDFT)";
3839
item.type = "Integer or string";
3940
item.description = R"(The number of stochastic orbitals
40-
* > 0: Perform stochastic DFT. Increasing the number of bands improves accuracy and reduces stochastic errors; To perform mixed stochastic-deterministic DFT, you should set nbands, which represents the number of KS orbitals.
41-
* 0: Perform Kohn-Sham DFT.
41+
* 1-1000000: Perform stochastic DFT. Increasing the number of bands improves accuracy and reduces stochastic errors; To perform mixed stochastic-deterministic DFT, you should set nbands, which represents the number of KS orbitals.
42+
* 0: Invalid. Use all for the complete-basis SDFT mode.
4243
* all: All complete basis sets are used to replace stochastic orbitals with the Chebyshev method (CT), resulting in the same results as KSDFT without stochastic errors.)";
4344
item.default_value = "256";
4445
item.unit = "";
@@ -47,28 +48,32 @@ void ReadInput::item_sdft()
4748
std::string nbandsto_str = strvalue;
4849
if (nbandsto_str != "all")
4950
{
50-
para.input.nbands_sto = std::stoi(nbandsto_str);
51+
std::size_t parsed_chars = 0;
52+
try
53+
{
54+
para.input.nbands_sto = std::stoi(nbandsto_str, &parsed_chars);
55+
}
56+
catch (const std::exception&)
57+
{
58+
ModuleBase::WARNING_QUIT("ReadInput",
59+
"nbands_sto should be in the range of 1 to 1000000 or be all");
60+
}
61+
if (parsed_chars != nbandsto_str.size())
62+
{
63+
ModuleBase::WARNING_QUIT("ReadInput",
64+
"nbands_sto should be in the range of 1 to 1000000 or be all");
65+
}
5166
}
5267
else
5368
{
5469
para.input.nbands_sto = 0;
5570
}
5671
};
57-
item.reset_value = [](const Input_Item& item, Parameter& para) {
58-
// only do it when nbands_sto is set in INPUT
59-
if (item.is_read())
60-
{
61-
if (strvalue == "0" && para.input.esolver_type == "sdft")
62-
{
63-
para.input.esolver_type = "ksdft";
64-
ModuleBase::GlobalFunc::AUTO_SET("esolver_type", para.input.esolver_type);
65-
}
66-
}
67-
};
6872
item.check_value = [](const Input_Item& item, const Parameter& para) {
69-
if (para.input.nbands_sto < 0 || para.input.nbands_sto > 100000)
73+
const bool use_complete_basis = item.is_read() && strvalue == "all";
74+
if ((!use_complete_basis && para.input.nbands_sto < 1) || para.input.nbands_sto > 1000000)
7075
{
71-
ModuleBase::WARNING_QUIT("ReadInput", "nbands_sto should be in the range of 0 to 100000");
76+
ModuleBase::WARNING_QUIT("ReadInput", "nbands_sto should be in the range of 1 to 1000000 or be all");
7277
}
7378
};
7479
item.get_final_value = [](Input_Item& item, const Parameter& para) {

source/source_io/module_parameter/read_input_item_system.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ void ReadInput::item_system()
313313
// GPU + PW: validate kpar against total processors
314314
// Moved from base_device::information::get_device_kpar()
315315
#if defined(__CUDA) || defined(__ROCM)
316-
if (para.input.device == "gpu" && para.input.basis_type == "pw")
316+
if (para.input.device == "gpu" && para.input.basis_type == "pw" && para.input.bndpar > 0)
317317
{
318318
if (GlobalV::NPROC != para.input.kpar * para.input.bndpar)
319319
{
@@ -339,25 +339,34 @@ void ReadInput::item_system()
339339
"will be distributed among each group";
340340
item.category = "System variables";
341341
item.type = "Integer";
342-
item.description = "Divide all processors into bndpar groups, and bands (only stochastic orbitals now) "
343-
"will be distributed among each group. It should be larger than 0.";
342+
item.description = "Divide all processors into bndpar groups for SDFT or the BPCG solver. bndpar must be "
343+
"positive, no greater than the number of MPI processes, and kpar * bndpar must divide "
344+
"the number of MPI processes exactly.";
344345
item.default_value = "1";
345346
read_sync_int(input.bndpar);
346-
item.reset_value = [](const Input_Item& item, Parameter& para) {
347-
if (para.input.esolver_type != "sdft" && para.input.ks_solver != "bpcg")
347+
item.check_value = [](const Input_Item& item, const Parameter& para) {
348+
if (para.input.bndpar <= 0)
348349
{
349-
para.input.bndpar = 1;
350+
ModuleBase::WARNING_QUIT("ReadInput", "bndpar must be greater than 0");
350351
}
351352
if (para.input.bndpar > GlobalV::NPROC)
352353
{
353-
para.input.bndpar = GlobalV::NPROC;
354+
ModuleBase::WARNING_QUIT("ReadInput", "bndpar can not exceed the number of MPI processes");
355+
}
356+
if (para.input.bndpar > 1 && para.input.esolver_type != "sdft" && para.input.ks_solver != "bpcg")
357+
{
358+
ModuleBase::WARNING_QUIT("ReadInput", "bndpar > 1 requires esolver_type=sdft or ks_solver=bpcg");
354359
}
355-
};
356-
item.check_value = [](const Input_Item& item, const Parameter& para) {
357360
if (GlobalV::NPROC % para.input.bndpar != 0)
358361
{
359362
ModuleBase::WARNING_QUIT("ReadInput", "The number of processors can not be divided by bndpar");
360363
}
364+
if (para.input.bndpar > 1
365+
&& (para.input.kpar <= 0 || (GlobalV::NPROC / para.input.bndpar) % para.input.kpar != 0))
366+
{
367+
ModuleBase::WARNING_QUIT("ReadInput",
368+
"The number of processors can not be divided by kpar * bndpar");
369+
}
361370
};
362371
this->add_item(item);
363372
}

0 commit comments

Comments
 (0)