Skip to content

Commit

Permalink
GetSensitivities (#12714)
Browse files Browse the repository at this point in the history
Co-authored-by: IAntonau <[email protected]>
  • Loading branch information
Igarizza and IAntonau authored Oct 2, 2024
1 parent 85f52e9 commit ac3b5fc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ def CalculateGradient(self, physical_variable_collective_expressions: 'dict[Supp
self.adjoint_analysis._GetSolver().GetComputingModelPart().ProcessInfo[KratosDT.TEST_ANALYSIS_NAME] = exec_policy.GetName()
self.adjoint_analysis._GetSolver().GetComputingModelPart().ProcessInfo[Kratos.STEP] = self.optimization_problem.GetStep()
self.adjoint_analysis.RunSolutionLoop()
sensitivities = self.adjoint_analysis.GetSensitivities()

for physical_variable, collective_expression in physical_variable_collective_expressions.items():
sensitivity_variable = Kratos.KratosGlobals.GetVariable(f"{physical_variable.Name()}_SENSITIVITY")
for container_expression in collective_expression.GetContainerExpressions():
sensitivities = self.adjoint_analysis.GetSensitivities(container_expression.GetModelPart())
container_expression.SetExpression((container_expression.GetExpression() - sensitivities[sensitivity_variable].GetExpression() * test_case_weight))
container_expression.SetExpression(Kratos.Expression.Utils.Collapse(container_expression).GetExpression())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def RunSolutionLoop(self):
sensor.FinalizeSolutionStep()
self.FinalizeSolutionStep()

sensitivities = self.GetSensitivities()
sensitivities = self.GetSensitivities(self._GetSolver().GetSensitivityModelPart())
for var, cexp in sensitivities.items():
exp_io.Write(f"{sensor.GetName()}_{var.Name()}", cexp)

Expand All @@ -117,14 +117,13 @@ def PrintAnalysisStageProgressInformation(self):
process_info = self._GetSolver().GetComputingModelPart().ProcessInfo
Kratos.Logger.PrintInfo(self._GetSimulationName(), f"Computed sensitivities for {process_info[KratosSI.SENSOR_NAME]} using \"{process_info[KratosSI.TEST_ANALYSIS_NAME]}\" analysis.")

def GetSensitivities(self) -> 'dict[typing.Union[Kratos.DoubleVariable, Kratos.Array1DVariable3], ExpressionUnionType]':
sensitivity_model_part: Kratos.ModelPart = self._GetSolver().GetSensitivityModelPart()
def GetSensitivities(self, model_part: Kratos.ModelPart) -> 'dict[typing.Union[Kratos.DoubleVariable, Kratos.Array1DVariable3], ExpressionUnionType]':
sensitivity_variables: 'dict[ExpressionDataLocation, list[typing.Union[Kratos.DoubleVariable, Kratos.Array1DVariable3]]]' = self._GetSolver().GetSensitivtyVariables()

result: 'dict[typing.Union[Kratos.DoubleVariable, Kratos.Array1DVariable3], ExpressionUnionType]' = {}
for data_location, variables in sensitivity_variables.items():
for variable in variables:
result[variable] = GetContainerExpression(sensitivity_model_part, data_location, variable)
result[variable] = GetContainerExpression(model_part, data_location, variable)
return result

if __name__ == "__main__":
Expand Down

0 comments on commit ac3b5fc

Please sign in to comment.