### Install Dependencies Source: https://github.com/aurion-polito/pypolydim/blob/main/README.md Installs all required Python packages listed in the 'requirements.txt' file. This command should be run after activating the virtual environment. ```bash pip install -r requirements.txt ``` -------------------------------- ### Activate Python Virtual Environment Source: https://github.com/aurion-polito/pypolydim/blob/main/README.md Activates the previously created Python virtual environment. This ensures that subsequent commands use the environment's isolated Python installation and packages. ```bash source .venv/bin/activate ``` -------------------------------- ### Initialize Mesh and DOFs Data Structures Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Loads mesh connectivity data, creates reference elements for trial and test spaces, and initializes a DOFs manager. It then sets up mesh DOFs information and creates DOFs data for both trial and test spaces. ```python mesh_connectivity_data = polydim.pde_tools.mesh.MeshMatricesDAO_mesh_connectivity_data(mesh) trial_reference_element_data = polydim.pde_tools.local_space_pcc_2_d.create_reference_element(method_type, method_order) test_reference_element_data = polydim.pde_tools.local_space_pcc_2_d.create_reference_element(method_type, method_order) dof_manager = polydim.pde_tools.do_fs.DOFsManager() trial_mesh_dofs_info = polydim.pde_tools.local_space_pcc_2_d.set_mesh_do_fs_info(trial_reference_element_data, mesh, boundary_info) trial_dofs_data = dof_manager.create_do_fs_2_d(trial_mesh_dofs_info, mesh_connectivity_data) test_mesh_dofs_info = polydim.pde_tools.local_space_pcc_2_d.set_mesh_do_fs_info(trial_reference_element_data, mesh, boundary_info) test_dofs_data = dof_manager.create_do_fs_2_d(test_mesh_dofs_info, mesh_connectivity_data) ``` -------------------------------- ### Create Snapshot Matrix Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Creates a snapshot matrix by solving a system for each parameter set in the training data. Uses sparse linear system solver. ```python #### snapshot matrix creation thetaA1 = 1. snapshot_matrix = [] tol = 1. - 1e-7 N_max = 10 for mu in training_set: thetaA2 = mu[0] thetaf1 = mu[1] A = thetaA1 * A_1 + thetaA2 * A_2 f = thetaf1 * weak_term snapshot = scipy.sparse.linalg.spsolve(A, f) snapshot_matrix.append(np.copy(snapshot)) snapshot_matrix = np.array(snapshot_matrix) print(snapshot_matrix.shape) ``` -------------------------------- ### Setting Up Export Folders and Mesh Parameters Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Configures the export paths for simulation results and mesh files, creating directories if they do not exist. Defines mesh type, method type, import folder, and method order for mesh processing. ```python # Export folder export_file_path = "./Export/ROM_POD" if not os.path.exists(export_file_path): os.makedirs(export_file_path) # Mesh file path export_mesh_path = export_file_path + "/Mesh" if not os.path.exists(export_mesh_path): os.makedirs(export_mesh_path) # Solution file path export_solution_path = export_file_path + "/Solution" if not os.path.exists(export_solution_path): os.makedirs(export_solution_path) mesh_type = polydim.pde_tools.mesh.pde_mesh_utilities.MeshGenerator_Types_2D.triangular_simple_importer method_type = polydim.pde_tools.local_space_pcc_2_d.MethodTypes.fem_pcc import_mesh_folder = "./ROM_POD/Mesh3" method_order = 1 ``` -------------------------------- ### Initializing Geometry and Mesh Utilities Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Initializes configuration and utility objects for geometry and mesh processing. Sets tolerances for 2D geometry calculations. ```python geometry_utilities_config = gedim.GeometryUtilitiesConfig() geometry_utilities_config.tolerance1_d = 1.0e-6 geometry_utilities_config.tolerance2_d = 1.0e-12 geometry_utilities = gedim.GeometryUtilities(geometry_utilities_config) mesh_utilities = gedim.MeshUtilities() vtk_utilities = ExportVTKUtilities() ``` -------------------------------- ### Assembling Reduced Linear System (Offline Phase) Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Assembles the reduced matrices A1, A2, and the reduced vector 'weak_term' for the offline phase of the ROM. These are used to form the reduced linear system. ```python ########## ASSEMBLE THE LINEAR SYSTEM ##### STILL OFFLINE reduced_A1 = np.transpose(basis_functions) @ A_1 @ basis_functions reduced_A2 = np.transpose(basis_functions) @ A_2 @ basis_functions reduced_f = np.transpose(basis_functions) @ weak_term ### shape? ##### ``` -------------------------------- ### Importing Libraries for POD Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Imports necessary libraries for performing Proper Orthogonal Decomposition, including numerical computation, argument parsing, file system operations, sparse matrices, VTK, and PyPolyDim modules. ```python import numpy as np import argparse import os.path import scipy.sparse import vtk from pypolydim import polydim, gedim from pypolydim.export_vtk_utilities import ExportVTKUtilities from pypolydim.assembler_utilities import assembler_utilities import sys sys.path.insert(1, './ROM_POD') import ROM_POD.other_utilities as other_ut ``` -------------------------------- ### Solving Reduced Linear System (Online Phase) Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Solves the reduced linear system for a new parameter in the online phase. It combines the pre-assembled reduced matrices and vectors based on parameter values and then solves for the reduced solution. ```python thetaA2 = 2. thetaf1 = 0.8 reduced_rhs = thetaA1*reduced_A1 + thetaA2*reduced_A2 reduced_lhs = thetaf1*reduced_f ##### solve ######### reduced_solution = np.linalg.solve(reduced_rhs, reduced_lhs) proj_reduced_solution = basis_functions @ reduced_solution ``` -------------------------------- ### Constructing Basis Functions for ROM Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Constructs a set of basis functions using the eigenvectors obtained from the eigenvalue decomposition. Each basis function is normalized using an inner product matrix. ```python # Create the basis function matrix basis_functions = [] for n in range(N): eigenvector = eigenvectors[n] # basis = (1/np.sqrt(snapshot_num))*np.transpose(snapshot_matrix)@eigenvector (This is the one of the book!!) basis = np.transpose(snapshot_matrix)@eigenvector norm = np.sqrt(np.transpose(basis) @ inner_product @ basis) basis /= norm basis_functions.append(np.copy(basis)) basis_functions = np.transpose(np.array(basis_functions)) ``` -------------------------------- ### Define Training Set for Parameters Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Generates a random uniform training set for model parameters. Sets the number of snapshots and parameter ranges. ```python ### define the training set snapshot_num = 100 # (M) mu1_range = [0.1, 10.] mu2_range = [-1., 1.] P = np.array([mu1_range, mu2_range]) training_set = np.random.uniform(low=P[:, 0], high=P[:, 1], size=(snapshot_num, P.shape[0])) ``` -------------------------------- ### Assemble Reaction Operator Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Assembles the reaction operator for a 2D problem. Requires geometry, mesh, and dofs data. ```python def inner_react(x, y, z): return 1.0 A_l_2 = polydim.pde_tools.assembler_utilities.pcc_2_d.assemble_reaction_operator(geometry_utilities, mesh, mesh_geometric_data, trial_dofs_data, test_dofs_data, trial_reference_element_data, test_reference_element_data, inner_react) A_L2 = other_ut.make_np_sparse(A_l_2.operator_dofs) ``` -------------------------------- ### Create Python Virtual Environment Source: https://github.com/aurion-polito/pypolydim/blob/main/README.md Creates a new Python virtual environment named '.venv'. This is a standard practice for managing project dependencies. ```bash python3 -m venv .venv ``` -------------------------------- ### Solving Full-Order Model (FOM) Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Solves the full-order model's linear system using sparse matrix solver from SciPy. This is used for comparison with the ROM solution. ```python A = thetaA1*A_1 + thetaA2*A_2 f = thetaf1*weak_term full_solution = scipy.sparse.linalg.spsolve(A, f) ``` -------------------------------- ### Exporting and Plotting Full-Order Solution Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Extracts the full-order solution onto the cell0Ds of the mesh and exports it to a VTK file. It also plots the full-order solution for comparison. ```python proj_full_on_cell0Ds = polydim.pde_tools.assembler_utilities.pcc_2_d.extract_solution_on_cell0_ds(mesh, trial_dofs_data, full_solution, u_D) vtk_utilities.export_solution_2(export_solution_path + '/u_full', mesh, proj_full_on_cell0Ds.numeric_solution) other_ut.plot_solution(mesh, proj_full_on_cell0Ds.numeric_solution) ``` -------------------------------- ### Assemble Strong Solution Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Assembles the strong form of the solution for a 2D PDE. Used to define boundary conditions or specific solution values. ```python def strong_solution_function(marker, x, y, z): return 0.0 u_D = polydim.pde_tools.assembler_utilities.pcc_2_d.assemble_strong_solution(geometry_utilities, mesh, mesh_geometric_data, trial_mesh_dofs_info, trial_dofs_data, trial_reference_element_data, strong_solution_function) ``` -------------------------------- ### Compute Error and Speedup Analysis Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb This snippet iterates through a testing set to compute the full-order model (FOM) and reduced-order model (ROM) solutions. It calculates the time taken for each, the speedup achieved by the ROM, and the absolute and relative errors between the solutions. Requires numpy, scipy.sparse.linalg, and time modules. ```python import time abs_err = [] rel_err = [] testing_set = np.random.uniform(low=P[:, 0], high=P[:, 1], size=(100, P.shape[0])) speed_up = [] print("Computing error and speedup analysis") for mu in testing_set: thetaA2 = mu[0] thetaf1 = mu[1] ##### full ##### A = thetaA1*A_1 + thetaA2*A_2 f = thetaf1*weak_term start_fom = time.time() full_solution = scipy.sparse.linalg.spsolve(A, f) time_fom = time.time() - start_fom #### reduced ##### reduced_rhs = thetaA1*reduced_A1 + thetaA2*reduced_A2 reduced_lhs = thetaf1*reduced_f start_rom = time.time() reduced_solution = np.linalg.solve(reduced_rhs, reduced_lhs) time_rom = time.time() - start_rom speed_up.append(time_fom/time_rom) proj_reduced_solution = basis_functions@reduced_solution ### computing error error_function = full_solution - proj_reduced_solution error_norm_squared_component = np.transpose(error_function) @ inner_product @ error_function absolute_error = np.sqrt(abs(error_norm_squared_component)) abs_err.append(absolute_error) full_solution_norm_squared_component = np.transpose(full_solution) @ inner_product @ full_solution relative_error = absolute_error/np.sqrt(abs(full_solution_norm_squared_component)) rel_err.append(relative_error) print("average relative error = ", np.mean(rel_err) ) print("average absolute error = ", np.mean(abs_err) ) print("average speed_up = ", np.mean(speed_up) ) ``` -------------------------------- ### Exporting and Plotting Reduced Solution Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Extracts the reduced solution onto the cell0Ds of the mesh and exports it to a VTK file. It also plots the projected reduced solution. ```python proj_reduced_on_cell0Ds = polydim.pde_tools.assembler_utilities.pcc_2_d.extract_solution_on_cell0_ds(mesh, trial_dofs_data, proj_reduced_solution, u_D) vtk_utilities.export_solution_2(export_solution_path + '/u_reduced', mesh, proj_reduced_on_cell0Ds.numeric_solution) other_ut.plot_solution(mesh, proj_reduced_on_cell0Ds.numeric_solution) ``` -------------------------------- ### Loading and Processing Mesh Data Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Initializes mesh data structures and imports a 2D mesh from a specified folder. Computes geometric data associated with the mesh. ```python mesh_data = gedim.MeshMatrices() mesh = gedim.MeshMatricesDAO(mesh_data) polydim.pde_tools.mesh.pde_mesh_utilities.import_mesh_2_d(geometry_utilities, mesh_utilities, mesh_type, import_mesh_folder, mesh) mesh_geometric_data = polydim.pde_tools.mesh.pde_mesh_utilities.compute_mesh_2_d_geometry_data(geometry_utilities, mesh_utilities, mesh) ``` -------------------------------- ### Assemble Diffusion Operator for Exterior Domain Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Defines a radial function 'omega_2' to represent the exterior of a circular domain and assembles the diffusion operator for this region. The resulting operator matrices are converted to NumPy sparse format. ```python def omega_2(x, y, z): if (x * x + y * y) <= (R * R + 1.0e-13): return 0.0 return 1.0 A_omega_2 = polydim.pde_tools.assembler_utilities.pcc_2_d.assemble_diffusion_operator(geometry_utilities, mesh, mesh_geometric_data, trial_dofs_data, test_dofs_data, trial_reference_element_data, test_reference_element_data, omega_2) A_2 = other_ut.make_np_sparse(A_omega_2.operator_dofs) A_2_D = other_ut.make_np_sparse(A_omega_2.operator_strong) ``` -------------------------------- ### Assemble Weak Term Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Assembles a weak term for a 2D PDE. The function handles different markers to define the term's behavior. ```python def weak_term_function(marker, x, y, z): match marker: case 1: return 1.0 case _: raise ValueError("not valid marker", marker) weak_term = polydim.pde_tools.assembler_utilities.pcc_2_d.assemble_weak_term(geometry_utilities, mesh, mesh_geometric_data, trial_mesh_dofs_info, test_dofs_data, trial_reference_element_data, test_reference_element_data, weak_term_function) ``` -------------------------------- ### Define Boundary Information for Mesh DOFs Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Sets up boundary information objects for different boundary types (internal, Dirichlet, Neumann) and assigns markers. This is used to configure the DOFs manager for mesh processing. ```python info_internal = polydim.pde_tools.do_fs.DOFsManager.MeshDOFsInfo.BoundaryInfo(polydim.pde_tools.do_fs.DOFsManager.MeshDOFsInfo.BoundaryInfo.BoundaryTypes.none) info_internal.marker = 0 info_dirichlet_up = polydim.pde_tools.do_fs.DOFsManager.MeshDOFsInfo.BoundaryInfo(polydim.pde_tools.do_fs.DOFsManager.MeshDOFsInfo.BoundaryInfo.BoundaryTypes.strong) info_dirichlet_up.marker = 3 info_neumann_down = polydim.pde_tools.do_fs.DOFsManager.MeshDOFsInfo.BoundaryInfo(polydim.pde_tools.do_fs.DOFsManager.MeshDOFsInfo.BoundaryInfo.BoundaryTypes.weak) info_neumann_down.marker = 1 info_neumann_none = polydim.pde_tools.do_fs.DOFsManager.MeshDOFsInfo.BoundaryInfo(polydim.pde_tools.do_fs.DOFsManager.MeshDOFsInfo.BoundaryInfo.BoundaryTypes.none) info_neumann_none.marker = 2 boundary_info = { 0: info_internal, 1: info_neumann_down, 2: info_neumann_none, 3: info_dirichlet_up } ``` -------------------------------- ### Exporting and Plotting Mesh Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Exports the processed mesh data to a VTK file and generates a plot of the mesh using utility functions. ```python vtk_utilities.export_mesh(export_mesh_path, mesh) other_ut.plot_mesh(mesh) ``` -------------------------------- ### Assemble Diffusion Operator for a Circular Domain Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Defines a radial function 'omega_1' to represent a circular domain and assembles the diffusion operator for this domain. The resulting operator matrices are converted to NumPy sparse format. ```python R = 0.5 def omega_1(x, y, z): if (x * x + y * y) <= (R * R + 1.0e-13): return 1.0 return 0.0 A_omega_1 = polydim.pde_tools.assembler_utilities.pcc_2_d.assemble_diffusion_operator(geometry_utilities, mesh, mesh_geometric_data, trial_dofs_data, test_dofs_data, trial_reference_element_data, test_reference_element_data, omega_1) A_1 = other_ut.make_np_sparse(A_omega_1.operator_dofs) A_1_D = other_ut.make_np_sparse(A_omega_1.operator_strong) ``` -------------------------------- ### Eigenvalue Decomposition and Dimension Reduction Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Performs eigenvalue decomposition on a matrix C to obtain eigenvalues and eigenvectors. It then determines the reduced dimension N based on a tolerance 'tol' for retained energy. ```python # ALTERNATIVE: VM, L, VMt = np.linalg.svd((C)) L_e, VM_e = np.linalg.eig(C) eigenvalues = [] eigenvectors = [] #### check for i in range(len(L_e)): eig_real = L_e[i].real eig_complex = L_e[i].imag assert np.isclose(eig_complex, 0.) eigenvalues.append(eig_real) eigenvectors.append(VM_e[i].real) total_energy = sum(eigenvalues) retained_energy_vector = np.cumsum(eigenvalues) relative_retained_energy = retained_energy_vector/total_energy if all(flag==False for flag in relative_retained_energy>= tol): N = N_max else: N = np.argmax(relative_retained_energy >= tol) + 1 print("The reduced dimension is", N) ``` -------------------------------- ### Calculate Covariance Matrix for POD Source: https://github.com/aurion-polito/pypolydim/blob/main/ROM_POD.ipynb Calculates the covariance matrix C used in the Proper Orthogonal Decomposition (POD). This matrix is essential for solving the eigenvalue problem. ```python ### covariance matrix C = snapshot_matrix @ inner_product @ np.transpose(snapshot_matrix) ###### shape?? ############ ``` -------------------------------- ### Deactivate Python Virtual Environment Source: https://github.com/aurion-polito/pypolydim/blob/main/README.md Deactivates the currently active Python virtual environment, returning the shell to the system's default Python environment. ```bash deactivate ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.