|
25 | 25 |
|
26 | 26 | @pytest.fixture(scope='session') |
27 | 27 | def command_line(): |
28 | | - # One random prefix to use per test that "tests" the command line args |
29 | | - prefix = ('d17weqroeg', 'riabfodkj5', 'fir8o3lsak', 'zwejklqn25') |
| 28 | + |
| 29 | + # Random prefixes to validate command line argument parsing |
| 30 | + prefix = ( |
| 31 | + 'd17weqroeg', 'riabfodkj5', 'fir8o3lsak', |
| 32 | + 'zwejklqn25', 'qtr2vfvwiu') |
30 | 33 |
|
31 | 34 | petsc_option = ( |
32 | 35 | ('ksp_rtol',), |
33 | 36 | ('ksp_rtol', 'ksp_atol'), |
34 | 37 | ('ksp_rtol', 'ksp_atol', 'ksp_divtol', 'ksp_max_it'), |
35 | | - ('ksp_type',) |
36 | | - |
| 38 | + ('ksp_type',), |
| 39 | + ('ksp_divtol', 'ksp_type') |
37 | 40 | ) |
38 | 41 | value = ( |
39 | 42 | (1e-8,), |
40 | 43 | (1e-11, 1e-15), |
41 | 44 | (1e-3, 1e-10, 50000, 2000), |
42 | | - ('cg',) |
| 45 | + ('cg',), |
| 46 | + (22000, 'richardson'), |
43 | 47 | ) |
44 | 48 | argv = [] |
45 | 49 | expected = {} |
@@ -1663,9 +1667,6 @@ def test_tolerances(self, log_level): |
1663 | 1667 |
|
1664 | 1668 | @skipif('petsc') |
1665 | 1669 | def test_clearing_options(self): |
1666 | | - # TODO: Extend this test to check that options are only cleared |
1667 | | - # if they have NOT ben set via the command line |
1668 | | - |
1669 | 1670 | # Explicitly set the solver parameters |
1670 | 1671 | solver1 = PETScSolve( |
1671 | 1672 | self.eq1, target=self.e, solver_parameters={'ksp_rtol': '1e-10'} |
@@ -1737,7 +1738,7 @@ def test_multiple_operators(self, log_level): |
1737 | 1738 | def test_command_line_priority_tols_1(self, command_line, log_level): |
1738 | 1739 | """ |
1739 | 1740 | Test solver tolerances specifed via the command line |
1740 | | - take precedence over those set by the defaults. |
| 1741 | + take precedence over those specified in the defaults. |
1741 | 1742 | """ |
1742 | 1743 | prefix = 'd17weqroeg' |
1743 | 1744 | _, expected = command_line |
@@ -1838,6 +1839,55 @@ def test_command_line_priority_ksp_type(self, command_line, log_level): |
1838 | 1839 | assert entry.KSPGetType == val |
1839 | 1840 | assert not entry.KSPGetType == params['ksp_type'] |
1840 | 1841 |
|
| 1842 | + @skipif('petsc') |
| 1843 | + def test_not_set_or_clear_command_line_opts(self, command_line): |
| 1844 | + """ |
| 1845 | + Verify that if an option is set via the command line, |
| 1846 | + the corresponding entry in `linear_solve_defaults` or in the user |
| 1847 | + specified `solver_parameters` is not set or cleared in the |
| 1848 | + generated code. (The command line option will have already been set in |
| 1849 | + the global PetscOptions database during PetscInitialize().) |
| 1850 | + """ |
| 1851 | + prefix = 'qtr2vfvwiu' |
| 1852 | + |
| 1853 | + solver = PETScSolve( |
| 1854 | + self.eq1, target=self.e, |
| 1855 | + # Specify a solver parameter that is not set via the |
| 1856 | + # command line (see the `command_line` fixture for this prefix). |
| 1857 | + solver_parameters={'ksp_rtol': '1e-10'}, |
| 1858 | + options_prefix=prefix |
| 1859 | + ) |
| 1860 | + with switchconfig(language='petsc'): |
| 1861 | + op = Operator(solver) |
| 1862 | + |
| 1863 | + set_options_callback = str(op._func_table['SetPetscOptions0'].root) |
| 1864 | + clear_options_callback = str(op._func_table['ClearPetscOptions0'].root) |
| 1865 | + |
| 1866 | + # Check that the `ksp_rtol` option IS set and cleared explicitly |
| 1867 | + # since it is NOT set via the command line. |
| 1868 | + assert f'PetscOptionsSetValue(NULL,"-{prefix}_ksp_rtol","1e-10")' \ |
| 1869 | + in set_options_callback |
| 1870 | + assert f'PetscOptionsClearValue(NULL,"-{prefix}_ksp_rtol")' \ |
| 1871 | + in clear_options_callback |
| 1872 | + |
| 1873 | + # Check that the `ksp_divtol` and `ksp_type` options are NOT set |
| 1874 | + # or cleared explicitly since they ARE set via the command line. |
| 1875 | + assert f'PetscOptionsSetValue(NULL,"-{prefix}_div_tol",' \ |
| 1876 | + not in set_options_callback |
| 1877 | + assert f'PetscOptionsSetValue(NULL,"-{prefix}_ksp_type",' \ |
| 1878 | + not in set_options_callback |
| 1879 | + assert f'PetscOptionsClearValue(NULL,"-{prefix}_div_tol"));' \ |
| 1880 | + not in clear_options_callback |
| 1881 | + assert f'PetscOptionsClearValue(NULL,"-{prefix}_ksp_type"));' \ |
| 1882 | + not in clear_options_callback |
| 1883 | + |
| 1884 | + # Check that options specifed by the `linear_solver_defaults` |
| 1885 | + # are still set and cleared |
| 1886 | + assert f'PetscOptionsSetValue(NULL,"-{prefix}_ksp_atol",' \ |
| 1887 | + in set_options_callback |
| 1888 | + assert f'PetscOptionsClearValue(NULL,"-{prefix}_ksp_atol"));' \ |
| 1889 | + in clear_options_callback |
| 1890 | + |
1841 | 1891 |
|
1842 | 1892 | class TestHashing: |
1843 | 1893 |
|
|
0 commit comments