def main(parallelEnv): comm = MPI.COMM_WORLD myGlobalRank = comm.rank # Create an Albany problem: filename = "input_dirichletT.yaml" parameter = Utils.createParameterList( filename, parallelEnv ) problem = Utils.createAlbanyProblem(parameter, parallelEnv) parameter_map_0 = problem.getParameterMap(0) parameter_0 = Tpetra.Vector(parameter_map_0, dtype="d") N = 200 p_min = -2. p_max = 2. # Generate N samples randomly chosen in [p_min, p_max]: p = np.random.uniform(p_min, p_max, N) QoI = np.zeros((N,)) # Loop over the N samples and evaluate the quantity of interest: for i in range(0, N): parameter_0[0] = p[i] problem.setParameter(0, parameter_0) problem.performSolve() response = problem.getResponse(0) QoI[i] = response.getData()[0] if myGlobalRank == 0: if printPlot: f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(16,6)) ax1.hist(p) ax1.set_ylabel('Counts') ax1.set_xlabel('Random parameter') ax2.scatter(p, QoI) ax2.set_ylabel('Quantity of interest') ax2.set_xlabel('Random parameter') ax3.hist(QoI) ax3.set_ylabel('Counts') ax3.set_xlabel('Quantity of interest') plt.savefig('UQ.jpeg', dpi=800) plt.close()
def test_all(self): cls = self.__class__ rank = cls.comm.getRank() file_dir = os.path.dirname(__file__) # Create an Albany problem: filename = "input_dirichlet_mixed_paramsT.yaml" parameter = Utils.createParameterList(file_dir + "/" + filename, cls.parallelEnv) parameter.sublist("Discretization").set("1D Elements", 10) parameter.sublist("Discretization").set("2D Elements", 10) problem = Utils.createAlbanyProblem(parameter, cls.parallelEnv) parameter_map_0 = problem.getParameterMap(0) para_0_new = Tpetra.Vector(parameter_map_0, dtype="d") parameter_map_1 = problem.getParameterMap(1) para_1_new = Tpetra.Vector(parameter_map_1, dtype="d") para_1_new[:] = 0.333333 n_values = 5 para_0_values = np.linspace(-1, 1, n_values) responses = np.zeros((n_values, )) responses_target = np.array( [0.69247527, 0.48990929, 0.35681844, 0.29320271, 0.2990621]) tol = 1e-8 for i in range(0, n_values): para_0_new[:] = para_0_values[i] problem.setParameter(0, para_0_new) problem.performSolve() response = problem.getResponse(0) responses[i] = response.getData()[0] print("p = " + str(para_0_values)) print("QoI = " + str(responses)) if rank == 0: self.assertTrue( np.abs(np.amax(responses - responses_target)) < tol)
def test_all(self): debug = True cls = self.__class__ myGlobalRank = cls.comm.getRank() nproc = cls.comm.getSize() # Create an Albany problem: n_params = 2 filename = "thermal_steady_hessian.yaml" parameter = Utils.createParameterList( filename, cls.parallelEnv ) problem = Utils.createAlbanyProblem(parameter, cls.parallelEnv) # ---------------------------------------------- # # 1. Evaluation of the theta star # # ---------------------------------------------- l_min = 1. l_max = 2. n_l = 4 p = 0.25 l = l_min + np.power(np.linspace(0.0, 1.0, n_l), p) * (l_max-l_min) theta_star, I_star, F_star, P_star = ee.evaluateThetaStar(l, problem, n_params) # ---------------------------------------------- # # 2. Evaluation of the prefactor using SO # # ---------------------------------------------- mean = np.array([1., 1.]) cov = np.array([[1., 0.], [0., 1.]]) P_SO = ee.secondOrderEstimator(mean, cov, l, theta_star, I_star, F_star, P_star, problem) if myGlobalRank == 0: expected_theta_star = np.loadtxt('expected_theta_star_steady_hessian_'+str(nproc)+'.txt') expected_I_star = np.loadtxt('expected_I_star_steady_hessian_'+str(nproc)+'.txt') expected_P_star = np.loadtxt('expected_P_star_steady_hessian_'+str(nproc)+'.txt') expected_F_star = np.loadtxt('expected_F_star_steady_hessian_'+str(nproc)+'.txt') expected_P_SO = np.loadtxt('expected_P_steady_hessian_SO_'+str(nproc)+'.txt') tol = 1e-6 if debug: for i in range(0, len(expected_theta_star)): print('i = ' + str(i) + ': theta star: expected value = ' + str(expected_theta_star[i]) + ', computed value = ' + str(theta_star[i]) + ', and diff = ' + str(expected_theta_star[i]-theta_star[i])) print('i = ' + str(i) + ': I star: expected value = ' + str(expected_I_star[i]) + ', computed value = ' + str(I_star[i]) + ', and diff = ' + str(expected_I_star[i]-I_star[i])) print('i = ' + str(i) + ': P star: expected value = ' + str(expected_P_star[i]) + ', computed value = ' + str(P_star[i]) + ', and diff = ' + str(expected_P_star[i]-P_star[i])) print('i = ' + str(i) + ': F star: expected value = ' + str(expected_F_star[i]) + ', computed value = ' + str(F_star[i]) + ', and diff = ' + str(expected_F_star[i]-F_star[i])) print('i = ' + str(i) + ': P SO: expected value = ' + str(expected_P_SO[i]) + ', computed value = ' + str(P_SO[i]) + ', and diff = ' + str(expected_P_SO[i]-P_SO[i])) self.assertTrue(np.amax(np.abs(expected_theta_star - theta_star)) < tol) self.assertTrue(np.amax(np.abs(expected_I_star - I_star)) < tol) self.assertTrue(np.amax(np.abs(expected_P_star - P_star)) < tol) self.assertTrue(np.amax(np.abs(expected_F_star - F_star)) < tol) self.assertTrue(np.amax(np.abs(expected_P_SO - P_SO)) < tol)
def test_all(self): debug = True cls = self.__class__ myGlobalRank = cls.comm.getRank() nproc = cls.comm.getSize() # Create an Albany problem: n_params = 2 filename = "thermal_steady.yaml" parameter = Utils.createParameterList( filename, cls.parallelEnv ) problem = Utils.createAlbanyProblem(parameter, cls.parallelEnv) # ---------------------------------------------- # # 1. Evaluation of the theta star # # ---------------------------------------------- l_min = 0. l_max = 2. n_l = 3 l = np.linspace(l_min, l_max, n_l) theta_star, I_star, F_star, P_star = ee.evaluateThetaStar(l, problem, n_params) # ---------------------------------------------- # # 2. Evaluation of the prefactor using IS # # ---------------------------------------------- N_samples = 10 mean = np.array([1., 1.]) cov = np.array([[1., 0.], [0., 1.]]) np.random.seed(41) samples = np.random.multivariate_normal(mean, cov, N_samples) angle_1 = 0.49999*np.pi angle_2 = np.pi - angle_1 P_IS = ee.importanceSamplingEstimator(mean, cov, theta_star, F_star, P_star, samples, problem) P_mixed = ee.mixedImportanceSamplingEstimator(mean, cov, theta_star, F_star, P_star, samples, problem, angle_1, angle_2) if myGlobalRank == 0: expected_theta_star = np.loadtxt('expected_theta_star_steady_'+str(nproc)+'.txt') expected_I_star = np.loadtxt('expected_I_star_steady_'+str(nproc)+'.txt') expected_P_star = np.loadtxt('expected_P_star_steady_'+str(nproc)+'.txt') expected_F_star = np.loadtxt('expected_F_star_steady_'+str(nproc)+'.txt') expected_P_IS = np.loadtxt('expected_P_steady_IS_'+str(nproc)+'.txt') expected_P_mixed = np.loadtxt('expected_P_steady_mixed_'+str(nproc)+'.txt') tol = 1e-8 tol_F = 5e-5 if debug: for i in range(0, len(expected_theta_star)): print('i = ' + str(i) + ': theta star: expected value = ' + str(expected_theta_star[i]) + ', computed value = ' + str(theta_star[i]) + ', and diff = ' + str(expected_theta_star[i]-theta_star[i])) print('i = ' + str(i) + ': I star: expected value = ' + str(expected_I_star[i]) + ', computed value = ' + str(I_star[i]) + ', and diff = ' + str(expected_I_star[i]-I_star[i])) print('i = ' + str(i) + ': P star: expected value = ' + str(expected_P_star[i]) + ', computed value = ' + str(P_star[i]) + ', and diff = ' + str(expected_P_star[i]-P_star[i])) print('i = ' + str(i) + ': F star: expected value = ' + str(expected_F_star[i]) + ', computed value = ' + str(F_star[i]) + ', and diff = ' + str(expected_F_star[i]-F_star[i])) print('i = ' + str(i) + ': P IS: expected value = ' + str(expected_P_IS[i]) + ', computed value = ' + str(P_IS[i]) + ', and diff = ' + str(expected_P_IS[i]-P_IS[i])) print('i = ' + str(i) + ': P mixed: expected value = ' + str(expected_P_mixed[i]) + ', computed value = ' + str(P_mixed[i]) + ', and diff = ' + str(expected_P_mixed[i]-P_mixed[i])) self.assertTrue(np.amax(np.abs(expected_theta_star - theta_star)) < tol) self.assertTrue(np.amax(np.abs(expected_I_star - I_star)) < tol) self.assertTrue(np.amax(np.abs(expected_P_star - P_star)) < tol) self.assertTrue(np.amax(np.abs(expected_F_star - F_star)) < tol_F) self.assertTrue(np.amax(np.abs(expected_P_IS - P_IS)) < tol) self.assertTrue(np.amax(np.abs(expected_P_mixed - P_mixed)) < tol)
def main(parallelEnv): comm = MPI.COMM_WORLD myGlobalRank = comm.rank # Create an Albany problem: n_params = 2 filename = "thermal_steady_2.yaml" parameter = Utils.createParameterList(filename, parallelEnv) problem = Utils.createAlbanyProblem(parameter, parallelEnv) # ---------------------------------------------- # # 1. Evaluation of the theta star # # ---------------------------------------------- l_min = 8. l_max = 20. n_l = 5 p = 1. l = l_min + np.power(np.linspace(0.0, 1.0, n_l), p) * (l_max - l_min) theta_star, I_star, F_star, P_star = ee.evaluateThetaStar( l, problem, n_params) np.savetxt('theta_star_steady_2.txt', theta_star) np.savetxt('I_star_steady_2.txt', I_star) np.savetxt('P_star_steady_2.txt', P_star) np.savetxt('F_star_steady_2.txt', F_star) # ---------------------------------------------- # # 2. Evaluation of the prefactor using IS # # ---------------------------------------------- N_samples = 100 mean = np.array([1., 1.]) cov = np.array([[1., 0.], [0., 1.]]) samples = np.random.multivariate_normal(mean, cov, N_samples) angle_1 = 0.49999 * np.pi angle_2 = np.pi - angle_1 P_IS = ee.importanceSamplingEstimator(mean, cov, theta_star, F_star, P_star, samples, problem) P_mixed = ee.mixedImportanceSamplingEstimator(mean, cov, theta_star, F_star, P_star, samples, problem, angle_1, angle_2) P_SO = ee.secondOrderEstimator(mean, cov, l, theta_star, I_star, F_star, P_star, problem) np.savetxt('P_steady_IS_2.txt', P_IS) np.savetxt('P_steady_mixed_2.txt', P_mixed) np.savetxt('P_steady_SO_2.txt', P_SO) problem.reportTimers() # ---------------------------------------------- # # 3. Plots # # ---------------------------------------------- if n_params == 2: X = np.arange(1, 7, 0.2) Y = np.arange(1, 7, 0.25) Z1, Z2 = evaluate_responses(X, Y, problem, True) X, Y = np.meshgrid(X, Y) if myGlobalRank == 0: if printPlot: plt.figure() plt.semilogy(F_star, P_star, 'k*-') plt.semilogy(F_star, P_IS, 'b*-') plt.semilogy(F_star, P_mixed, 'r*--') plt.semilogy(F_star, P_SO, 'g*-') plt.savefig('extreme_steady_2.jpeg', dpi=800) plt.close() if n_params == 2: plt.figure() plt.plot(theta_star[:, 0], theta_star[:, 1], '*-') plt.contour(X, Y, Z1, levels=I_star, colors='g') plt.contour(X, Y, Z2, levels=F_star, colors='r') plt.savefig('theta_star_2.jpeg', dpi=800) plt.close() fig = plt.figure() ax = fig.gca(projection='3d') ax.plot_surface(X, Y, Z1) plt.savefig('Z1_2.jpeg', dpi=800) plt.close() fig = plt.figure() ax = fig.gca(projection='3d') ax.plot_surface(X, Y, Z2) plt.savefig('Z2_2.jpeg', dpi=800) plt.close()
def test_all(self): cls = self.__class__ rank = cls.comm.getRank() file_dir = os.path.dirname(__file__) # Create an Albany problem: filename = "input_dirichlet_mixed_paramsT.yaml" parameter = Utils.createParameterList( file_dir + "/" + filename, cls.parallelEnv ) parameter.sublist("Discretization").set("1D Elements", 10) parameter.sublist("Discretization").set("2D Elements", 10) problem = Utils.createAlbanyProblem(parameter, cls.parallelEnv) g_target_before = 0.35681844 g_target_after = 0.17388298 g_target_2 = 0.19570272 p_0_target = 0.39886689 p_1_norm_target = 5.37319376038225 tol = 1e-8 problem.performSolve() response_before_analysis = problem.getResponse(0) problem.performAnalysis() para_0 = problem.getParameter(0) para_1 = problem.getParameter(1) print(para_0.getData()) print(para_1.getData()) para_1_norm = Utils.norm(para_1.getData(), cls.comm) print(para_1_norm) if rank == 0: self.assertTrue(np.abs(para_0[0] - p_0_target) < tol) self.assertTrue(np.abs(para_1_norm - p_1_norm_target) < tol) problem.performSolve() response_after_analysis = problem.getResponse(0) print("Response before analysis " + str(response_before_analysis.getData())) print("Response after analysis " + str(response_after_analysis.getData())) if rank == 0: self.assertTrue(np.abs(response_before_analysis[0] - g_target_before) < tol) self.assertTrue(np.abs(response_after_analysis[0] - g_target_after) < tol) parameter_map_0 = problem.getParameterMap(0) para_0_new = Tpetra.Vector(parameter_map_0, dtype="d") para_0_new[:] = 0.0 problem.setParameter(0, para_0_new) problem.performSolve() response = problem.getResponse(0) print("Response after setParameter " + str(response.getData())) if rank == 0: self.assertTrue(np.abs(response[0] - g_target_2) < tol)