1 | """Automatic verification of ANUGA flows. |
---|
2 | See functions exercised by this wrapper for more details |
---|
3 | """ |
---|
4 | |
---|
5 | import unittest |
---|
6 | import os |
---|
7 | import numpy |
---|
8 | import anuga |
---|
9 | |
---|
10 | indent = anuga.indent |
---|
11 | |
---|
12 | verbose = True |
---|
13 | |
---|
14 | class Test_results(unittest.TestCase): |
---|
15 | def setUp(self): |
---|
16 | for file in os.listdir('.'): |
---|
17 | if file.endswith('.stdout') or\ |
---|
18 | file.endswith('.sww') or\ |
---|
19 | file.endswith('.msh') or\ |
---|
20 | file.endswith('.png'): |
---|
21 | os.remove(file) |
---|
22 | |
---|
23 | |
---|
24 | def tearDown(self): |
---|
25 | pass |
---|
26 | |
---|
27 | def test_simulation(self): |
---|
28 | |
---|
29 | |
---|
30 | if verbose: |
---|
31 | print |
---|
32 | print indent+'Running simulation script' |
---|
33 | |
---|
34 | s = 'produce_results.py' |
---|
35 | res = os.system('python %s > validate_output.stdout' %s) |
---|
36 | |
---|
37 | # Test that script runs ok |
---|
38 | assert res == 0 |
---|
39 | |
---|
40 | |
---|
41 | if verbose: |
---|
42 | print indent+'Testing accuracy' |
---|
43 | |
---|
44 | |
---|
45 | import anuga.utilities.plot_utils as util |
---|
46 | from matplotlib import pyplot as pyplot |
---|
47 | from analytical_avalanche_wet import analytical_sol |
---|
48 | |
---|
49 | p_st = util.get_output('avalanche.sww') |
---|
50 | p2_st=util.get_centroids(p_st) |
---|
51 | |
---|
52 | v = p2_st.y[20] |
---|
53 | v2=(p2_st.y==v) |
---|
54 | |
---|
55 | x_n = p2_st.x[v2] |
---|
56 | |
---|
57 | u0,h0,w0,z0,p0 = analytical_sol(x_n, p2_st.time[0]) |
---|
58 | u20,h20,w20,z20,p20 = analytical_sol(x_n, p2_st.time[20]) |
---|
59 | u40,h40,w40,z40,p40 = analytical_sol(x_n, p2_st.time[40]) |
---|
60 | |
---|
61 | |
---|
62 | |
---|
63 | w0_n = p2_st.stage[0,v2] |
---|
64 | w20_n = p2_st.stage[20,v2] |
---|
65 | w40_n = p2_st.stage[40,v2] |
---|
66 | |
---|
67 | z_n = p2_st.elev[v2] |
---|
68 | |
---|
69 | uh0_n = p2_st.xmom[0,v2] |
---|
70 | uh20_n = p2_st.xmom[20,v2] |
---|
71 | uh40_n = p2_st.xmom[40,v2] |
---|
72 | |
---|
73 | u0_n = p2_st.xvel[0,v2] |
---|
74 | u20_n = p2_st.xvel[20,v2] |
---|
75 | u40_n = p2_st.xvel[40,v2] |
---|
76 | |
---|
77 | |
---|
78 | #Test stages |
---|
79 | # Calculate L^1 error at times corrsponding to slices 20, 40 |
---|
80 | eh20 = numpy.sum(numpy.abs(w20_n-w20))/numpy.sum(numpy.abs(w20)) |
---|
81 | eh40 = numpy.sum(numpy.abs(w40_n-w40))/numpy.sum(numpy.abs(w40)) |
---|
82 | |
---|
83 | |
---|
84 | if verbose: |
---|
85 | print indent+'Errors in stage: ',eh20, eh40 |
---|
86 | |
---|
87 | assert eh20 < 0.01, 'L^1 error %g greater than 1 percent'% eh20 |
---|
88 | assert eh40 < 0.01, 'L^1 error %g greater than 1 percent'% eh40 |
---|
89 | |
---|
90 | |
---|
91 | #Test xmomenta |
---|
92 | # Calculate L^1 error at times corrsponding to slices 20, 40 |
---|
93 | euh20 = numpy.sum(numpy.abs(uh20_n-u20*h20))/numpy.sum(numpy.abs(u20*h20)) |
---|
94 | euh40 = numpy.sum(numpy.abs(uh40_n-u40*h40))/numpy.sum(numpy.abs(u40*h40)) |
---|
95 | |
---|
96 | |
---|
97 | if verbose: |
---|
98 | print indent+'Errors in xmomentum: ',euh20, euh40 |
---|
99 | |
---|
100 | |
---|
101 | assert euh20 < 0.025, 'L^1 error %g greater than 2.5 percent'% euh20 |
---|
102 | assert euh40 < 0.025, 'L^1 error %g greater than 2.5 percent'% euh40 |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | #Test xvelocity |
---|
107 | # Calculate L^1 error at times corrsponding to slices 20, 40 |
---|
108 | eu20 = numpy.sum(numpy.abs(u20_n-u20))/numpy.sum(numpy.abs(u20)) |
---|
109 | eu40 = numpy.sum(numpy.abs(u40_n-u40))/numpy.sum(numpy.abs(u40)) |
---|
110 | |
---|
111 | if verbose: |
---|
112 | print indent+'Errors in xvelocity: ', eu20, eu40 |
---|
113 | |
---|
114 | assert eu20 < 0.02, 'L^1 error %g greater than 2 percent'% eu20 |
---|
115 | assert eu40 < 0.01, 'L^1 error %g greater than 2 percent'% eu40 |
---|
116 | |
---|
117 | |
---|
118 | #------------------------------------------------------------- |
---|
119 | if __name__ == '__main__': |
---|
120 | suite = unittest.makeSuite(Test_results, 'test') |
---|
121 | runner = unittest.TextTestRunner(verbosity=2) |
---|
122 | runner.run(suite) |
---|