Compadre 1.7.0
Loading...
Searching...
No Matches
Python_3D_Convergence.py.in
Go to the documentation of this file.
1import sys
2
3# installation folder
4gmls_python_installation_path = "@PYTHON_LIBRARY_PREFIX@"
5sys.path.insert(0,gmls_python_installation_path)
6
7import pycompadre
8
9# import other relevant models
10import numpy as np
11import math
12import random
13
14# just for formatting
15class colors:
16 HEADER = '\033[95m'
17 ENDC = '\033[0m'
18
19# function used to generate sample data
20def sinxsinysinz(coord):
21 return math.sin(coord[0])*math.sin(coord[1])*math.sin(coord[2])
22
23# function used to get analytic gradient
24def grad_sinxsinysinz(coord,component):
25 if (component==0):
26 return math.cos(coord[0])*math.sin(coord[1])*math.sin(coord[2])
27 elif (component==1):
28 return math.sin(coord[0])*math.cos(coord[1])*math.sin(coord[2])
29 elif (component==2):
30 return math.sin(coord[0])*math.sin(coord[1])*math.cos(coord[2])
31
32def test1(ND):
33 random.seed(1234) # for consistent results
34
35 polyOrder = 2
36 manifoldPolyOrder = 2 # Not used
37 dimensions = 3
38
39 # initialize 3rd order reconstruction using 2nd order basis in 3D (GMLS)
40 gmls_obj=pycompadre.GMLS(polyOrder, dimensions, "QR", "STANDARD")
41 gmls_obj.setWeightingParameter(10)
42 gmls_obj.setWeightingType("power")
43
44 NT = 100 # Targets
45 nx, ny, nz = (ND, ND, ND)
46
47 xmax = 1
48 ymax = 1
49 zmax = 1
50 xmin = -xmax
51 ymin = -ymax
52 zmin = -zmax
53
54 dx = np.linspace(xmin, xmax, nx)
55 dy = np.linspace(ymin, ymax, ny)
56 dz = np.linspace(zmin, zmax, nz)
57
58 N=ND*ND*ND
59
60
61 # target sites
62 target_sites = []
63 for i in range(NT):
64 target_sites.append([random.uniform(xmin, xmax), random.uniform(ymin, ymax), random.uniform(zmin, zmax)])
65 target_sites = np.array(target_sites, dtype='d')
66
67
68 # source sites
69 t_sites = list()
70 for i in range(ND):
71 for j in range(ND):
72 for k in range(ND):
73 t_sites.append([dx[i],dy[j],dz[k]])
74 source_sites = np.array(t_sites, dtype=np.dtype('d'))
75
76
77 # neighbor search
78 epsilon_multiplier = 1.5
79 gmls_helper = pycompadre.ParticleHelper(gmls_obj)
80 gmls_helper.generateKDTree(source_sites)
81 gmls_helper.generateNeighborListsFromKNNSearchAndSet(target_sites, polyOrder, dimensions, epsilon_multiplier)
82
83 # used in combination with polynomial coefficients
84 epsilons = gmls_helper.getWindowSizes()
85
86 gmls_obj.addTargets(pycompadre.TargetOperation.ScalarPointEvaluation)
87 gmls_obj.addTargets(pycompadre.TargetOperation.PartialXOfScalarPointEvaluation)
88 (dimensions>1) and gmls_obj.addTargets(pycompadre.TargetOperation.PartialYOfScalarPointEvaluation)
89 (dimensions>2) and gmls_obj.addTargets(pycompadre.TargetOperation.PartialYOfScalarPointEvaluation)
90
91 # generate stencil with number of batches set to 1, and keeping coefficients (not necessary)
92 gmls_obj.generateAlphas(1, True)
93
94
95 # create sample data at source sites
96 data_vector = []
97 for i in range(N):
98 data_vector.append(sinxsinysinz(source_sites[i]))
99 data_vector = np.array(data_vector, dtype=np.dtype('d'))
100
101 # apply stencil to sample data for all targets
102 computed_answer = gmls_helper.applyStencil(data_vector, pycompadre.TargetOperation.ScalarPointEvaluation)
103
104 l2_error = 0
105 for i in range(NT):
106 l2_error += np.power(abs(computed_answer[i] - sinxsinysinz(target_sites[i])),2)
107 l2_error = math.sqrt(l2_error/float(NT))
108
109 # get polynomial coefficients
110 polynomial_coefficients = gmls_helper.getPolynomialCoefficients(data_vector)
111
112 # alternative way to compute h1 semi norm
113 # could remap using the gradient operator, but instead this uses calculated polynomial coefficients and applies
114 # the action of the gradient target operation on the polynomial basis at the target sites
115 # this serves as a test for getting accurate calculation and retrieval of polynomial coefficients using
116 # the python interface
117 h1_seminorm_error = 0
118 for i in range(NT):
119 h1_seminorm_error += np.power(abs(1./epsilons[i]*polynomial_coefficients[i,1] - grad_sinxsinysinz(target_sites[i],0)),2)
120 h1_seminorm_error += np.power(abs(1./epsilons[i]*polynomial_coefficients[i,2] - grad_sinxsinysinz(target_sites[i],1)),2)
121 h1_seminorm_error += np.power(abs(1./epsilons[i]*polynomial_coefficients[i,3] - grad_sinxsinysinz(target_sites[i],2)),2)
122 h1_seminorm_error = math.sqrt(h1_seminorm_error/float(NT))
123
124 return (l2_error, h1_seminorm_error)
125
126kokkos_obj=pycompadre.KokkosParser(sys.argv, True) # print state after initializing
127
128# of points in each dimension to run the test over
129ND = [5, 10, 20, 40]
130
131# Calculate rates of convergence and print errors
132print("\n" + colors.HEADER + "(l2) Errors:" + colors.ENDC)
133errors = list()
134for nd in ND:
135 errors.append(test1(nd))
136 print(str(errors[-1][0]))
137
138print("\n" + colors.HEADER + "(l2) Rates:" + colors.ENDC)
139for i in range(len(ND)-1):
140 print(str(math.log(errors[i+1][0]/errors[i][0])/math.log(0.5)))
141print("\n")
142
143# Calculate rates of convergence and print errors
144print("\n" + colors.HEADER + "(h1-seminorm) Errors:" + colors.ENDC)
145for nd in ND:
146 print(str(errors[-1][1]))
147
148print("\n" + colors.HEADER + "(h1-seminorm) Rates:" + colors.ENDC)
149for i in range(len(ND)-1):
150 print(str(math.log(errors[i+1][1]/errors[i][1])/math.log(0.5)))
151print("\n")