33import numpy as np
44import os
55import re
6- from collections import OrderedDict
76
87from conftest import skipif
98from devito import (Grid , Function , TimeFunction , Eq , Operator ,
@@ -1437,7 +1436,6 @@ def test_logging(self, log_level):
14371436
14381437 assert snesits0 == snesits1 == snesits2 == snesits3
14391438
1440- assert isinstance (snesits0 , OrderedDict )
14411439 assert len (snesits0 ) == 1
14421440 key , value = next (iter (snesits0 .items ()))
14431441 assert str (key ) == "PetscKey(name='section0', options_prefix='poisson')"
@@ -1813,20 +1811,25 @@ class TestGetInfo:
18131811 iterations to converge.
18141812 """
18151813 @skipif ('petsc' )
1816- def test_get_info (self ):
1817-
1814+ def setup_class (self ):
1815+ """
1816+ Setup grid, functions and equations shared across
1817+ tests in this class
1818+ """
18181819 grid = Grid (shape = (11 , 11 ), dtype = np .float64 )
1819- functions = [Function (name = n , grid = grid , space_order = 2 )
1820- for n in ['e' , 'f' ]]
1821- e , f = functions
1822- eq = Eq (e .laplace , f )
1820+ self .e , self .f , self .g , self .h = [
1821+ Function (name = n , grid = grid , space_order = 2 )
1822+ for n in ['e' , 'f' , 'g' , 'h' ]
1823+ ]
1824+ self .eq1 = Eq (self .e .laplace , self .f )
1825+ self .eq2 = Eq (self .g .laplace , self .h )
18231826
1827+ @skipif ('petsc' )
1828+ def test_get_info (self ):
18241829 get_info = ['kspgetiterationnumber' , 'snesgetiterationnumber' ]
1825-
18261830 petsc = PETScSolve (
1827- eq , target = e , options_prefix = 'pde1' , get_info = get_info
1831+ self . eq1 , target = self . e , options_prefix = 'pde1' , get_info = get_info
18281832 )
1829-
18301833 with switchconfig (language = 'petsc' ):
18311834 op = Operator (petsc )
18321835 summary = op .apply ()
@@ -1846,18 +1849,10 @@ def test_get_info_with_logging(self, log_level):
18461849 """
18471850 Test that `get_info` works correctly when logging is enabled.
18481851 """
1849- grid = Grid (shape = (11 , 11 ), dtype = np .float64 )
1850- functions = [Function (name = n , grid = grid , space_order = 2 )
1851- for n in ['e' , 'f' ]]
1852- e , f = functions
1853- eq = Eq (e .laplace , f )
1854-
18551852 get_info = ['kspgetiterationnumber' ]
1856-
18571853 petsc = PETScSolve (
1858- eq , target = e , options_prefix = 'pde1' , get_info = get_info
1854+ self . eq1 , target = self . e , options_prefix = 'pde1' , get_info = get_info
18591855 )
1860-
18611856 with switchconfig (language = 'petsc' , log_level = log_level ):
18621857 op = Operator (petsc )
18631858 summary = op .apply ()
@@ -1878,26 +1873,17 @@ def test_different_solvers(self):
18781873 Test that `get_info` works correctly when multiple solvers are used
18791874 within the same Operator.
18801875 """
1881- grid = Grid (shape = (11 , 11 ), dtype = np .float64 )
1882- functions = [Function (name = n , grid = grid , space_order = 2 )
1883- for n in ['e' , 'f' , 'g' , 'h' ]]
1884- e , f , g , h = functions
1885-
1886- eq1 = Eq (e .laplace , f )
1887- eq2 = Eq (g .laplace , h )
1888-
18891876 # Create two PETScSolve instances with different get_info arguments
18901877
18911878 get_info_1 = ['kspgetiterationnumber' ]
18921879 get_info_2 = ['snesgetiterationnumber' ]
18931880
18941881 solver1 = PETScSolve (
1895- eq1 , target = e , options_prefix = 'pde1' , get_info = get_info_1
1882+ self . eq1 , target = self . e , options_prefix = 'pde1' , get_info = get_info_1
18961883 )
18971884 solver2 = PETScSolve (
1898- eq2 , target = g , options_prefix = 'pde2' , get_info = get_info_2
1885+ self . eq2 , target = self . g , options_prefix = 'pde2' , get_info = get_info_2
18991886 )
1900-
19011887 with switchconfig (language = 'petsc' ):
19021888 op = Operator ([solver1 , solver2 ])
19031889 summary = op .apply ()
@@ -1916,3 +1902,59 @@ def test_different_solvers(self):
19161902
19171903 assert not hasattr (entry2 , "KSPGetIterationNumber" )
19181904 assert hasattr (entry2 , "SNESGetIterationNumber" )
1905+
1906+ @skipif ('petsc' )
1907+ def test_case_insensitive (self ):
1908+ """
1909+ Test that `get_info` is case insensitive
1910+ """
1911+ # Create a list with mixed cases
1912+ get_info = ['KSPGetIterationNumber' , 'snesgetiterationnumber' ]
1913+ petsc = PETScSolve (
1914+ self .eq1 , target = self .e , options_prefix = 'pde1' , get_info = get_info
1915+ )
1916+ with switchconfig (language = 'petsc' ):
1917+ op = Operator (petsc )
1918+ summary = op .apply ()
1919+
1920+ petsc_summary = summary .petsc
1921+ entry = petsc_summary .get_entry ('section0' , 'pde1' )
1922+
1923+ assert hasattr (entry , "KSPGetIterationNumber" )
1924+ assert hasattr (entry , "SNESGetIterationNumber" )
1925+
1926+ @skipif ('petsc' )
1927+ def test_get_ksp_type (self ):
1928+ """
1929+ Test that `get_info` can retrieve the KSP type as
1930+ a string.
1931+ """
1932+ get_info = ['kspgettype' ]
1933+ solver1 = PETScSolve (
1934+ self .eq1 , target = self .e , options_prefix = 'poisson1' , get_info = get_info
1935+ )
1936+ solver2 = PETScSolve (
1937+ self .eq1 , target = self .e , options_prefix = 'poisson2' ,
1938+ solver_parameters = {'ksp_type' : 'cg' }, get_info = get_info
1939+ )
1940+ with switchconfig (language = 'petsc' ):
1941+ op = Operator ([solver1 , solver2 ])
1942+ summary = op .apply ()
1943+
1944+ petsc_summary = summary .petsc
1945+ entry1 = petsc_summary .get_entry ('section0' , 'poisson1' )
1946+ entry2 = petsc_summary .get_entry ('section1' , 'poisson2' )
1947+
1948+ assert hasattr (entry1 , "KSPGetType" )
1949+ # Check the type matches the default in linear_solve_defaults
1950+ # since it has not been overridden
1951+ assert entry1 .KSPGetType == linear_solve_defaults ['ksp_type' ]
1952+ assert entry1 ['KSPGetType' ] == linear_solve_defaults ['ksp_type' ]
1953+ assert entry1 ['kspgettype' ] == linear_solve_defaults ['ksp_type' ]
1954+
1955+ # Test that the KSP type default is correctly overridden by the
1956+ # solver_parameters dictionary passed to solver2
1957+ assert hasattr (entry2 , "KSPGetType" )
1958+ assert entry2 .KSPGetType == 'cg'
1959+ assert entry2 ['KSPGetType' ] == 'cg'
1960+ assert entry2 ['kspgettype' ] == 'cg'
0 commit comments