-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12140 from KratosMultiphysics/RomApp_FixBugIndexes
[RomApp] Fixing Ids issues in ROM files.
- Loading branch information
Showing
8 changed files
with
219 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+168 Bytes
...ns/RomApplication/tests/calculate_rom_basis_output_process_test_files/NodeIds_Results.npy
Binary file not shown.
Binary file added
BIN
+208 Bytes
...lication/tests/calculate_rom_basis_output_process_test_files/RightBasisMatrix_Results.npy
Binary file not shown.
46 changes: 28 additions & 18 deletions
46
...ation/tests/calculate_rom_basis_output_process_test_files/RomParameters_test_Results.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,52 @@ | ||
{ | ||
"rom_manager": false, | ||
"train_hrom": false, | ||
"run_hrom": false, | ||
"projection_strategy": "galerkin", | ||
"assembling_strategy": "global", | ||
"rom_format": "json", | ||
"rom_settings": { | ||
"rom_bns_settings": {}, | ||
"nodal_unknowns": [ | ||
"TEMPERATURE" | ||
], | ||
"number_of_rom_dofs": 2, | ||
"petrov_galerkin_number_of_rom_dofs": 0, | ||
"rom_bns_settings":{} | ||
"petrov_galerkin_number_of_rom_dofs": 0 | ||
}, | ||
"hrom_settings": { | ||
"hrom_format": "json" | ||
}, | ||
"nodal_modes": { | ||
"1": [ | ||
"2": [ | ||
[ | ||
-0.17574638455134867, | ||
0.7543959227866553 | ||
-0.01983405679273213, | ||
0.5993762115792658 | ||
] | ||
], | ||
"2": [ | ||
"5": [ | ||
[ | ||
-0.2949770806015417, | ||
0.4615067950960123 | ||
-0.04391666176273041, | ||
0.5763832650962621 | ||
] | ||
], | ||
"3": [ | ||
"12": [ | ||
[ | ||
-0.4142077766517345, | ||
0.16861766740536666 | ||
-0.10010940669272668, | ||
0.5227330566359412 | ||
] | ||
], | ||
"4": [ | ||
"69": [ | ||
[ | ||
-0.5334384727019273, | ||
-0.12427146028527797 | ||
-0.5576789011226962, | ||
0.08586707345904437 | ||
] | ||
], | ||
"5": [ | ||
"102": [ | ||
[ | ||
-0.6526691687521202, | ||
-0.4171605879759215 | ||
-0.8225875557926786, | ||
-0.16705533785390972 | ||
] | ||
] | ||
} | ||
}, | ||
"elements_and_weights": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
applications/RomApplication/tests/test_calculate_rom_basis_output_process_numpy.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import os | ||
|
||
import KratosMultiphysics | ||
import KratosMultiphysics.KratosUnittest as KratosUnittest | ||
import KratosMultiphysics.kratos_utilities as kratos_utilities | ||
from KratosMultiphysics.RomApplication.calculate_rom_basis_output_process import CalculateRomBasisOutputProcess | ||
import numpy as np | ||
|
||
class TestCalculateRomBasisOutputProcessNumpy(KratosUnittest.TestCase): | ||
|
||
def setUp(self): | ||
# Test data | ||
self.print_output = False | ||
self.work_folder = "calculate_rom_basis_output_process_test_files" | ||
|
||
# List containing the fake total times | ||
self.time_steps_list = [1.0,2.0,4.0] | ||
|
||
# Create a model part to perform the ROM basis calculation | ||
self.model = KratosMultiphysics.Model() | ||
model_part = self.model.CreateModelPart("MainModelPart") | ||
model_part.AddNodalSolutionStepVariable(KratosMultiphysics.TEMPERATURE) | ||
node_ids_non_ordered = [2,5,12,69,102] | ||
n_nodes = len(node_ids_non_ordered) | ||
for i in range(n_nodes): | ||
model_part.CreateNewNode(node_ids_non_ordered[i],float(i),0.0,0.0) | ||
|
||
def testCalculateRomBasisOutputProcess(self): | ||
# Create a CalculateROMBasisOutputProcess instance | ||
self.process_settings = KratosMultiphysics.Parameters("""{ | ||
"model_part_name": "MainModelPart", | ||
"snapshots_control_type": "step", | ||
"snapshots_interval": 1.0, | ||
"nodal_unknowns": ["TEMPERATURE"], | ||
"rom_basis_output_format": "numpy", | ||
"rom_basis_output_name": "RomParameters_test", | ||
"rom_basis_output_folder": "rom_data_test", | ||
"svd_truncation_tolerance": 1.0e-6 | ||
}""") | ||
|
||
# Run a "fake" simulation to calculate the ROM basis from its results | ||
self.__ExecuteTest(self.process_settings) | ||
|
||
# Check results | ||
self.__CheckResults() | ||
|
||
def tearDown(self): | ||
with KratosUnittest.WorkFolderScope(self.work_folder, __file__): | ||
if not self.print_output: | ||
for path in os.listdir(): | ||
full_path = os.path.join(os.getcwd(), path) | ||
if not "_Results" in path: | ||
if os.path.isfile(full_path): | ||
kratos_utilities.DeleteFileIfExisting(full_path) | ||
elif os.path.isdir(full_path): | ||
kratos_utilities.DeleteDirectoryIfExisting(full_path) | ||
|
||
|
||
def __ExecuteTest(self, process_settings): | ||
# Emulate a simulation to get the ROM basis output | ||
with KratosUnittest.WorkFolderScope(self.work_folder, __file__): | ||
# Create a calculate ROM basis process instance | ||
rom_basis_process = CalculateRomBasisOutputProcess(self.model, process_settings) | ||
|
||
# Emulate simulation to fill the database and calculate the ROM basis | ||
rom_basis_process.ExecuteInitialize() | ||
rom_basis_process.Check() | ||
rom_basis_process.ExecuteBeforeSolutionLoop() | ||
|
||
model_part = self.model.GetModelPart("MainModelPart") | ||
for time,step in zip(self.time_steps_list, range(1,len(self.time_steps_list)+1,1)): | ||
# Fake time advance | ||
model_part.CloneTimeStep(time) | ||
model_part.ProcessInfo[KratosMultiphysics.STEP] = step | ||
|
||
# Set nodal values | ||
if step % 2 == 0: | ||
get_value = lambda node_id, time : node_id*time**2 + step | ||
else: | ||
get_value = lambda node_id, time : -node_id/time**step | ||
for node in model_part.Nodes: | ||
node.SetSolutionStepValue(KratosMultiphysics.TEMPERATURE, get_value(node.Id, time)) | ||
|
||
# Call the process instance methods | ||
rom_basis_process.ExecuteInitializeSolutionStep() | ||
if rom_basis_process.IsOutputStep(): | ||
rom_basis_process.ExecuteBeforeOutputStep() | ||
rom_basis_process.PrintOutput() | ||
rom_basis_process.ExecuteAfterOutputStep() | ||
rom_basis_process.ExecuteFinalizeSolutionStep() | ||
|
||
rom_basis_process.ExecuteFinalize() | ||
|
||
def __CheckResults(self): | ||
with KratosUnittest.WorkFolderScope(self.work_folder, __file__): | ||
# Load ROM basis output file | ||
output_folder = self.process_settings["rom_basis_output_folder"].GetString() | ||
RightBasisOutput_name = os.path.join(output_folder, "{}.{}".format("RightBasisMatrix", "npy")) #TODO allow name customization in Numpy | ||
NodeIdsOutput_name = os.path.join(output_folder, "{}.{}".format("NodeIds", "npy")) #TODO allow name customization in Numpy | ||
RightBasisOutput = np.load(RightBasisOutput_name) | ||
NodeIdsOutput = np.load(NodeIdsOutput_name) | ||
|
||
# Load reference file | ||
RightBasisReference_name = "{}_Results.{}".format("RightBasisMatrix", "npy") | ||
NodeIdsReference_name = "{}_Results.{}".format("NodeIds", "npy") | ||
RightBasisReference = np.load(RightBasisReference_name) | ||
NodeIdsReference = np.load(NodeIdsReference_name) | ||
|
||
|
||
|
||
for node_output, node_reference in zip(NodeIdsOutput,NodeIdsReference): | ||
self.assertEqual(node_output, node_reference) | ||
for i in range(RightBasisOutput.shape[0]): | ||
for j in range(RightBasisOutput.shape[1]): | ||
self.assertAlmostEqual(RightBasisOutput[i,j], RightBasisReference[i,j]) | ||
|
||
#JSON part tested in the JSON test already | ||
|
||
|
||
########################################################################################## | ||
|
||
if __name__ == '__main__': | ||
KratosMultiphysics.Logger.GetDefaultOutput().SetSeverity(KratosMultiphysics.Logger.Severity.WARNING) | ||
KratosUnittest.main() |