Skip to content

Commit

Permalink
[OptApp] Fix Output + Update QNBB (#12676)
Browse files Browse the repository at this point in the history
* fix

* fix algorithms

* apply

---------

Co-authored-by: IAntonau <[email protected]>
  • Loading branch information
Igarizza and IAntonau authored Sep 20, 2024
1 parent e678d21 commit 6222e4d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def Output(self) -> KratosOA.CollectiveExpression:
self.algorithm_data.GetBufferedData()["control_field"] = self.__control_field.Clone()
OutputGradientFields(self.__objective, self._optimization_problem, True)
for constraint in self.__constraints_list:
OutputGradientFields(constraint, self._optimization_problem, True)
OutputGradientFields(constraint, self._optimization_problem, constraint.IsActive())
for process in self._optimization_problem.GetListOfProcesses("output_processes"):
if process.IsOutputStep():
process.PrintOutput()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def Output(self) -> KratosOA.CollectiveExpression:
self.algorithm_data.GetBufferedData()["control_field"] = self.__control_field.Clone()
OutputGradientFields(self.__objective, self._optimization_problem, True)
for constraint in self.__constraints_list:
OutputGradientFields(constraint, self._optimization_problem, True)
OutputGradientFields(constraint, self._optimization_problem, constraint.IsActive())
for process in self._optimization_problem.GetListOfProcesses("output_processes"):
if process.IsOutputStep():
process.PrintOutput()
Expand All @@ -237,7 +237,7 @@ def CheckLinearizedConstraints(self, active_constr_grad: 'list[KratosOA.Collecti
return all_feasible

def GetActiveConstraintsList(self, ):
return [constraint for constraint in self.__constraints_list if constraint.IsActiveConstrant()]
return [constraint for constraint in self.__constraints_list if constraint.IsActive()]

def GetConstraintsList(self):
return self.__constraints_list
Expand Down Expand Up @@ -265,9 +265,9 @@ def Solve(self):
self.__constr_value.append(value)
constr_name = constraint.GetResponseName()
self.algorithm_data.GetBufferedData()[f"std_constr_{constr_name}_value"] = value
msg = "active" if constraint.IsActiveConstrant() else "non-active"
msg = "active" if constraint.IsActive() else "non-active"
print(f"RGP Constaint {constraint.GetResponseName()} is {msg}")
if constraint.IsActiveConstrant():
if constraint.IsActive():
active_constr_grad.append(constraint.CalculateStandardizedGradient())

w_r, w_c = self.ComputeBufferCoefficients()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ def CalculateStandardizedValue(self, control_field: KratosOA.CollectiveExpressio
DictLogger("Constraint info",self.GetInfo())

return standardized_response_value

def IsActive(self):
return self.GetStandardizedValue() > 0.0

def CalculateStandardizedGradient(self, save_field: bool = True) -> KratosOA.CollectiveExpression:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def UpdateBufferSize(self):
print(f"RGP Constraint {self.GetResponseName()}:: BS = {self.BS}")
print(f"RGP Constraint {self.GetResponseName()}:: CF = {self.CF}")

def IsActiveConstrant(self):
def IsActive(self):
return self.ComputeW() > 0.0

def IsSatisfied(self, value=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ def ComputeStep(self) -> KratosOA.CollectiveExpression:
d = algorithm_buffered_data.GetValue("control_field_update", 1)
dy = KratosOA.ExpressionUtils.InnerProduct(d,y)
dd = KratosOA.ExpressionUtils.InnerProduct(d,d)
if not math.isclose(dy, 0.0, abs_tol=1e-16):
self.step = abs( dd / dy )
else:
if math.isclose(dy, 0.0, abs_tol=1e-16):
self.step = self._max_step / norm
else:
self.step = abs( dd / dy )

if self.step > self._max_step / norm:
self.step = self._max_step / norm
Expand Down Expand Up @@ -144,12 +144,12 @@ def ComputeStep(self) -> KratosOA.CollectiveExpression:
d = algorithm_buffered_data.GetValue("control_field_update", 1)
d = d.Evaluate()
for i in range(len(y)):
yy = y[i] * y[i]
yd = y[i] * d[i]
if math.isclose(yy, 0.0, abs_tol=1e-16):
dy = d[i] * y[i]
dd = d[i] * d[i]
if math.isclose(dy, 0.0, abs_tol=1e-16):
self.step_numpy[i] = self._max_step / norm
else:
self.step_numpy[i] = abs( yd / yy )
self.step_numpy[i] = abs( dd / dy )
if self.step_numpy[i] > self._max_step / norm:
self.step_numpy[i] = self._max_step / norm

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
# Headers:
# STEP, mass:value, strain_energy:value
0, 5.887500e+03, 5.004872e+06
1, 5.848128e+03, 8.701519e+05
2, 5.545008e+03, 1.378514e+06
3, 5.276620e+03, 1.946824e+06
4, 5.030478e+03, 3.210748e+06
5, 4.808663e+03, 4.492684e+06
6, 4.612616e+03, 7.389329e+06
7, 4.746264e+03, 6.147038e+06
8, 4.814889e+03, 4.188684e+06
9, 4.667011e+03, 4.823676e+06
10, 4.529983e+03, 8.241959e+06
1, 5.878896e+03, 9.030505e+05
2, 5.804777e+03, 1.086486e+06
3, 5.745948e+03, 1.190949e+06
4, 5.687356e+03, 1.327465e+06
5, 5.629154e+03, 1.473035e+06
6, 5.571371e+03, 1.636759e+06
7, 5.514070e+03, 1.818822e+06
8, 5.457308e+03, 2.021705e+06
9, 5.401141e+03, 2.247398e+06
10, 5.345625e+03, 2.498211e+06
# End of file
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from KratosMultiphysics.OptimizationApplication.optimization_analysis import OptimizationAnalysis
from KratosMultiphysics.compare_two_files_check_process import CompareTwoFilesCheckProcess

# Temporaly failing
@KratosUnittest.skipIf(True, "Temporaly Failing")

class TestGradientProjectionAnalysis(KratosUnittest.TestCase):
def test_gradient_projection_analysis(self):
with KratosUnittest.WorkFolderScope(".", __file__):
Expand Down

0 comments on commit 6222e4d

Please sign in to comment.