|
| 1 | +# Importing the Kratos Library |
| 2 | +import sys |
| 3 | +import time |
| 4 | +import os |
| 5 | +import KratosMultiphysics |
| 6 | + |
| 7 | +# Import packages |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +# Import pickle for serialization |
| 11 | +import pickle |
| 12 | + |
| 13 | +# Import pycompss |
| 14 | +from pycompss.api.task import task |
| 15 | +from pycompss.api.constraint import constraint |
| 16 | +from pycompss.api.api import compss_wait_on, compss_barrier |
| 17 | +from pycompss.api.parameter import * |
| 18 | +from pycompss.api.api import compss_barrier |
| 19 | +from pycompss.api.software import software |
| 20 | +from pycompss.api.data_transformation import * |
| 21 | + |
| 22 | +from dts import * |
| 23 | + |
| 24 | +SW_CATALOG = os.environ.get("SW_CATALOG","/software-catalog/packages") |
| 25 | + |
| 26 | +# Workflows constants |
| 27 | +TotalNumberOFCases = 5 |
| 28 | +number_of_dofs = 604264 |
| 29 | +snapshots_per_simulation = 11 |
| 30 | +number_of_columns = TotalNumberOFCases * snapshots_per_simulation |
| 31 | +expected_shape = (number_of_dofs,number_of_columns) # We will know the size of the array! |
| 32 | +row_splits = 10 |
| 33 | +column_splits = 1 |
| 34 | +A_row_chunk_size = int(number_of_dofs / row_splits) |
| 35 | +A_column_chunk_size = int(number_of_columns / column_splits) |
| 36 | +desired_block_size = (A_row_chunk_size, A_column_chunk_size) |
| 37 | +simulation_block_size = (number_of_dofs, snapshots_per_simulation) |
| 38 | +desired_rank=30 |
| 39 | + |
| 40 | +@software(config_file = SW_CATALOG+"/kratos/fom.json") |
| 41 | +def execute_FOM_instance(model,parameters, sample): |
| 42 | + import KratosMultiphysics |
| 43 | + from kratos_simulations import GetTrainingData |
| 44 | + current_model = KratosMultiphysics.Model() |
| 45 | + model.Load("ModelSerialization",current_model) |
| 46 | + del(model) |
| 47 | + current_parameters = KratosMultiphysics.Parameters() |
| 48 | + parameters.Load("ParametersSerialization",current_parameters) |
| 49 | + del(parameters) |
| 50 | + # get sample |
| 51 | + simulation = GetTrainingData(current_model,current_parameters,sample) |
| 52 | + simulation.Run() |
| 53 | + return simulation.GetSnapshotsMatrix() |
| 54 | + |
| 55 | + |
| 56 | +@dt(target="rom", function=ROM_file_generation, type=OBJECT_TO_FILE, destination=sys.argv[3]) |
| 57 | +@software(config_file = SW_CATALOG + "/kratos/rom.json") |
| 58 | +def execute_ROM_instance(model,parameters,sample,rom): |
| 59 | + import KratosMultiphysics |
| 60 | + from kratos_simulations import RunROM_SavingData |
| 61 | + load_ROM(rom) |
| 62 | + current_model = KratosMultiphysics.Model() |
| 63 | + model.Load("ModelSerialization",current_model) |
| 64 | + del(model) |
| 65 | + current_parameters = KratosMultiphysics.Parameters() |
| 66 | + parameters.Load("ParametersSerialization",current_parameters) |
| 67 | + del(parameters) |
| 68 | + # get sample |
| 69 | + simulation = RunROM_SavingData(current_model,current_parameters,sample) |
| 70 | + simulation.Run() |
| 71 | + return simulation.GetSnapshotsMatrix() |
| 72 | + |
| 73 | + |
| 74 | +@software(config_file = SW_CATALOG+"/kratos/model.json") |
| 75 | +def load_model_parameters(model_file): |
| 76 | + import KratosMultiphysics |
| 77 | + from kratos_simulations import GetTrainingData |
| 78 | + with open(model_file,'r') as parameter_file: |
| 79 | + parameters = KratosMultiphysics.Parameters(parameter_file.read()) |
| 80 | + model = KratosMultiphysics.Model() |
| 81 | + fake_sample = [5] |
| 82 | + simulation = GetTrainingData(model,parameters,fake_sample) |
| 83 | + serialized_model = KratosMultiphysics.StreamSerializer() |
| 84 | + serialized_model.Save("ModelSerialization",simulation.model) |
| 85 | + serialized_parameters = KratosMultiphysics.StreamSerializer() |
| 86 | + serialized_parameters.Save("ParametersSerialization",simulation.project_parameters) |
| 87 | + return serialized_model,serialized_parameters |
| 88 | + |
| 89 | +@dt("blocks", load_blocks_rechunk, shape=expected_shape, block_size=simulation_block_size, |
| 90 | + new_block_size=desired_block_size, is_workflow=True) |
| 91 | +@software(config_file = SW_CATALOG + "/dislib/dislib.json") |
| 92 | +def rSVD(blocks, desired_rank=30): |
| 93 | + from dislib_randomized_svd import rsvd |
| 94 | + u,s = rsvd(blocks, desired_rank, A_row_chunk_size, A_column_chunk_size) |
| 95 | + return u |
| 96 | + |
| 97 | + |
| 98 | +@dt("SnapshotsMatrixROM", load_blocks_rechunk, shape=expected_shape, block_size=simulation_block_size, |
| 99 | + new_block_size=desired_block_size, is_workflow=True) |
| 100 | +@dt("SnapshotsMatrixFOM", load_blocks_rechunk, shape=expected_shape, block_size=simulation_block_size, |
| 101 | + new_block_size=desired_block_size, is_workflow=True) |
| 102 | +@software(config_file = SW_CATALOG + "/dislib/dislib.json") |
| 103 | +def compare_ROM_vs_FOM(SnapshotsMatrixROM, SnapshotsMatrixFOM): |
| 104 | + import dislib as ds |
| 105 | + import numpy as np |
| 106 | + #using the Frobenious norm of the snapshots of the solution |
| 107 | + original_norm= np.linalg.norm((SnapshotsMatrixFOM.norm().collect())) |
| 108 | + intermediate = ds.data.matsubtract(SnapshotsMatrixROM, SnapshotsMatrixFOM) #(available on latest release) |
| 109 | + intermediate = np.linalg.norm((intermediate.norm().collect())) |
| 110 | + final = intermediate/original_norm |
| 111 | + np.save('relative_error_rom.npy', final) |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + |
| 115 | + data_path = sys.argv[1] |
| 116 | + parameters_template = sys.argv[2] |
| 117 | + rom_file = sys.argv[3] |
| 118 | + model_file="ProjectParameters_run.json" |
| 119 | + replace_template(parameters_template, model_file, '%MODEL_PATH%', data_path) |
| 120 | + |
| 121 | + """ |
| 122 | + Here we define the parameters for the simulation. |
| 123 | + In this case a sinlge parameter is defined. |
| 124 | + More parameters are possible. |
| 125 | + """ |
| 126 | + sim_cfgs = range(5,10) |
| 127 | + model, parameters = load_model_parameters(model_file) |
| 128 | + """ |
| 129 | + Stage 1 |
| 130 | + - launches in parallel a Full Order Model (FOM) simulation for each simulation parameter. |
| 131 | + """ |
| 132 | + sim_results=[] |
| 133 | + for cfg in sim_cfgs: |
| 134 | + sim_results.append(execute_FOM_instance(model,parameters,[cfg])) |
| 135 | + """ |
| 136 | + Stage 2 |
| 137 | + - computes the "fixed rank" randomized SVD in parallel using the dislib library #TODO implement the fixed presicion RSVD |
| 138 | + """ |
| 139 | + rom = rSVD(sim_results, desired_rank) |
| 140 | + """ |
| 141 | + Stage 3 |
| 142 | + - launches the Reduced Order Model simulations for the same simulation parameters used for the FOM |
| 143 | + """ |
| 144 | + rom_results=[] |
| 145 | + for cfg in sim_cfgs: |
| 146 | + sim_results.append(execute_ROM_instance(model,parameters,[cfg],rom)) |
| 147 | + |
| 148 | + #compare_ROM_vs_FOM(rom_results, sim_results) |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
0 commit comments