Controls can be initialized from csv files now. Added test for MaterialPropertiesControl - #14616
Open
talhah-ansari wants to merge 2 commits into
Open
Controls can be initialized from csv files now. Added test for MaterialPropertiesControl#14616talhah-ansari wants to merge 2 commits into
talhah-ansari wants to merge 2 commits into
Conversation
…alPropertiesControl
There was a problem hiding this comment.
Pull request overview
This PR introduces optional initialization of SystemIdentification control fields from CSV files for DataValuesControl and MaterialPropertiesControl, and extends the test suite to cover initialization behavior across container types.
Changes:
- Added
control_initialization_settingstoDataValuesControlandMaterialPropertiesControl, enabling initialization from a CSV file duringInitialize(). - Implemented CSV reading and assignment into the corresponding tensor adaptors.
- Added/extended unit tests and included them in the SystemIdentificationApplication test suite.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| applications/SystemIdentificationApplication/python_scripts/controls/data_values_control.py | Adds CSV-based initialization support for control fields across supported container types. |
| applications/SystemIdentificationApplication/python_scripts/controls/material_properties_control.py | Adds CSV-based initialization support for element material property controls. |
| applications/SystemIdentificationApplication/tests/controls/test_data_values_control.py | Adds initialization-from-CSV test cases for nodal historical and condition container types; adds CSV cleanup. |
| applications/SystemIdentificationApplication/tests/controls/test_material_properties_control.py | New tests for MaterialPropertiesControl, including initialization-from-CSV coverage. |
| applications/SystemIdentificationApplication/tests/test_SystemIdentificationApplication.py | Registers new/extended control tests in the SystemIdentificationApplication test suites. |
Comments suppressed due to low confidence (1)
applications/SystemIdentificationApplication/tests/controls/test_data_values_control.py:406
- Same as the nodal initialization test: the CSV is written relative to the runner's current working directory, while cleanup is relative to the test file directory. This can make the test flaky and/or leave files behind. Prefer creating the CSV and running Initialize() inside WorkFolderScope.
# Create a CSV file with initial control values
initial_control_values = np.ones(len(cls.model_part.Conditions))
initial_control_values[:] = -2.5 # Set all initial control values to -2.5
with open("initial_condition_control_values.csv", "w") as f:
f.write("ConditionId,PRESSURE\n")
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
113
to
+117
| if not KratosOA.OptAppModelPartUtils.CheckModelPartStatus(self.primal_model_part, "element_specific_properties_created"): | ||
| KratosOA.OptimizationUtils.CreateEntitySpecificPropertiesForContainer(self.primal_model_part, self.primal_model_part.Elements, self.consider_recursive_property_update) | ||
| KratosOA.OptAppModelPartUtils.LogModelPartStatus(self.primal_model_part, "element_specific_properties_created") | ||
|
|
||
| if self.initialize_from_file: |
Comment on lines
+123
to
+136
| # initialize the control field if required from a file | ||
| if self.initialize_from_file: | ||
| df = numpy.loadtxt(self.initialization_file_name, delimiter=',', skiprows=1) | ||
|
|
||
| # check sizes | ||
| if df.shape[1] != 2: | ||
| raise RuntimeError(f"Initialization file for control \"{self.GetName()}\" should have 2 columns (Node/Element/ConditionID, {self.controlled_physical_variable.Name()}) with first row for header. [ provided file: {self.initialization_file_name} ]") | ||
| if len(df) != self.GetEmptyField().Shape()[0]: | ||
| raise RuntimeError(f"Control initialization file does not have the required number of entries. [ control name = \"{self.GetName()}\", required = {self.GetEmptyField().Shape()[0]}, provided = {len(df)}]") | ||
|
|
||
| # Assign to the specific field | ||
| ta = GetTensorAdaptor(self.primal_model_part, self.container_type, self.controlled_physical_variable) | ||
| ta.data[:] = df[:, 1].flatten() | ||
| ta.StoreData() |
Comment on lines
+205
to
+213
| # Create a CSV file with initial control values | ||
| initial_control_values = np.ones(len(cls.model_part.Nodes)) | ||
| initial_control_values[:] = -2.5 # Set all initial control values to -2.5 | ||
| with open("initial_nodal_historical_control_values.csv", "w") as f: | ||
| f.write("NodeId,TEMPERATURE\n") | ||
| for idx, node in enumerate(cls.model_part.Nodes): | ||
| f.write(f"{node.Id},{initial_control_values[idx]}\n") | ||
|
|
||
| cls.temperature_control.Initialize() |
Comment on lines
+191
to
+200
| # Create a CSV file with initial control values | ||
| initial_control_values = np.ones(len(cls.model_part.Elements)) | ||
| initial_control_values[:] = 2e6 # Set all initial control values to 2e6 | ||
| with open("initial_material_properties_control_values.csv", "w") as f: | ||
| f.write("ElementId,YOUNG_MODULUS\n") | ||
| for idx, element in enumerate(cls.model_part.Elements): | ||
| f.write(f"{element.Id},{initial_control_values[idx]}\n") | ||
|
|
||
| cls.young_modulus_control.Initialize() | ||
| cls.initial_young_modulus = KratosOA.TensorAdaptors.PropertiesVariableTensorAdaptor(cls.model_part.Elements, Kratos.YOUNG_MODULUS) |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📝 Description
This pull request adds support for initializing control fields from CSV files in both
DataValuesControlandMaterialPropertiesControl, enabling users to specify initial control values via external files. It also introduces comprehensive tests to verify this feature for different container types.The most important changes are:
Feature: Control Field Initialization from CSV Files
"control_initialization_settings"parameter to bothDataValuesControlandMaterialPropertiesControl, allowing control fields to be optionally initialized from a CSV file. The code reads the CSV, checks for size consistency, and assigns the values to the appropriate tensor adaptor. Only CSV files are currently supported.Dependency Update
pandasas a new dependency in both control modules to facilitate reading CSV files.Testing: New and Updated Unit Tests