-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
221 lines (174 loc) · 6.73 KB
/
tests.py
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
import numpy as np
from scipy import special as sp
import time
import math
import container
import betas
import helper
from helper import dic as dic
############################################
# Test the cubature file and compilation ###
# Don't mess with it and don't bother, it ##
# would make your life miserable. ##########
############################################
if True:
def calculate( name, y, kappa, arr, tol):
if "simple" in name or "singular" in name:
result = np.empty( 1 )
else:
result = np.empty( 3 )
xpr = betas.generateInstant( name )
xpr.integrateVector( y, kappa, arr, tol, result )
return result
arr = np.array( [
-3, 0, # a
6, 0, # b
0, 3 # c
] , dtype = np.float64 )
tol = 1e-5
val = calculate( "simple2D", np.zeros(2), 1.23, arr, tol)
print "\n\nExample from some exam\n"
print "Computed integral = " + str (val[0] )
print "Analytic solution = " + str (13.5 )
print "Difference = " + str (val[0] - 13.5 )
print "Tolerance = " + str ( tol )
assert abs( val - 13.5 ) / 13.5 < tol
arr = np.array( [
0, 0, # a
1, 0, # b
1, 1 # c
], dtype = np.float64 )
val = calculate( "singular2D", np.zeros(2), 1.23, arr, tol)
print "\n\nExample I made\n"
print "Computed integral = " + str ( val[0] )
print "Analytic solution = " + str ( .184014120655140 )
print "Difference = " + str (val[0] - .184014120655140 )
print "Tolerance = " + str ( tol )
assert abs( val -.184014120655140 ) /.184014120655140 < tol
############################################
# Test the beta modules ####################
############################################
if True:
print "2D tests!!"
def enum2D( x0,x1, kappa, n, factor = 1 ):
'''
calculates the enumerator using the naivest
of integrations. No FENICS or anything - just
numpy and scipy. Returns a vector, because the
others also return a vector (which is supposed
to be dotted with the unit normal).
'''
ra = np.sqrt( x0*x0 + x1*x1 ) + 1e-13
kappara = kappa * ra
k0 = sp.kn( 0, kappara )
k1 = sp.kn( 1, kappara )
tmp = -kappa * factor * kappara * ( k0*k0 + k1*k1 ) / ra
return ( np.sum(tmp*x0) / n**2 , np.sum(tmp*x1) / n**2 )
def denom2D( x0,x1, kappa, n, factor = 1 ):
'''
calculates the denominator using the naivest
of integrations. No FENICS or anything - just
numpy and scipy
'''
ra = np.sqrt( x0*x0 + x1*x1 ) + 1e-13
kappara = kappa * ra
tmp = factor * kappara * sp.kv( 1.0, kappara ) * sp.kn( 0, kappara )
return 2.0 * np.sum(tmp) / n**2
n = 17 * 13 * 17
x0 = np.linspace( 0, 1.0, n, endpoint = False )
x1 = np.linspace( -.5, .5, n, endpoint = False )
X0, X1 = np.meshgrid( x0, x1 )
mesh_obj = helper.get_mesh( "square" , 50 )
alpha = 15.
cot = container.Container( "square",
mesh_obj,
alpha )
factor = cot.factor / 2. / math.pi
denom = denom2D( X0, X1, cot.kappa, n, factor = factor )
enum = np.array( enum2D( X0, X1, cot.kappa, n, factor = factor ) )
nx_beta = enum / denom
b = betas.Beta2DAdaptive( cot )
cb_beta = b(0.0,0.5)
b = betas.Beta2D( cot )
fe_beta = b(0.0,0.5)
b = betas.Beta2DRadial( cot )
rd_beta = b(0.0, 0.5)
print "Cubtur 2D: " + str( -cb_beta )
print "Numrix 2D: " + str( -nx_beta )
print "FEniCS 2D: " + str( -fe_beta )
print "Radial 2D: " + str( -rd_beta )
############################################
# Now test in 3D ###########################
############################################
if True:
print "3D tests!"
def enum3D( x0,x1,x2, kappa, n ):
'''
calculates the enumerator using the naivest
of integrations. No FENICS or anything - just
numpy and scipy. Returns a vector, because the
others also return a vector (which is supposed
to be dotted with the unit normal).
'''
ra = np.sqrt( x0*x0 + x1*x1 + x2*x2 ) + 1e-9
kappara = kappa * ra
Khalf = sp.kv( 0.5, kappara )
expon = np.exp( -kappara )
tot = -kappa * Khalf * expon * (2+1/kappara) * np.power( ra, -1.5 )
tmp0 = tot * x0
tmp1 = tot * x1
tmp2 = tot * x2
return ( np.sum(tmp0), np.sum(tmp1), np.sum(tmp2) )
def denom3D( x0,x1,x2, kappa, n ):
'''
calculates the denominator using the naivest
of integrations. No FENICS or anything - just
numpy and scipy
'''
ra = np.sqrt( x0*x0 + x1*x1 + x2*x2) + 1e-9
kappara = kappa * ra
Khalf = sp.kv( 0.5, kappara )
expon = np.exp( -kappara )
tmp = Khalf * expon / np.sqrt( ra )
return 2.0 * np.sum(tmp)
print "make the mesh and container..."
alpha = 25.0
mesh_obj = helper.get_mesh( "cube", 77 )
cot = container.Container( "cube",
mesh_obj,
alpha )
print "Mesh and container ready!!"
n = 555
x0 = np.linspace( 0, 1.0, n, endpoint = False )
x1 = np.linspace( -.5, .5, n, endpoint = False )
x2 = np.linspace( -.5, .5, n, endpoint = False )
X0, X1, X2 = np.meshgrid( x0, x1, x2 )
print "Grid ready!"
kappa = cot.kappa
print "Cubature..."
b = betas.BetaCubeAdaptive( cot )
start_time = time.time()
cb_beta_3d = b(0.0,0.5,0.5)
print "Run time: " + str( time.time() - start_time )
print "Naive integration..."
start_time = time.time()
nx_denom_3d = denom3D( X0, X1, X2, kappa, n )
nx_enum_3d = enum3D ( X0, X1, X2, kappa, n )
nx_beta_3d = np.array( nx_enum_3d ) / nx_denom_3d
print "Run time: " + str( time.time() - start_time )
print "FEniCS integration..."
b = betas.Beta3D( cot )
start_time = time.time()
fe_beta_3d = b(0.0, 0.5, 0.5 )
run = time.time() - start_time
print "Run time: " + str( run )
print "Radial integration..."
b = betas.Beta3DRadial( cot )
start_time = time.time()
rd_beta_3d = b(0.0, 0.5, 0.5 )
run = time.time() - start_time
print "Run time: " + str( run )
print "Cubtur 3D: " + str( -cb_beta_3d )
print "Numrix 3D: " + str( -nx_beta_3d )
print "FEniCS 3D: " + str( -fe_beta_3d )
print "Radial 3D: " + str( -rd_beta_3d )