<s> import numpy as np <EOL> import unittest <EOL> from SimPEG . Utils import mkvc <EOL> from SimPEG import Mesh , Tests <EOL> import unittest <EOL> MESHTYPES = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> TOLERANCES = [ <NUM_LIT> , <NUM_LIT:0.5> , <NUM_LIT:0.5> ] <EOL> call1 = lambda fun , xyz : fun ( xyz ) <EOL> call2 = lambda fun , xyz : fun ( xyz [ : , <NUM_LIT:0> ] , xyz [ : , - <NUM_LIT:1> ] ) <EOL> call3 = lambda fun , xyz : fun ( xyz [ : , <NUM_LIT:0> ] , xyz [ : , <NUM_LIT:1> ] , xyz [ : , <NUM_LIT:2> ] ) <EOL> cart_row2 = lambda g , xfun , yfun : np . c_ [ call2 ( xfun , g ) , call2 ( yfun , g ) ] <EOL> cart_row3 = lambda g , xfun , yfun , zfun : np . c_ [ call3 ( xfun , g ) , call3 ( yfun , g ) , call3 ( zfun , g ) ] <EOL> cartF2 = lambda M , fx , fy : np . vstack ( ( cart_row2 ( M . gridFx , fx , fy ) , cart_row2 ( M . gridFy , fx , fy ) ) ) <EOL> cartF2Cyl = lambda M , fx , fy : np . vstack ( ( cart_row2 ( M . gridFx , fx , fy ) , cart_row2 ( M . gridFz , fx , fy ) ) ) <EOL> cartE2 = lambda M , ex , ey : np . vstack ( ( cart_row2 ( M . gridEx , ex , ey ) , cart_row2 ( M . gridEy , ex , ey ) ) ) <EOL> cartE2Cyl = lambda M , ex , ey : cart_row2 ( M . gridEy , ex , ey ) <EOL> cartF3 = lambda M , fx , fy , fz : np . vstack ( ( cart_row3 ( M . gridFx , fx , fy , fz ) , cart_row3 ( M . gridFy , fx , fy , fz ) , cart_row3 ( M . gridFz , fx , fy , fz ) ) ) <EOL> cartE3 = lambda M , ex , ey , ez : np . vstack ( ( cart_row3 ( M . gridEx , ex , ey , ez ) , cart_row3 ( M . gridEy , ex , ey , ez ) , cart_row3 ( M . gridEz , ex , ey , ez ) ) ) <EOL> TOL = <NUM_LIT> <EOL> class TestInterpolation1D ( Tests . OrderTest ) : <EOL> LOCS = np . random . rand ( <NUM_LIT:50> ) * <NUM_LIT> + <NUM_LIT> <EOL> name = "<STR_LIT>" <EOL> meshTypes = MESHTYPES <EOL> tolerance = TOLERANCES <EOL> meshDimension = <NUM_LIT:1> <EOL> meshSizes = [ <NUM_LIT:8> , <NUM_LIT:16> , <NUM_LIT:32> , <NUM_LIT:64> , <NUM_LIT> ] <EOL> def getError ( self ) : <EOL> funX = lambda x : np . cos ( <NUM_LIT:2> * np . pi * x ) <EOL> ana = call1 ( funX , self . LOCS ) <EOL> if '<STR_LIT>' == self . type : <EOL> grid = call1 ( funX , self . M . gridCC ) <EOL> elif '<STR_LIT:N>' == self . type : <EOL> grid = call1 ( funX , self . M . gridN ) <EOL> comp = self . M . getInterpolationMat ( self . LOCS , self . type ) * grid <EOL> err = np . linalg . norm ( ( comp - ana ) , <NUM_LIT:2> ) <EOL> return err <EOL> def test_orderCC ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderN ( self ) : <EOL> self . type = '<STR_LIT:N>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> class TestOutliersInterp1D ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> pass <EOL> def test_outliers ( self ) : <EOL> M = Mesh . TensorMesh ( [ <NUM_LIT:4> ] ) <EOL> Q = M . getInterpolationMat ( np . array ( [ [ <NUM_LIT:0> ] , [ <NUM_LIT> ] , [ <NUM_LIT> ] ] ) , '<STR_LIT>' , zerosOutside = True ) <EOL> x = np . arange ( <NUM_LIT:4> ) + <NUM_LIT:1> <EOL> self . assertTrue ( np . linalg . norm ( Q * x - np . r_ [ <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT> ] ) < TOL ) <EOL> Q = M . getInterpolationMat ( np . array ( [ [ - <NUM_LIT:1> ] , [ <NUM_LIT> ] , [ <NUM_LIT> ] ] ) , '<STR_LIT>' , zerosOutside = True ) <EOL> self . assertTrue ( np . linalg . norm ( Q * x - np . r_ [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> ] ) < TOL ) <EOL> class TestInterpolation2d ( Tests . OrderTest ) : <EOL> name = "<STR_LIT>" <EOL> LOCS = np . random . rand ( <NUM_LIT:50> , <NUM_LIT:2> ) * <NUM_LIT> + <NUM_LIT> <EOL> meshTypes = MESHTYPES <EOL> tolerance = TOLERANCES <EOL> meshDimension = <NUM_LIT:2> <EOL> meshSizes = [ <NUM_LIT:8> , <NUM_LIT:16> , <NUM_LIT:32> , <NUM_LIT:64> ] <EOL> def getError ( self ) : <EOL> funX = lambda x , y : np . cos ( <NUM_LIT:2> * np . pi * y ) <EOL> funY = lambda x , y : np . cos ( <NUM_LIT:2> * np . pi * x ) <EOL> if '<STR_LIT:x>' in self . type : <EOL> ana = call2 ( funX , self . LOCS ) <EOL> elif '<STR_LIT:y>' in self . type : <EOL> ana = call2 ( funY , self . LOCS ) <EOL> else : <EOL> ana = call2 ( funX , self . LOCS ) <EOL> if '<STR_LIT:F>' in self . type : <EOL> Fc = cartF2 ( self . M , funX , funY ) <EOL> grid = self . M . projectFaceVector ( Fc ) <EOL> elif '<STR_LIT:E>' in self . type : <EOL> Ec = cartE2 ( self . M , funX , funY ) <EOL> grid = self . M . projectEdgeVector ( Ec ) <EOL> elif '<STR_LIT>' == self . type : <EOL> grid = call2 ( funX , self . M . gridCC ) <EOL> elif '<STR_LIT:N>' == self . type : <EOL> grid = call2 ( funX , self . M . gridN ) <EOL> comp = self . M . getInterpolationMat ( self . LOCS , self . type ) * grid <EOL> err = np . linalg . norm ( ( comp - ana ) , np . inf ) <EOL> return err <EOL> def test_orderCC ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderN ( self ) : <EOL> self . type = '<STR_LIT:N>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderFx ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderFy ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderEx ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderEy ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> class TestInterpolation2dCyl_Simple ( unittest . TestCase ) : <EOL> def test_simpleInter ( self ) : <EOL> M = Mesh . CylMesh ( [ <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> locs = np . r_ [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0.5> ] <EOL> fx = np . array ( [ [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> ] ] ) <EOL> self . assertTrue ( np . all ( fx == M . getInterpolationMat ( locs , '<STR_LIT>' ) . todense ( ) ) ) <EOL> fz = np . array ( [ [ <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.5> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.5> , <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> ] ] ) <EOL> self . assertTrue ( np . all ( fz == M . getInterpolationMat ( locs , '<STR_LIT>' ) . todense ( ) ) ) <EOL> def test_exceptions ( self ) : <EOL> M = Mesh . CylMesh ( [ <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> locs = np . r_ [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0.5> ] <EOL> self . assertRaises ( Exception , lambda : M . getInterpolationMat ( locs , '<STR_LIT>' ) ) <EOL> self . assertRaises ( Exception , lambda : M . getInterpolationMat ( locs , '<STR_LIT>' ) ) <EOL> self . assertRaises ( Exception , lambda : M . getInterpolationMat ( locs , '<STR_LIT>' ) ) <EOL> class TestInterpolation2dCyl ( Tests . OrderTest ) : <EOL> name = "<STR_LIT>" <EOL> LOCS = np . c_ [ np . random . rand ( <NUM_LIT:4> ) * <NUM_LIT> + <NUM_LIT> , np . zeros ( <NUM_LIT:4> ) , np . random . rand ( <NUM_LIT:4> ) * <NUM_LIT> + <NUM_LIT> ] <EOL> meshTypes = [ '<STR_LIT>' ] <EOL> tolerance = <NUM_LIT> <EOL> meshDimension = <NUM_LIT:2> <EOL> meshSizes = [ <NUM_LIT:32> , <NUM_LIT:64> , <NUM_LIT> , <NUM_LIT> ] <EOL> def getError ( self ) : <EOL> funX = lambda x , y : np . cos ( <NUM_LIT:2> * np . pi * y ) <EOL> funY = lambda x , y : np . cos ( <NUM_LIT:2> * np . pi * x ) <EOL> if '<STR_LIT:x>' in self . type : <EOL> ana = call2 ( funX , self . LOCS ) <EOL> elif '<STR_LIT:y>' in self . type : <EOL> ana = call2 ( funY , self . LOCS ) <EOL> elif '<STR_LIT:z>' in self . type : <EOL> ana = call2 ( funY , self . LOCS ) <EOL> else : <EOL> ana = call2 ( funX , self . LOCS ) <EOL> if '<STR_LIT>' == self . type : <EOL> Fc = cartF2Cyl ( self . M , funX , funY ) <EOL> Fc = np . c_ [ Fc [ : , <NUM_LIT:0> ] , np . zeros ( self . M . nF ) , Fc [ : , <NUM_LIT:1> ] ] <EOL> grid = self . M . projectFaceVector ( Fc ) <EOL> elif '<STR_LIT>' == self . type : <EOL> Fc = cartF2Cyl ( self . M , funX , funY ) <EOL> Fc = np . c_ [ Fc [ : , <NUM_LIT:0> ] , np . zeros ( self . M . nF ) , Fc [ : , <NUM_LIT:1> ] ] <EOL> grid = self . M . projectFaceVector ( Fc ) <EOL> elif '<STR_LIT:E>' in self . type : <EOL> Ec = cartE2Cyl ( self . M , funX , funY ) <EOL> grid = Ec [ : , <NUM_LIT:1> ] <EOL> elif '<STR_LIT>' == self . type : <EOL> grid = call2 ( funX , self . M . gridCC ) <EOL> elif '<STR_LIT:N>' == self . type : <EOL> grid = call2 ( funX , self . M . gridN ) <EOL> comp = self . M . getInterpolationMat ( self . LOCS , self . type ) * grid <EOL> err = np . linalg . norm ( ( comp - ana ) , np . inf ) <EOL> return err <EOL> def test_orderCC ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderN ( self ) : <EOL> self . type = '<STR_LIT:N>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderFx ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderFz ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderEy ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> class TestInterpolation3D ( Tests . OrderTest ) : <EOL> name = "<STR_LIT>" <EOL> LOCS = np . random . rand ( <NUM_LIT:50> , <NUM_LIT:3> ) * <NUM_LIT> + <NUM_LIT> <EOL> meshTypes = MESHTYPES <EOL> tolerance = TOLERANCES <EOL> meshDimension = <NUM_LIT:3> <EOL> meshSizes = [ <NUM_LIT:8> , <NUM_LIT:16> , <NUM_LIT:32> , <NUM_LIT:64> ] <EOL> def getError ( self ) : <EOL> funX = lambda x , y , z : np . cos ( <NUM_LIT:2> * np . pi * y ) <EOL> funY = lambda x , y , z : np . cos ( <NUM_LIT:2> * np . pi * z ) <EOL> funZ = lambda x , y , z : np . cos ( <NUM_LIT:2> * np . pi * x ) <EOL> if '<STR_LIT:x>' in self . type : <EOL> ana = call3 ( funX , self . LOCS ) <EOL> elif '<STR_LIT:y>' in self . type : <EOL> ana = call3 ( funY , self . LOCS ) <EOL> elif '<STR_LIT:z>' in self . type : <EOL> ana = call3 ( funZ , self . LOCS ) <EOL> else : <EOL> ana = call3 ( funX , self . LOCS ) <EOL> if '<STR_LIT:F>' in self . type : <EOL> Fc = cartF3 ( self . M , funX , funY , funZ ) <EOL> grid = self . M . projectFaceVector ( Fc ) <EOL> elif '<STR_LIT:E>' in self . type : <EOL> Ec = cartE3 ( self . M , funX , funY , funZ ) <EOL> grid = self . M . projectEdgeVector ( Ec ) <EOL> elif '<STR_LIT>' == self . type : <EOL> grid = call3 ( funX , self . M . gridCC ) <EOL> elif '<STR_LIT:N>' == self . type : <EOL> grid = call3 ( funX , self . M . gridN ) <EOL> comp = self . M . getInterpolationMat ( self . LOCS , self . type ) * grid <EOL> err = np . linalg . norm ( ( comp - ana ) , np . inf ) <EOL> return err <EOL> def test_orderCC ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderN ( self ) : <EOL> self . type = '<STR_LIT:N>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderFx ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderFy ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderFz ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderEx ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderEy ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> def test_orderEz ( self ) : <EOL> self . type = '<STR_LIT>' <EOL> self . name = '<STR_LIT>' <EOL> self . orderTest ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
