Skip to content

Commit

Permalink
Merge pull request #208 from timcallow/warnings_improve
Browse files Browse the repository at this point in the history
Add suppress warnings variable to config
  • Loading branch information
timcallow authored Oct 11, 2023
2 parents b98ae3a + ad0aea7 commit e430864
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 50 deletions.
37 changes: 25 additions & 12 deletions atoMEC/check_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,16 @@ def check_temp(self, temp, units_temp):
temp = unitconv.ev_to_ha * temp
elif units_temp.lower() == "k":
temp = unitconv.K_to_ha * temp
# check if temperature is within some reasonable limits
# check if temperature is within some reasonable limits (<1000 and > 0.1 eV)
if temp < 0:
raise InputError.temp_error("temperature is negative")
if temp < 0.01:
print(InputWarning.temp_warning("low"))
if temp < 0.0036:
if not config.suppress_warnings:
print(InputWarning.temp_warning("low"))
return temp
elif temp > 3.5:
print(InputWarning.temp_warning("high"))
elif temp > 36.7:
if not config.suppress_warnings:
print(InputWarning.temp_warning("high"))
return temp
else:
return temp
Expand Down Expand Up @@ -715,10 +717,12 @@ def check_grid_params(grid_params):
# check that ngrid is a positive number
if ngrid < 0:
raise InputError.grid_error("Number of grid points must be positive")
elif ngrid < 500:
print(InputWarning.ngrid_warning("low", "inaccurate"))
elif ngrid > 5000:
print(InputWarning.ngrid_warning("high", "expensive"))
elif ngrid < 300:
if not config.suppress_warnings:
print(InputWarning.ngrid_warning("low", "inaccurate"))
elif ngrid > 10000:
if not config.suppress_warnings:
print(InputWarning.ngrid_warning("high", "expensive"))

# check that ngrid_coarse is an integer
if not isinstance(ngrid_coarse, intc):
Expand All @@ -727,16 +731,25 @@ def check_grid_params(grid_params):
if ngrid_coarse < 0:
raise InputError.grid_error("Number of coarse grid points must be positive")
elif ngrid_coarse < 100:
print(InputWarning.ngrid_warning("low", "inaccurate"))
if not config.suppress_warnings:
print(InputWarning.ngrid_warning("low", "inaccurate"))
elif ngrid_coarse > 500:
print(InputWarning.ngrid_warning("high", "expensive"))
if not config.suppress_warnings:
print(InputWarning.ngrid_warning("high", "expensive"))

# check that x0 is reasonable
if x0 > -3:
raise InputError.grid_error(
"x0 is too high, calculation will likely not converge"
)

if s0 <= 1e-6:
raise InputError.grid_error("s0 is too small, numerical problems likely")
elif s0 >= 1e-2:
raise InputError.grid_error(
"s0 is too large, calculation will likely not converge"
)

grid_params = {"ngrid": ngrid, "x0": x0, "ngrid_coarse": ngrid_coarse, "s0": s0}

return grid_params
Expand Down Expand Up @@ -1189,7 +1202,7 @@ def ngrid_warning(err1, err2):
+ ". Proceeding anyway, but results may be "
+ err2
+ "\n"
+ "Suggested grid range is between 1000-5000 but should be tested wrt"
+ "Suggested grid range is between 500-10000 but should be tested wrt"
" convergence \n"
)
return warning
Expand Down
3 changes: 3 additions & 0 deletions atoMEC/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@

# parallelization
numcores = 0 # defaults to serial

# whether to suppress warnings (not errors)
suppress_warnings = False
4 changes: 2 additions & 2 deletions atoMEC/staticKS.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ def occnums_w(self):
norbs_ok, lorbs_ok = self.check_orbs(
self._occnums_w, config.conv_params["bandtol"]
)
if not norbs_ok:
if not norbs_ok and not config.suppress_warnings:
print(check_inputs.InputWarning.norbs_warning("nmax"))
if not lorbs_ok:
if not lorbs_ok and not config.suppress_warnings:
print(check_inputs.InputWarning.norbs_warning("lmax"))
return self._occnums_w

Expand Down
2 changes: 1 addition & 1 deletion tests/boundary_conditions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _run(bc):
4,
4,
scf_params={"maxscf": 5, "mixfrac": 0.3},
grid_params={"ngrid": 1000},
grid_params={"ngrid": 1000, "ngrid_coarse": 600},
band_params={"nkpts": nkpts},
)

Expand Down
5 changes: 4 additions & 1 deletion tests/conductivity_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ def _run_SCF():

# run the SCF calculation
output = model.CalcEnergy(
4, 4, scf_params={"mixfrac": 0.3, "maxscf": 6}, grid_params={"ngrid": 1200}
4,
4,
scf_params={"mixfrac": 0.3, "maxscf": 6},
grid_params={"ngrid": 1200, "ngrid_coarse": 300},
)

output_dict = {"Atom": F_at, "model": model, "SCF_out": output}
Expand Down
5 changes: 4 additions & 1 deletion tests/energy_alt_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ def _run(unbound):

# run the SCF calculation
output = model.CalcEnergy(
10, 5, scf_params={"maxscf": 6, "mixfrac": 0.3}, grid_params={"ngrid": 1000}
10,
5,
scf_params={"maxscf": 6, "mixfrac": 0.3},
grid_params={"ngrid": 1000, "ngrid_coarse": 300},
)

# construct the EnergyAlt object
Expand Down
40 changes: 21 additions & 19 deletions tests/exceptions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class TestAtom:
def test_element(self, ele_input):
"""Check chemical species input."""
with pytest.raises(SystemExit):
atom = Atom(ele_input, 0.05, radius=1)
atom = Atom(ele_input, 0.01, radius=1)
return atom

def test_temp_units(self):
"""Check temperature units input."""
with pytest.raises(SystemExit):
atom = Atom("H", 0.05, radius=1, units_temp="jk")
atom = Atom("H", 0.01, radius=1, units_temp="jk")
return atom

@pytest.mark.parametrize("temp_input", [("a"), (-0.2)])
Expand All @@ -37,45 +37,45 @@ def test_temp(self, temp_input):
def test_charge(self):
"""Check charge input."""
with pytest.raises(SystemExit):
atom = Atom("H", 0.05, radius=1.0, charge="jk")
atom = Atom("H", 0.01, radius=1.0, charge="jk")
return atom

def test_radius_units(self):
"""Check radius units input."""
with pytest.raises(SystemExit):
atom = Atom("H", 0.05, radius=1.0, units_radius="cm")
atom = Atom("H", 0.01, radius=1.0, units_radius="cm")
return atom

def test_density_units(self):
"""Check density units input."""
with pytest.raises(SystemExit):
atom = Atom("H", 0.05, density=0.1, units_density="ggcm3")
atom = Atom("H", 0.01, density=0.1, units_density="ggcm3")
return atom

@pytest.mark.parametrize("rad_input", [("a"), (-0.2)])
def test_radius(self, rad_input):
"""Check radius input."""
with pytest.raises(SystemExit):
atom = Atom("H", 0.05, radius=rad_input)
atom = Atom("H", 0.01, radius=rad_input)
return atom

@pytest.mark.parametrize("dens_input", [("a"), (-0.2)])
def test_density(self, dens_input):
"""Check density input."""
with pytest.raises(SystemExit):
atom = Atom("H", 0.05, density=dens_input)
atom = Atom("H", 0.01, density=dens_input)
return atom

def test_rad_dens_1(self):
"""Check radius and density compatibility."""
with pytest.raises(SystemExit):
atom = Atom("H", 0.05, radius=2.0, density=10.0)
atom = Atom("H", 0.01, radius=2.0, density=10.0)
return atom

def test_rad_dens_2(self):
"""Check one of radius or density specified."""
with pytest.raises(SystemExit):
atom = Atom("H", 0.05)
atom = Atom("H", 0.01)
return atom


Expand All @@ -88,7 +88,7 @@ class TestModel:
)
def test_xc(self, xc_input):
"""Test the exchange-correlation (xc) input."""
atom = Atom("Al", 0.05, radius=1)
atom = Atom("Al", 0.01, radius=1)

with pytest.raises((SystemExit, TypeError)):
model = models.ISModel(atom, xfunc_id=xc_input)
Expand All @@ -104,7 +104,7 @@ def test_xc(self, xc_input):
)
def test_unbound(self, unbound_input):
"""Test unbound input."""
atom = Atom("Al", 0.05, radius=1)
atom = Atom("Al", 0.01, radius=1)

with pytest.raises(SystemExit):
model = models.ISModel(atom, unbound=unbound_input, bc="bands")
Expand All @@ -119,15 +119,15 @@ def test_unbound(self, unbound_input):
)
def test_bcs(self, bcs_input):
"""Test boundary conditions input."""
atom = Atom("Al", 0.05, radius=1)
atom = Atom("Al", 0.01, radius=1)

with pytest.raises(SystemExit):
model = models.ISModel(atom, bc=bcs_input)
return model

def test_spinpol(self):
"""Test spin polarization input."""
atom = Atom("Al", 0.05, radius=1)
atom = Atom("Al", 0.01, radius=1)

with pytest.raises(SystemExit):
model = models.ISModel(atom, spinpol="a")
Expand All @@ -139,15 +139,15 @@ def test_spinpol(self):
)
def test_spinmag(self, spinmag_input):
"""Test spin magnetization input."""
atom = Atom(spinmag_input[1], 0.05, radius=1)
atom = Atom(spinmag_input[1], 0.01, radius=1)

with pytest.raises(SystemExit):
model = models.ISModel(atom, spinmag=spinmag_input[0])
return model

def test_v_shift(self):
"""Test v shift input."""
atom = Atom("Al", 0.05, radius=1)
atom = Atom("Al", 0.01, radius=1)

with pytest.raises(SystemExit):
model = models.ISModel(atom, v_shift="a")
Expand All @@ -165,11 +165,13 @@ class TestCalcEnergy:
({"ngrid_coarse": "a"}),
({"ngrid_coarse": -100}),
({"x0": -2}),
({"s0": 1e-7}),
({"s0": 1e-1}),
],
)
def test_grid_params(self, grid_input):
"""Test the grid_params input."""
atom = Atom("Al", 0.05, radius=1)
atom = Atom("Al", 0.01, radius=1)
model = models.ISModel(atom)

with pytest.raises(SystemExit):
Expand All @@ -184,7 +186,7 @@ def test_grid_params(self, grid_input):
)
def test_conv_params(self, conv_input):
"""Test the conv_params input."""
atom = Atom("Al", 0.05, radius=1)
atom = Atom("Al", 0.01, radius=1)
model = models.ISModel(atom)

with pytest.raises(SystemExit):
Expand All @@ -201,7 +203,7 @@ def test_conv_params(self, conv_input):
)
def test_scf_params(self, scf_input):
"""Test the scf_params input."""
atom = Atom("Al", 0.05, radius=1)
atom = Atom("Al", 0.01, radius=1)
model = models.ISModel(atom)

with pytest.raises(SystemExit):
Expand All @@ -218,7 +220,7 @@ def test_scf_params(self, scf_input):
)
def test_band_params(self, bands_input):
"""Test the band params input."""
atom = Atom("Al", 0.05, radius=1)
atom = Atom("Al", 0.01, radius=1)
model = models.ISModel(atom, bc="bands", unbound="quantum")

with pytest.raises(SystemExit):
Expand Down
2 changes: 1 addition & 1 deletion tests/functionals_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _run(func):
6,
6,
scf_params={"maxscf": 5, "mixfrac": 0.3},
grid_params={"ngrid": 1000},
grid_params={"ngrid": 1000, "ngrid_coarse": 90},
band_params={"nkpts": 30},
)

Expand Down
5 changes: 4 additions & 1 deletion tests/gramschmidt_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ def _run_SCF():

# run SCF calculation
output = model.CalcEnergy(
4, 4, scf_params={"mixfrac": 0.3, "maxscf": 5}, grid_params={"ngrid": 1000}
4,
4,
scf_params={"mixfrac": 0.3, "maxscf": 5},
grid_params={"ngrid": 1000, "ngrid_coarse": 300},
)

return output
Expand Down
2 changes: 1 addition & 1 deletion tests/localization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def _run_SCF(spinpol):
3,
2,
scf_params={"mixfrac": 0.3, "maxscf": 50},
grid_params={"ngrid": 1000},
grid_params={"ngrid": 1000, "ngrid_coarse": 300},
)

output_dict = {"Atom": Al_at, "model": model, "SCF_out": output}
Expand Down
3 changes: 2 additions & 1 deletion tests/pressure_log_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class TestPressure:
def SCF_output(self):
"""Run a spin-unpolarized SCF calc and save the output."""
config.numcores = -1
config.suppress_warnings = True
return self._run_SCF()

@pytest.mark.parametrize(
Expand Down Expand Up @@ -111,7 +112,7 @@ def _run_SCF():
3,
3,
scf_params={"maxscf": 5, "mixfrac": 0.3},
grid_params={"ngrid": 1000},
grid_params={"ngrid": 1000, "ngrid_coarse": 300},
band_params={"nkpts": 50},
verbosity=1,
grid_type="log",
Expand Down
2 changes: 1 addition & 1 deletion tests/pressure_sqrt_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _run_SCF():
6,
8,
scf_params={"maxscf": 5, "mixfrac": 0.3},
grid_params={"ngrid": 1000},
grid_params={"ngrid": 1000, "ngrid_coarse": 300},
band_params={"nkpts": 50},
verbosity=1,
grid_type="sqrt",
Expand Down
14 changes: 7 additions & 7 deletions tests/serial_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@


# expected values and tolerance
dense_expected = -0.5620194349606303
coarse_expected = -0.5625664849484403
dense_expected = -0.5620111902408967
coarse_expected = -0.56341460946266
accuracy = 1e-3


Expand All @@ -23,8 +23,8 @@ class TestSerial:
@pytest.mark.parametrize(
"test_input,expected",
[
(400, coarse_expected),
(5001, dense_expected),
(250, coarse_expected),
(10001, dense_expected),
],
)
def test_serial(self, test_input, expected):
Expand Down Expand Up @@ -59,7 +59,7 @@ def _run(ngrid):
2,
scf_params={"maxscf": 1, "mixfrac": 0.7},
band_params={"nkpts": 30},
grid_params={"ngrid": ngrid},
grid_params={"ngrid": ngrid, "ngrid_coarse": 300},
)

# extract the total free energy
Expand All @@ -69,7 +69,7 @@ def _run(ngrid):

if __name__ == "__main__":
config.numcores = 0
dense = TestSerial._run(5001)
coarse = TestSerial._run(400)
dense = TestSerial._run(10001)
coarse = TestSerial._run(250)
print("dense_expected =", dense)
print("coarse_expected =", coarse)
2 changes: 1 addition & 1 deletion tests/spin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _run(spinmag):
5,
scf_params={"maxscf": 4, "mixfrac": 0.3},
band_params={"nkpts": 50},
grid_params={"ngrid": 1000},
grid_params={"ngrid": 1000, "ngrid_coarse": 300},
)

# extract the total free energy
Expand Down
Loading

0 comments on commit e430864

Please sign in to comment.