/[escript]/trunk/downunder/test/python/run_regularization.py
ViewVC logotype

Contents of /trunk/downunder/test/python/run_regularization.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5001 - (show annotations)
Thu Jun 5 23:59:21 2014 UTC (5 years ago) by gross
File MIME type: text/x-python
File size: 3800 byte(s)
regularization test added.
1
2 ##############################################################################
3 #
4 # Copyright (c) 2012-2014 by University of Queensland
5 # http://www.uq.edu.au
6 #
7 # Primary Business: Queensland, Australia
8 # Licensed under the Open Software License version 3.0
9 # http://www.opensource.org/licenses/osl-3.0.php
10 #
11 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 # Development 2012-2013 by School of Earth Sciences
13 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 #
15 ##############################################################################
16
17 __copyright__="""Copyright (c) 2012-2014 by University of Queensland
18 http://www.uq.edu.au
19 Primary Business: Queensland, Australia"""
20 __license__="""Licensed under the Open Software License version 3.0
21 http://www.opensource.org/licenses/osl-3.0.php"""
22 __url__="https://launchpad.net/escript-finley"
23
24 import esys.escriptcore.utestselect as unittest
25 from esys.escriptcore.testing import *
26 import sys
27 import esys.ripley
28 from esys.downunder import *
29 from esys.escript import *
30 import numpy as np
31
32
33 class TestRegularizaton2D(unittest.TestCase):
34 def setUp(self):
35 self.domain = esys.ripley.Rectangle(20*getMPISizeWorld()-1,20,d0=getMPISizeWorld()) # expected dimension 1mx1m
36 self.COORDINATES=CartesianReferenceSystem()
37
38 def tearDown(self):
39 del self.domain
40
41 # standart inversion case
42 def test_ConstantLevelSet1(self): # doesn't test the regularization
43
44 reg=Regularization(dom, numLevelSets=1,
45 w1=[1,1.], # consider gradient terms
46 #wc=[[0,1],[0,0]], # and cross-gradient term
47 coordinates=self.COORDINATES)
48 #location_of_set_m=reg_mask)
49 cf=InversionCostFunction(reg, pm1, sm1)
50
51
52 m=Scalar(1., Solution(dom))
53 args=cf.getArguments(m)
54 df=cf.getValue(m, *args)
55 gf=cf.getGradient(m, *args)[0]
56
57 self.assertIsInstance(df, float)
58 self.assertAlmostEqual(df, sm1.getRealValue(s0))
59
60 self.assertIsInstance(gf, Data)
61 self.assertEqual(gf.getFunctionSpace(), Function(dom))
62 self.assertEqual(gf.getShape(), ())
63
64
65 STEP=0.001
66 m2=m+STEP
67 df2=cf.getValue(m2, *(cf.getArguments(m2)))
68 gf2=df2-df
69 self.assertTrue( Lsup(gf2-gf*STEP) < 1e-3 * Lsup(gf2) )
70
71 def test_ConstantLevelSet2(self): # doesn't test the regularization
72 reg=Regularization(dom, numLevelSets=2,
73 w1=[[1,1.],[1.,1.]], # consider gradient terms
74 wc=[[0,0],[0,0]], # and cross-gradient term
75 coordinates=self.COORDINATES)
76 #location_of_set_m=reg_mask)
77
78
79 m=Data([1.,2], Solution(dom))
80 args=cf.getArguments(m)
81
82 df=cf.getValue(m, *args)
83 df_real=sm1.getRealValue(s[0]*1.)+sm2.getRealValue(s[1]*2.)
84 gf=cf.getGradient(m, *args)[0]
85
86 self.assertIsInstance(df, float)
87 self.assertAlmostEqual(df, df_real)
88
89 self.assertIsInstance(gf, Data)
90 self.assertEqual(gf.getFunctionSpace(), Function(dom))
91 self.assertEqual(gf.getShape(), (2,))
92
93
94 STEP=0.001
95
96 m2=m+[STEP,0]
97 df2=cf.getValue(m2, *(cf.getArguments(m2)))
98 gf2=df2-df
99 self.assertTrue( Lsup(gf2-gf[0]*STEP) < 1e-3 * Lsup(gf2) )
100
101 m2=m+[0, STEP]
102 df2=cf.getValue(m2, *(cf.getArguments(m2)))
103 gf2=df2-df
104 self.assertTrue( Lsup(gf2-gf[1]*STEP) < 1e-3 * Lsup(gf2) )
105
106
107 if __name__ == '__main__':
108 run_tests(__name__, exit_on_failure=True)
109

  ViewVC Help
Powered by ViewVC 1.1.26