4 gmls_python_installation_path = "@PYTHON_LIBRARY_PREFIX@"
5 sys.path.insert(0,gmls_python_installation_path)
9 # import other relevant models
19 # function used to generate sample data
20 def sinxsinysinz(coord):
21 return math.sin(coord[0])*math.sin(coord[1])*math.sin(coord[2])
23 # function used to get analytic gradient
24 def grad_sinxsinysinz(coord,component):
26 return math.cos(coord[0])*math.sin(coord[1])*math.sin(coord[2])
28 return math.sin(coord[0])*math.cos(coord[1])*math.sin(coord[2])
30 return math.sin(coord[0])*math.sin(coord[1])*math.cos(coord[2])
33 random.seed(1234) # for consistent results
36 manifoldPolyOrder = 2 # Not used
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")
45 nx, ny, nz = (ND, ND, ND)
54 dx = np.linspace(xmin, xmax, nx)
55 dy = np.linspace(ymin, ymax, ny)
56 dz = np.linspace(zmin, zmax, nz)
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')
73 t_sites.append([dx[i],dy[j],dz[k]])
74 source_sites = np.array(t_sites, dtype=np.dtype('d'))
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)
83 # used in combination with polynomial coefficients
84 epsilons = gmls_helper.getWindowSizes()
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)
91 # generate stencil with number of batches set to 1, and keeping coefficients (not necessary)
92 gmls_obj.generateAlphas(1, True)
95 # create sample data at source sites
98 data_vector.append(sinxsinysinz(source_sites[i]))
99 data_vector = np.array(data_vector, dtype=np.dtype('d'))
101 # apply stencil to sample data for all targets
102 computed_answer = gmls_helper.applyStencil(data_vector, pycompadre.TargetOperation.ScalarPointEvaluation)
106 l2_error += np.power(abs(computed_answer[i] - sinxsinysinz(target_sites[i])),2)
107 l2_error = math.sqrt(l2_error/float(NT))
109 # get polynomial coefficients
110 polynomial_coefficients = gmls_helper.getPolynomialCoefficients(data_vector)
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
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))
124 return (l2_error, h1_seminorm_error)
126 kokkos_obj=pycompadre.KokkosParser(sys.argv, True) # print state after initializing
128 # of points in each dimension to run the test over
131 # Calculate rates of convergence and print errors
132 print("\n" + colors.HEADER + "(l2) Errors:" + colors.ENDC)
135 errors.append(test1(nd))
136 print(str(errors[-1][0]))
138 print("\n" + colors.HEADER + "(l2) Rates:" + colors.ENDC)
139 for i in range(len(ND)-1):
140 print(str(math.log(errors[i+1][0]/errors[i][0])/math.log(0.5)))
143 # Calculate rates of convergence and print errors
144 print("\n" + colors.HEADER + "(h1-seminorm) Errors:" + colors.ENDC)
146 print(str(errors[-1][1]))
148 print("\n" + colors.HEADER + "(h1-seminorm) Rates:" + colors.ENDC)
149 for i in range(len(ND)-1):
150 print(str(math.log(errors[i+1][1]/errors[i][1])/math.log(0.5)))