1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
##############################################################################
#
# Copyright (c) 2003-2018 by The University of Queensland
# http://www.uq.edu.au
#
# Primary Business: Queensland, Australia
# Licensed under the Apache License, version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Development until 2012 by Earth Systems Science Computational Center (ESSCC)
# Development 2012-2013 by School of Earth Sciences
# Development from 2014 by Centre for Geoscience Computing (GeoComp)
#
##############################################################################
from __future__ import print_function, division
__copyright__="""Copyright (c) 2003-2018 by The University of Queensland
http://www.uq.edu.au
Primary Business: Queensland, Australia"""
__license__="""Licensed under the Apache License, version 2.0
http://www.apache.org/licenses/LICENSE-2.0"""
__url__="https://launchpad.net/escript-finley"
"""
Generic base class for PDE solving tests
"""
from esys.escript import Data, Function, Lsup, Solution, Tensor4, Vector, \
grad, inner, kronecker, matrixmult, whereZero, hasFeature
from esys.escript.linearPDEs import LinearPDE, SolverOptions
import esys.escriptcore.utestselect as unittest
import numpy
HAVE_DIRECT_PASO = hasFeature('paso') and (hasFeature('umfpack') or hasFeature("mkl") or hasFeature("mumps"))
HAVE_MUMPS = hasFeature("mumps")
HAVE_TRILINOS = hasFeature('trilinos')
HAVE_SOLVER = HAVE_DIRECT_PASO or HAVE_TRILINOS
HAVE_SOLVER_COMPLEX = HAVE_TRILINOS or HAVE_MUMPS
class SolveTestCaseTemplate(unittest.TestCase):
"""
this is the template class for testing solvers:
"""
REL_TOL = 1.e-6
SOLVER_VERBOSE = False
SOLVER_TOL = 1.e-8
# the following members must be set by the test methods in subclasses
domain = None
package = None
method = None
preconditioner = SolverOptions.NO_PRECONDITIONER
def getPDE(self, system, iscomplex=False):
dim = self.domain.getDim()
if system:
pde=LinearPDE(self.domain, numEquations=dim, isComplex=iscomplex)
else:
pde=LinearPDE(self.domain, numEquations=1, isComplex=iscomplex)
self.setCoefficients(pde, system)
so = pde.getSolverOptions()
so.setPackage(self.package)
so.setSolverMethod(self.method)
so.setPreconditioner(self.preconditioner)
so.setTolerance(self.SOLVER_TOL)
so.setVerbosity(self.SOLVER_VERBOSE)
pde.setSolverOptions(so)
return pde, self.getSolution(system), self.getGrad(system)
class SolveTestCaseOrder1(SolveTestCaseTemplate):
"""
this is the class for testing solvers for order 1 meshes:
"""
def getGrad(self, system):
"""returns exact gradient"""
dim = self.domain.getDim()
if system:
g_ex = Data(0., (dim,dim), Solution(self.domain))
if dim == 2:
g_ex[0,0] = 2.
g_ex[0,1] = 3.
g_ex[1,0] = 3.
g_ex[1,1] = 2.
else:
g_ex[0,0] = 2.
g_ex[0,1] = 3.
g_ex[0,2] = 4.
g_ex[1,0] = 4.
g_ex[1,1] = 1.
g_ex[1,2] = -2.
g_ex[2,0] = 8.
g_ex[2,1] = 4.
g_ex[2,2] = 5.
else:
g_ex = Data(0., (dim,), Solution(self.domain))
if dim == 2:
g_ex[0] = 2.
g_ex[1] = 3.
else:
g_ex[0] = 2.
g_ex[1] = 3.
g_ex[2] = 4.
return g_ex
def getSolution(self, system):
"""returns exact solution"""
dim = self.domain.getDim()
x = Solution(self.domain).getX()
if system:
u_ex = Vector(0., Solution(self.domain))
if dim == 2:
u_ex[0] = 1.+2.*x[0]+3.*x[1]
u_ex[1] = -1.+3.*x[0]+2.*x[1]
else:
u_ex[0] = 1.+2.*x[0]+3.*x[1]+4.*x[2]
u_ex[1] = -1.+4.*x[0]+1.*x[1]-2.*x[2]
u_ex[2] = 5.+8.*x[0]+4.*x[1]+5.*x[2]
else:
if dim == 2:
u_ex = 1.+2.*x[0]+3.*x[1]
else:
u_ex = 1.+2.*x[0]+3.*x[1]+4.*x[2]
return u_ex
def setCoefficients(self, pde, system):
"""sets PDE coefficients"""
FAC_DIAG = self.FAC_DIAG
FAC_OFFDIAG =self.FAC_OFFDIAG
x = Solution(self.domain).getX()
mask = whereZero(x[0])
dim = self.domain.getDim()
u_ex = self.getSolution(system)
g_ex = self.getGrad(system)
if system:
A = Tensor4(0., Function(self.domain))
for i in range(dim):
A[i,:,i,:] = kronecker(dim)
Y = Vector(0., Function(self.domain))
if dim == 2:
Y[0] = u_ex[0]*FAC_DIAG+u_ex[1]*FAC_OFFDIAG
Y[1] = u_ex[1]*FAC_DIAG+u_ex[0]*FAC_OFFDIAG
else:
Y[0] = u_ex[0]*FAC_DIAG+u_ex[2]*FAC_OFFDIAG+u_ex[1]*FAC_OFFDIAG
Y[1] = u_ex[1]*FAC_DIAG+u_ex[0]*FAC_OFFDIAG+u_ex[2]*FAC_OFFDIAG
Y[2] = u_ex[2]*FAC_DIAG+u_ex[1]*FAC_OFFDIAG+u_ex[0]*FAC_OFFDIAG
pde.setValue(r=u_ex, q=mask*numpy.ones(dim,),
A=A,
D=kronecker(dim)*(FAC_DIAG-FAC_OFFDIAG)+numpy.ones((dim,dim))*FAC_OFFDIAG,
Y=Y,
y=matrixmult(g_ex,self.domain.getNormal()))
else:
pde.setValue(r=u_ex, q=mask, A=kronecker(dim),
y=inner(g_ex, self.domain.getNormal()))
class SolveTestCaseOrder2(SolveTestCaseTemplate):
"""
this is the class for testing solvers for order 2 meshes:
"""
def getGrad(self, system):
"""returns exact gradient"""
dim = self.domain.getDim()
x = Solution(self.domain).getX()
if system:
g_ex = Data(0., (dim,dim), Solution(self.domain))
if dim == 2:
g_ex[0,0] = 2.+8.*x[0]+ 5.*x[1]
g_ex[0,1] = 3.+5.*x[0]+12.*x[1]
g_ex[1,0] = 4.+2.*x[0]+ 6.*x[1]
g_ex[1,1] = 2.+6.*x[0]+ 8.*x[1]
else:
g_ex[0,0] = 2.+6.*x[1]+8.*x[2]+18.*x[0]
g_ex[0,1] = 3.+6.*x[0]+7.*x[2]+20.*x[1]
g_ex[0,2] = 4.+7.*x[1]+8.*x[0]+22.*x[2]
g_ex[1,0] = 4.+3.*x[1]-8.*x[2]- 4.*x[0]
g_ex[1,1] = 1.+3.*x[0]+2.*x[2]+14.*x[1]
g_ex[1,2] = -6.+2.*x[1]-8.*x[0]+10.*x[2]
g_ex[2,0] = 7.-6.*x[1]+2.*x[2]+ 4.*x[0]
g_ex[2,1] = 9.-6.*x[0]+8.*x[2]+16.*x[1]
g_ex[2,2] = 2.+8.*x[1]+2.*x[0]+ 2.*x[2]
else:
g_ex = Data(0., (dim,), Solution(self.domain))
if dim == 2:
g_ex[0] = 2.+8.*x[0]+5.*x[1]
g_ex[1] = 3.+5.*x[0]+12.*x[1]
else:
g_ex[0] = 2.+6.*x[1]+8.*x[2]+18.*x[0]
g_ex[1] = 3.+6.*x[0]+7.*x[2]+20.*x[1]
g_ex[2] = 4.+7.*x[1]+8.*x[0]+22.*x[2]
return g_ex
def getSolution(self, system):
"""returns exact solution"""
dim = self.domain.getDim()
x = Solution(self.domain).getX()
if system:
u_ex = Vector(0., Solution(self.domain))
if dim == 2:
u_ex[0] = 1.+2.*x[0]+3.*x[1]+4.*x[0]**2+5.*x[1]*x[0]+6.*x[1]**2
u_ex[1] = -1.+4.*x[0]+2.*x[1]+1.*x[0]**2+6.*x[1]*x[0]+4.*x[1]**2
else:
u_ex[0] = 1.+2.*x[0]+3.*x[1]+4.*x[2]+\
6.*x[0]*x[1]+7.*x[1]*x[2]+8.*x[2]*x[0]+\
9.*x[0]**2+10.*x[1]**2+11.*x[2]**2
u_ex[1] = 2.+4.*x[0]+1.*x[1]-6.*x[2]+\
3.*x[0]*x[1]+2.*x[1]*x[2]-8.*x[2]*x[0]-\
2.*x[0]**2+7.*x[1]**2+5.*x[2]**2
u_ex[2] = -2.+7.*x[0]+9.*x[1]+2*x[2]-\
6.*x[0]*x[1]+8.*x[1]*x[2]+2.*x[2]*x[0]+\
2.*x[0]**2+8.*x[1]**2+1.*x[2]**2
else:
if dim == 2:
u_ex = 1.+2.*x[0]+3.*x[1]+4.*x[0]**2+5.*x[1]*x[0]+6.*x[1]**2
else:
u_ex = 1.+2.*x[0]+3.*x[1]+4.*x[2]+\
6.*x[0]*x[1]+7.*x[1]*x[2]+8.*x[2]*x[0]+\
9.*x[0]**2+10.*x[1]**2+11.*x[2]**2
return u_ex
def setCoefficients(self, pde, system):
"""sets PDE coefficients"""
FAC_DIAG = self.FAC_DIAG
FAC_OFFDIAG =self.FAC_OFFDIAG
x = Solution(self.domain).getX()
mask = whereZero(x[0])
dim = self.domain.getDim()
u_ex = self.getSolution(system)
g_ex = self.getGrad(system)
if system:
A = Tensor4(0., Function(self.domain))
for i in range(dim):
A[i,:,i,:] = kronecker(dim)
Y = Vector(0., Function(self.domain))
if dim == 2:
Y[0] = u_ex[0]*FAC_DIAG+u_ex[1]*FAC_OFFDIAG-20
Y[1] = u_ex[1]*FAC_DIAG+u_ex[0]*FAC_OFFDIAG-10
else:
Y[0] = u_ex[0]*FAC_DIAG+u_ex[2]*FAC_OFFDIAG+u_ex[1]*FAC_OFFDIAG-60
Y[1] = u_ex[1]*FAC_DIAG+u_ex[0]*FAC_OFFDIAG+u_ex[2]*FAC_OFFDIAG-20
Y[2] = u_ex[2]*FAC_DIAG+u_ex[1]*FAC_OFFDIAG+u_ex[0]*FAC_OFFDIAG-22
pde.setValue(r=u_ex, q=mask*numpy.ones(dim,),
A=A,
D=kronecker(dim)*(FAC_DIAG-FAC_OFFDIAG)+numpy.ones((dim,dim))*FAC_OFFDIAG,
Y=Y,
y=matrixmult(g_ex,self.domain.getNormal()))
else:
pde.setValue(r=u_ex, q=mask, A=kronecker(dim),
y=inner(g_ex, self.domain.getNormal()))
if dim == 2:
pde.setValue(Y=-20.)
else:
pde.setValue(Y=-60.)
class SimpleSolveTestCase(SolveTestCaseOrder1):
"""
testing the real PDEs
"""
FAC_DIAG = 1.
FAC_OFFDIAG = -0.4
def test_single(self):
pde, u_ex, g_ex = self.getPDE(False)
g=grad(u_ex)
self.assertLess(Lsup(g_ex-g), self.REL_TOL*Lsup(g_ex))
u = pde.getSolution()
self.assertFalse(u.isComplex())
self.assertEqual(u.getShape(), ( ))
error = Lsup(u-u_ex)
self.assertLess(error, self.REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
@unittest.skipIf(not HAVE_SOLVER, "No solver available")
def test_system(self):
pde, u_ex, g_ex = self.getPDE(True)
g = grad(u_ex)
self.assertLess(Lsup(g_ex-g), self.REL_TOL*Lsup(g_ex))
u = pde.getSolution()
self.assertFalse(u.isComplex())
self.assertEqual(u.getShape(), (pde.getDim(), ))
error = Lsup(u-u_ex)
self.assertLess(error, self.REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
class SimpleSolveTestCaseOrder2(SolveTestCaseOrder2):
"""
testing the real PDEs
"""
FAC_DIAG = 1.
FAC_OFFDIAG = -0.4
def test_single(self):
pde, u_ex, g_ex = self.getPDE(False)
g=grad(u_ex)
self.assertLess(Lsup(g_ex-g), self.REL_TOL*Lsup(g_ex))
u = pde.getSolution()
self.assertFalse(u.isComplex())
self.assertEqual(u.getShape(), ( ))
error = Lsup(u-u_ex)
self.assertLess(error, self.REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
@unittest.skipIf(not HAVE_SOLVER, "No solver available")
def test_system(self):
pde, u_ex, g_ex = self.getPDE(True)
g = grad(u_ex)
self.assertLess(Lsup(g_ex-g), self.REL_TOL*Lsup(g_ex))
u = pde.getSolution()
error = Lsup(u-u_ex)
self.assertFalse(u.isComplex())
self.assertEqual(u.getShape(), (pde.getDim(), ))
self.assertLess(error, self.REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
class ComplexSolveTestCase(SolveTestCaseOrder1):
"""
testing the complex PDEs
"""
FAC_DIAG = 1.+0.2j
FAC_OFFDIAG = -0.4
@unittest.skipIf(not HAVE_SOLVER_COMPLEX, "No solver available")
def test_singlecomplex(self):
pde, u_ex, g_ex = self.getPDE(False, iscomplex=True)
g=grad(u_ex)
self.assertLess(Lsup(g_ex-g), self.REL_TOL*Lsup(g_ex))
u = pde.getSolution()
error = Lsup(u-u_ex)
self.assertTrue(u.isComplex())
self.assertEqual(u.getShape(), ())
self.assertLess(error, self.REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
@unittest.skipIf(not HAVE_SOLVER_COMPLEX, "No solver available")
def test_systemcomplex(self):
pde, u_ex, g_ex = self.getPDE(True, iscomplex=True)
g = grad(u_ex)
self.assertLess(Lsup(g_ex-g), self.REL_TOL*Lsup(g_ex))
u = pde.getSolution()
error = Lsup(u-u_ex)
self.assertTrue(u.isComplex())
self.assertEqual(u.getShape(), (pde.getDim(),))
self.assertLess(error, self.REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
class ComplexSolveTestCaseOrder2(SolveTestCaseOrder2):
"""
testing the complex PDEs for order 2 meshes
"""
FAC_DIAG = 1.+0.2j
FAC_OFFDIAG = -0.4
@unittest.skipIf(not HAVE_SOLVER_COMPLEX, "No solver available")
def test_singlecomplex(self):
pde, u_ex, g_ex = self.getPDE(False, iscomplex=True)
g=grad(u_ex)
self.assertLess(Lsup(g_ex-g), self.REL_TOL*Lsup(g_ex))
u = pde.getSolution()
self.assertTrue(u.isComplex())
self.assertEqual(u.getShape(), ( ))
error = Lsup(u-u_ex)
self.assertLess(error, self.REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
@unittest.skipIf(not HAVE_SOLVER_COMPLEX, "No solver available")
def test_systemcomplex(self):
pde, u_ex, g_ex = self.getPDE(True, iscomplex=True)
g = grad(u_ex)
self.assertLess(Lsup(g_ex-g), self.REL_TOL*Lsup(g_ex))
u = pde.getSolution()
error = Lsup(u-u_ex)
self.assertTrue(u.isComplex())
self.assertEqual(u.getShape(), (pde.getDim(), ))
self.assertLess(error, self.REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
|