Skip to content

Controls can be initialized from csv files now. Added test for MaterialPropertiesControl - #14616

Open
talhah-ansari wants to merge 2 commits into
masterfrom
sicontrol
Open

Controls can be initialized from csv files now. Added test for MaterialPropertiesControl#14616
talhah-ansari wants to merge 2 commits into
masterfrom
sicontrol

Conversation

@talhah-ansari

Copy link
Copy Markdown
Contributor

📝 Description

This pull request adds support for initializing control fields from CSV files in both DataValuesControl and MaterialPropertiesControl, 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

  • Added a new "control_initialization_settings" parameter to both DataValuesControl and MaterialPropertiesControl, 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

  • Imported pandas as a new dependency in both control modules to facilitate reading CSV files.

Testing: New and Updated Unit Tests

  • Added and extended test cases to verify initialization from CSV files for both nodal historical and condition container types. The tests create temporary CSV files with initial values, initialize the controls from these files, and check that the data is correctly loaded.

@talhah-ansari talhah-ansari self-assigned this Jul 29, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_settings to DataValuesControl and MaterialPropertiesControl, enabling initialization from a CSV file during Initialize().
  • 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants