2626@pytest .fixture (scope = 'session' )
2727def command_line ():
2828 # One random prefix to use per test that "tests" the command line args
29- prefix = ('d17weqroegn ' , 'riabfodkj ' )
29+ prefix = ('d17weqroeg ' , 'riabfodkj5' , 'fir8o3lsak' , 'zwejklqn25 ' )
3030
3131 petsc_option = (
3232 ('ksp_rtol' ,),
33- ('ksp_rtol' , 'ksp_atol' )
33+ ('ksp_rtol' , 'ksp_atol' ),
34+ ('ksp_rtol' , 'ksp_atol' , 'ksp_divtol' , 'ksp_max_it' ),
35+ ('ksp_type' ,)
36+
3437 )
3538 value = (
3639 (1e-8 ,),
3740 (1e-11 , 1e-15 ),
41+ (1e-3 , 1e-10 , 50000 , 2000 ),
42+ ('cg' ,)
3843 )
3944 argv = []
4045 expected = {}
@@ -1728,20 +1733,39 @@ def test_multiple_operators(self, log_level):
17281733 assert entry2 .KSPGetTolerances ['rtol' ] == 1e-12
17291734
17301735 @skipif ('petsc' )
1731- def test_command_line_priority_1 (self , command_line ):
1736+ @pytest .mark .parametrize ('log_level' , ['PERF' , 'DEBUG' ])
1737+ def test_command_line_priority_tols_1 (self , command_line , log_level ):
17321738 """
1733- Test solver parameters specifed via the command line
1734- take precedence over those set in the solver_parameters
1735- dictionary.
1739+ Test solver tolerances specifed via the command line
1740+ take precedence over those set by the defaults.
17361741 """
1737- prefix = 'd17weqroegn'
1742+ prefix = 'd17weqroeg'
1743+ _ , expected = command_line
1744+
1745+ solver1 = PETScSolve (
1746+ self .eq1 , target = self .e ,
1747+ options_prefix = prefix
1748+ )
1749+ with switchconfig (language = 'petsc' , log_level = log_level ):
1750+ op = Operator (solver1 )
1751+ summary = op .apply ()
1752+
1753+ petsc_summary = summary .petsc
1754+ entry = petsc_summary .get_entry ('section0' , prefix )
1755+ for opt , val in expected [prefix ]:
1756+ assert entry .KSPGetTolerances [opt .removeprefix ('ksp_' )] == val
1757+
1758+ @skipif ('petsc' )
1759+ @pytest .mark .parametrize ('log_level' , ['PERF' , 'DEBUG' ])
1760+ def test_command_line_priority_tols_2 (self , command_line , log_level ):
1761+ prefix = 'riabfodkj5'
17381762 _ , expected = command_line
17391763
17401764 solver1 = PETScSolve (
17411765 self .eq1 , target = self .e ,
17421766 options_prefix = prefix
17431767 )
1744- with switchconfig (language = 'petsc' , log_level = 'DEBUG' ):
1768+ with switchconfig (language = 'petsc' , log_level = log_level ):
17451769 op = Operator (solver1 )
17461770 summary = op .apply ()
17471771
@@ -1751,17 +1775,31 @@ def test_command_line_priority_1(self, command_line):
17511775 assert entry .KSPGetTolerances [opt .removeprefix ('ksp_' )] == val
17521776
17531777 @skipif ('petsc' )
1754- def test_command_line_priority_2 (self , command_line ):
1778+ @pytest .mark .parametrize ('log_level' , ['PERF' , 'DEBUG' ])
1779+ def test_command_line_priority_tols3 (self , command_line , log_level ):
17551780 """
1781+ Test solver tolerances specifed via the command line
1782+ take precedence over those specified by the `solver_parameters` dict.
17561783 """
1757- prefix = 'riabfodkj '
1784+ prefix = 'fir8o3lsak '
17581785 _ , expected = command_line
17591786
1787+ # Set solver parameters that differ both from the defaults and from the
1788+ # values provided on the command line for this prefix (see the `command_line`
1789+ # fixture).
1790+ params = {
1791+ 'ksp_rtol' : 1e-13 ,
1792+ 'ksp_atol' : 1e-35 ,
1793+ 'ksp_divtol' : 300000 ,
1794+ 'ksp_max_it' : 500
1795+ }
1796+
17601797 solver1 = PETScSolve (
17611798 self .eq1 , target = self .e ,
1799+ solver_parameters = params ,
17621800 options_prefix = prefix
17631801 )
1764- with switchconfig (language = 'petsc' , log_level = 'DEBUG' ):
1802+ with switchconfig (language = 'petsc' , log_level = log_level ):
17651803 op = Operator (solver1 )
17661804 summary = op .apply ()
17671805
@@ -1770,6 +1808,36 @@ def test_command_line_priority_2(self, command_line):
17701808 for opt , val in expected [prefix ]:
17711809 assert entry .KSPGetTolerances [opt .removeprefix ('ksp_' )] == val
17721810
1811+ @skipif ('petsc' )
1812+ @pytest .mark .parametrize ('log_level' , ['PERF' , 'DEBUG' ])
1813+ def test_command_line_priority_ksp_type (self , command_line , log_level ):
1814+ """
1815+ Test the solver parameter 'ksp_type' specified via the command line
1816+ take precedence over the one specified in the `solver_parameters` dict.
1817+ """
1818+ prefix = 'zwejklqn25'
1819+ _ , expected = command_line
1820+
1821+ # Set `ksp_type`` in the solver parameters, which should be overridden
1822+ # by the command line value (which is set to `cg` -
1823+ # see the `command_line` fixture).
1824+ params = {'ksp_type' : 'richardson' }
1825+
1826+ solver1 = PETScSolve (
1827+ self .eq1 , target = self .e ,
1828+ solver_parameters = params ,
1829+ options_prefix = prefix
1830+ )
1831+ with switchconfig (language = 'petsc' , log_level = log_level ):
1832+ op = Operator (solver1 )
1833+ summary = op .apply ()
1834+
1835+ petsc_summary = summary .petsc
1836+ entry = petsc_summary .get_entry ('section0' , prefix )
1837+ for _ , val in expected [prefix ]:
1838+ assert entry .KSPGetType == val
1839+ assert not entry .KSPGetType == params ['ksp_type' ]
1840+
17731841
17741842class TestHashing :
17751843
0 commit comments