-
Notifications
You must be signed in to change notification settings - Fork 0
/
2D_test.py
253 lines (184 loc) · 10.3 KB
/
2D_test.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
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
#!/bin/python -u
import numpy
import unittest
import matplotlib.pyplot as plt
import Globals.configPaths
import Optimization.DistanceFunction.DistanceFunctionOptimization
from Optimization.DistanceFunction import OptimizationMaker
import Writers.VTKMeshWriter
import Geometry.FrechetDistance
import Geometry.ProjectionDistance
from Geometry.Curve import Curve
from Geometry.Curve import Curve2DPol
from Geometry.Curve import Curve1DPol
from Geometry.Curve import LogSpiral
from Geometry.Curve import Circle
from Geometry.Curve import Segment
from Discretization.Meshers import CurveMesher,SurfaceMesher
from Writers.NumpyMeshWriter import NumpyMeshWriter
from Globals.configPython import *
import polynomial
import quadratures
class TestDistanceFunctionOptimization(unittest.TestCase):
@staticmethod
def getGeometry2D(c, a, b):
if c == 0: return Curve2DPol.Curve2DExp (a, b)
elif c == 1: return Curve2DPol.Curve2DSine(a, b)
elif c == 11:return Curve2DPol.Curve2DSineSine(a, b)
elif c == 2: return Curve2DPol.Curve2DPol2(a, b)
elif c == 3: return Curve2DPol.Curve2DPol3(a, b)
elif c == 6: return Curve1DPol.Curve2DPol6(a, b)
@staticmethod
def getMeshDistances(mesh, parametrization, functionName, tol, nOfSubdivisions, fixU = False):
disparityDistanceComputer=Geometry.FrechetDistance.FrechetDistance(
mesh,parametrization,
functionName)
if fixU:
oldParametricMask = mesh.theParametricNodesMask.copy()
mesh.theParametricNodesMask[:] = True
disparityDistanceComputer.theFTolRel=tol
disparityDistanceComputer.theXTolRel=tol
disparityValue,normalError=disparityDistanceComputer.run()
projectorDistance = Geometry.ProjectionDistance.ProjectionDistance(
mesh,parametrization,nOfSubdivisions)
projectorValue = projectorDistance.run()
if fixU:
mesh.theParametricNodesMask = oldParametricMask
return disparityValue, projectorValue, normalError
@staticmethod
def testDistanceFunction(pX, pU, ne, nR, curve, I, showPlots):
relocateX = False
fixU = False
callFix = True
method = 'Newton'
tolDistanceCalculation = 1.e-8
tol = 1.e-8
if curve == 0: inverseMap = True
else: inverseMap = False
nOfSubdivisions = 25
objectiveFunctionName = "Intrinsic"
frechetFunctionName = "Intrinsic"
parametrization = TestDistanceFunctionOptimization.getGeometry2D(curve, I[0], I[1])
disp_TI = numpy.zeros(nR + 1)
disp_TO = numpy.zeros(nR + 1)
disp_I = numpy.zeros(nR + 1)
disp_O = numpy.zeros(nR + 1)
for ref in range(nR + 1):
n = pow (2, ref) * ne
h = (I[1] - I[0]) / n
optimizer = Optimization.DistanceFunction.DistanceFunctionOptimization.DistanceFunctionOptimization(
parametrization,
h,pX,pU,
objectiveFunctionName,
tol,
initialP = pX,
method = method,
relocateX = relocateX,
fixU = fixU
)
meshO, meshI = optimizer.run()
disp,proj,norm = TestDistanceFunctionOptimization.getMeshDistances(
meshI,parametrization,frechetFunctionName,
tolDistanceCalculation, nOfSubdivisions)
disp_I[ref] = disp * disp * 0.5
disp,proj,norm = TestDistanceFunctionOptimization.getMeshDistances(
meshO,parametrization,frechetFunctionName,
tolDistanceCalculation, nOfSubdivisions)
disp_O[ref] = disp * disp * 0.5
w = meshO.theMasterElementX.theGaussWeights
z = meshO.theMasterElementX.theGaussPoints
for i in range(meshO.theNOfElements):
uO = meshO.getXElement(i)
tO = meshO.getUElement(i)
solO = parametrization.value(tO)
uI = meshI.getXElement(i)
tI = meshI.getUElement(i)
solI = parametrization.value(tI)
zO = 0.5 * (tO[-1] - tO[0]) * z + 0.5 * (tO[-1] + tO[0])
zI = 0.5 * (tI[-1] - tI[0]) * z + 0.5 * (tI[-1] + tI[0])
plot_tit = "Basis Degree " + str(pX)
if ref == nR:
fig = plt.figure(20)
if i == meshO.theNOfElements -1:
plt.suptitle(plot_tit)
plt.plot(solO[:,0], solO[:,1], c = 'c' , linestyle='-', linewidth=4, label='Exact tOpt')
plt.plot(uO[:,0], uO[:,1], c = 'red', linestyle='-', linewidth=3, label='Opti')
plt.plot(solI[:,0], solI[:,1], c = 'b' , linestyle='-.', linewidth=3, label='Exact Interp')
plt.plot(solI[:,0], solI[:,1], c = 'orange', linestyle='-.', linewidth=2, label='Interp')
plt.legend(loc = 'best')
else:
plt.plot(solO[:,0], solO[:,1], c = 'c' , linestyle='-', linewidth=4)
plt.plot( uO[:,0], uO[:,1], c = 'red', linestyle='-', linewidth=3)
plt.plot(solI[:,0], solI[:,1], c = 'b' , linestyle='-.', linewidth=3)
plt.plot( uI[:,0], uI[:,1], c = 'orange', linestyle='-.', linewidth=2)
fig = plt.figure(3)
plot_tit = "OPTIMIZED "
plt.suptitle(plot_tit)
distO = numpy.sqrt((solO[:,0] - uO[:,0]) * (solO[:,0] - uO[:,0]) + (solO[:,1] - uO[:,1]) * (solO[:,1] - uO[:,1]))
distI = numpy.sqrt((solI[:,0] - uI[:,0]) * (solI[:,0] - uI[:,0]) + (solI[:,1] - uI[:,1]) * (solI[:,1] - uI[:,1]))
print(" END INTERVALS X", solI[0,0], solI[-1,0], uI[0,0], uI[-1,0], " ERROR ", distI[0])
print(" END INTERVALS Y", solI[0,1], solI[-1,1], uI[0,1], uI[-1,1], " ERROR ", distI[-1])
plt.plot(tO, solO[:,0] - uO[:,0], c = 'b' , linestyle='-.', linewidth=3)
plt.plot(zO, solO[:,0] - uO[:,0], c = 'c' , linestyle='-', linewidth=3)
plt.plot(tO, solO[:,1] - uO[:,1], c = 'orange' , linestyle='-.', linewidth=3)
plt.plot(zO, solO[:,1] - uO[:,1], c = 'r' , linestyle='-', linewidth=3)
plt.plot(tO, distO, c = 'g' , linestyle='--', linewidth=3)
plt.scatter(tO[0], 0, c = 'b', linewidth=4)
plt.scatter(tO[-1], 0, c = 'b', linewidth=4)
plt.scatter(zO[0], 0, c = 'r')
plt.scatter(zO[-1], 0, c = 'r')
fig = plt.figure(4)
plot_tit = "INTERPOL "
plt.suptitle(plot_tit)
plt.plot(tI, solI[:,0] - uI[:,0], c = 'b' , linestyle='-.', linewidth=3)
plt.plot(zI, solI[:,0] - uI[:,0], c = 'c' , linestyle='-', linewidth=3)
plt.plot(tI, solI[:,1] - uI[:,1], c = 'orange' , linestyle='-.', linewidth=3)
plt.plot(zI, solI[:,1] - uI[:,1], c = 'r' , linestyle='-', linewidth=3)
plt.plot(tI, distI, c = 'g' , linestyle='--', linewidth=3)
plt.scatter(tI[0], 0, c = 'b', linewidth=4)
plt.scatter(tI[-1], 0, c = 'b', linewidth=4)
plt.scatter(zI[0], 0, c = 'r')
plt.scatter(zI[-1], 0, c = 'r')
print("------------------------------------------------------------------------")
print("----------------------- POLYNOMIAL DEGREES: X ",pX," T ",pU," ----------------")
print("------------------------------------------------------------------------\n")
print("\n \t|| x_p^* - alpha t_q || ==> expect 2p =", 2 * pX,"\n\n")
print ("N\t E(x,t)\t ORDER sqrt(E) ORDER")
print("------------------------------------------------------------------------\n")
for r in range(nR + 1):
ne1 = pow(2, r) * ne
if r == 0:
print (ne1,"\t%1.3e"%disp_O[r]," |","%1.3e"%numpy.sqrt(disp_O[r]))
else:
a = numpy.log10( disp_O[r-1] / disp_O[r]) / numpy.log10(2.0)
b = numpy.log10(numpy.sqrt(disp_O[r-1])/ numpy.sqrt(disp_O[r])) / numpy.log10(2.0)
print (ne1,"\t%1.3e"%disp_O[r]," %1.2f"%a, " | %1.3e"%numpy.sqrt(disp_O[r])," %1.2f" %b)
print("____________________________________________________________________\n")
print("\n \t|| I_p^* - alpha t_q || ==> expect max(p,q) + 1 =", max(pX, pU) + 1,"\n\n")
print ("N\t E(x,t)\t ORDER sqrt(E) ORDER")
print("------------------------------------------------------------------------\n")
for r in range(nR + 1):
ne1 = pow(2, r) * ne
if r == 0:
print (ne1,"\t%1.3e"%disp_I[r]," |","%1.3e"%numpy.sqrt(disp_I[r]))
else:
a = numpy.log10( disp_I[r-1] / disp_I[r]) / numpy.log10(2.0)
b = numpy.log10(numpy.sqrt(disp_I[r-1])/ numpy.sqrt(disp_I[r])) / numpy.log10(2.0)
print (ne1,"\t%1.3e"%disp_I[r]," %1.2f"%a, " | %1.3e"%numpy.sqrt(disp_I[r])," %1.2f" %b)
print("____________________________________________________________________\n")
if (showPlots == True): plt.show()
if __name__ == '__main__':
argc = len(sys.argv)
if argc != 7:
print (" I NEED DEGREEX + degree T + INITIAL ELEMENTS + REFINEMENTS + CURVE TYPE")
print(sys.argv)
quit(1)
degX = int(sys.argv[1]) # number of elements
degT = int(sys.argv[2]) # number of elements
elmts = int(sys.argv[3]) # number of elements
refine = int(sys.argv[4]) # number of elements
curve = int(sys.argv[5]) # number of elements
showPlots = int(sys.argv[6]) # number of elements
if ( curve == 0): I = [0, numpy.pi * 0.5]
else: I = [0.25, 1]
TestDistanceFunctionOptimization.testDistanceFunction(degX, degT, elmts, refine, curve, I, showPlots)