Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,28 @@ def __init__(self, parameters: Kratos.Parameters, master_control: MasterControl,
raise RuntimeError(f"Provided \"type\" = {self.__constraint_type} is not supported in constraint response functions. Followings are supported options: \n\t=\n\t<=\n\t<\n\t>=\n\t>")

# buffer coefficients
self.BSF = parameters["buffer_factor"].GetDouble()
self.BSF_init = self.BSF
self.CBV = 0.0
self.BS = 1e-6
self.BSF_init = parameters["buffer_factor"].GetDouble()
self.max_w_c = parameters["max_w_c"].GetDouble()
self.CF = 1.0

self.tolerance = parameters["tolerance"].GetDouble()

# these coefficients evolve every iteration (see UpdateBufferSize). On a restart
# (step > 0) they are re-hydrated from unbuffered data instead of re-initialized,
# so the constraint resumes exactly where it left off.
if optimization_problem.GetStep() > 0 and self.__unbuffered_data.HasValue("BSF"):
self.BSF = self.__unbuffered_data["BSF"]
self.CBV = self.__unbuffered_data["CBV"]
self.BS = self.__unbuffered_data["BS"]
self.CF = self.__unbuffered_data["CF"]
else:
self.BSF = self.BSF_init
self.CBV = 0.0
self.BS = 1e-6
self.CF = 1.0
self.__unbuffered_data.SetValue("BSF", self.BSF, overwrite=True)
self.__unbuffered_data.SetValue("CBV", self.CBV, overwrite=True)
self.__unbuffered_data.SetValue("BS", self.BS, overwrite=True)
self.__unbuffered_data.SetValue("CF", self.CF, overwrite=True)

def IsEqualityType(self) -> str:
return self.__constraint_type == ConstraintType.EQUAL

Expand Down Expand Up @@ -170,6 +183,12 @@ def UpdateBufferSize(self):
if delta_values[0] * delta_values[1] < 0.0 and delta_values[1] * delta_values[2] < 0.0:
self.BSF += 1

# write-through so a restart can re-hydrate these evolving coefficients (see __init__)
self.__unbuffered_data.SetValue("BSF", self.BSF, overwrite=True)
self.__unbuffered_data.SetValue("CBV", self.CBV, overwrite=True)
self.__unbuffered_data.SetValue("BS", self.BS, overwrite=True)
self.__unbuffered_data.SetValue("CF", self.CF, overwrite=True)

print(f"RGP Constraint {self.GetResponseName()}:: UpdateBufferSize")
print(f"RGP Constraint {self.GetResponseName()}:: CBV = {self.CBV}")
print(f"RGP Constraint {self.GetResponseName()}:: BSF = {self.BSF}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ def __init__(self, name: str, model: Kratos.Model, parameters: Kratos.Parameters

self.materials = Materials(parameters["list_of_materials"].values())

self.density_projection = CreateProjection(parameters["density_projection_settings"], self.optimization_problem)
self.young_modulus_projection = CreateProjection(parameters["young_modulus_projection_settings"], self.optimization_problem)
density_projection_restart_data = ComponentDataView(f"{self.GetName()}:density_projection", optimization_problem).GetUnBufferedData()
self.density_projection = CreateProjection(parameters["density_projection_settings"], self.optimization_problem, density_projection_restart_data)
young_modulus_projection_restart_data = ComponentDataView(f"{self.GetName()}:young_modulus_projection", optimization_problem).GetUnBufferedData()
self.young_modulus_projection = CreateProjection(parameters["young_modulus_projection_settings"], self.optimization_problem, young_modulus_projection_restart_data)

controlled_model_names_parts = parameters["controlled_model_part_names"].GetStringArray()
if len(controlled_model_names_parts) == 0:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def __init__(self, name: str, model: Kratos.Model, parameters: Kratos.Parameters
self.output_all_fields = self.parameters["output_all_fields"].GetBool()
self.physical_thicknesses = self.parameters["physical_thicknesses"].GetVector()

self.thickness_projection = CreateProjection(parameters["thickness_projection_settings"], self.optimization_problem)
thickness_projection_restart_data = ComponentDataView(f"{self.GetName()}:thickness_projection", optimization_problem).GetUnBufferedData()
self.thickness_projection = CreateProjection(parameters["thickness_projection_settings"], self.optimization_problem, thickness_projection_restart_data)

self.consider_recursive_property_update = parameters["consider_recursive_property_update"].GetBool()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ def Initialize(self):
component = GetComponentHavingDataByFullName(self.__component_name, self.__optimization_problem)
self.__component_data_view = ComponentDataView(component, self.__optimization_problem)

# __abs_value_change/__init_value evolve across iterations and are mirrored into
# unbuffered data below. On a restart (step > 0), re-hydrate them here instead of
# letting IsConverged() re-initialize from the current (mid-run) value.
unbuffered_data = self.__component_data_view.GetUnBufferedData()
prefix = f"{self.__value_name.split(':')[0]}_avg_abs_{self.__tracked_iter}_itr"
if self.__optimization_problem.GetStep() > 0 and unbuffered_data.HasValue(f"{prefix}_init_value"):
self.__init_value = unbuffered_data[f"{prefix}_init_value"]
self.__abs_value_change = numpy.array(unbuffered_data[f"{prefix}_abs_value_change"])

@time_decorator()
def IsConverged(self) -> bool:
step = self.__optimization_problem.GetStep()
Expand All @@ -78,7 +87,11 @@ def IsConverged(self) -> bool:
self.__value = numpy.abs(numpy.sum(self.__abs_value_change)) / step
self.__conv = False

self.__component_data_view.GetBufferedData().SetValue(f"{self.__value_name.split(':')[0]}_avg_abs_{self.__tracked_iter}_itr", self.__value)
prefix = f"{self.__value_name.split(':')[0]}_avg_abs_{self.__tracked_iter}_itr"
unbuffered_data = self.__component_data_view.GetUnBufferedData()
unbuffered_data.SetValue(f"{prefix}_init_value", self.__init_value, overwrite=True)
unbuffered_data.SetValue(f"{prefix}_abs_value_change", self.__abs_value_change.tolist(), overwrite=True)
self.__component_data_view.GetBufferedData().SetValue(prefix, self.__value)

return self.__conv

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def IsConverged(self) -> bool:
if not hasattr(field, "data"):
raise RuntimeError(f"The value represented by {self.__field_name} is not a field.")

self.__norm = numpy.linalg.norm(field.data)
self.__norm = float(numpy.linalg.norm(field.data))
self.__conv = self.__norm <= self.__tolerance
self.__component_data_view.GetBufferedData().SetValue(self.__field_name.split(':')[0] + "_l2_norm", self.__norm)
return self.__conv
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ def Initialize(self):
component = GetComponentHavingDataByFullName(self.__component_name, self.__optimization_problem)
self.__component_data_view = ComponentDataView(component, self.__optimization_problem)

# "target_value" is normally computed once, the first time IsConverged() sees step == 0.
# On a restart that first call never happens again in this process, so re-hydrate it here.
target_key = f"{self.__value_name.split(':')[0]}_conv_target"
unbuffered_data = self.__component_data_view.GetUnBufferedData()
if self.__optimization_problem.GetStep() > 0 and unbuffered_data.HasValue(target_key):
self.__target_value = unbuffered_data[target_key]

@time_decorator()
def IsConverged(self) -> bool:
iter = self.__optimization_problem.GetStep()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, parameters: Kratos.Parameters, optimization_problem: Optimiza
self.__max_iter = parameters["max_iter"].GetInt()
self.__optimization_problem = optimization_problem

if self.__max_iter <= 0:
if self.__max_iter < 0:
raise RuntimeError(f"The number of max iterations cannot be zero or negative.")
Comment thread
Igarizza marked this conversation as resolved.
Outdated

def Initialize(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ def Initialize(self):
component = GetComponentHavingDataByFullName(self.__component_name, self.__optimization_problem)
self.__component_data_view = ComponentDataView(component, self.__optimization_problem)

# __best_value/__patience_step/__init_value evolve across iterations and are mirrored
# into unbuffered data below. On a restart (step > 0), re-hydrate them here instead of
# letting IsConverged() re-initialize from the current (mid-run) value.
unbuffered_data = self.__component_data_view.GetUnBufferedData()
prefix = f"{self.__value_name.split(':')[0]}_pat_itr_{self.__patience_itr}"
if self.__optimization_problem.GetStep() > 0 and unbuffered_data.HasValue(f"{prefix}_best_value"):
self.__best_value = unbuffered_data[f"{prefix}_best_value"]
self.__patience_step = unbuffered_data[f"{prefix}_patience_step"]
self.__init_value = unbuffered_data[f"{prefix}_init_value"]

@time_decorator()
def IsConverged(self) -> bool:
step = self.__optimization_problem.GetStep()
Expand All @@ -93,8 +103,11 @@ def IsConverged(self) -> bool:
else:
self.__conv = False

self.__component_data_view.GetBufferedData().SetValue(f"{self.__value_name.split(':')[0]}_pat_itr_{self.__patience_itr}_best_value", self.__best_value)
self.__component_data_view.GetBufferedData().SetValue(f"{self.__value_name.split(':')[0]}_pat_itr_{self.__patience_itr}_patience_step", self.__patience_step)
prefix = f"{self.__value_name.split(':')[0]}_pat_itr_{self.__patience_itr}"
unbuffered_data = self.__component_data_view.GetUnBufferedData()
unbuffered_data.SetValue(f"{prefix}_best_value", self.__best_value, overwrite=True)
unbuffered_data.SetValue(f"{prefix}_patience_step", self.__patience_step, overwrite=True)
unbuffered_data.SetValue(f"{prefix}_init_value", self.__init_value, overwrite=True)

return self.__conv

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def __init__(self, model: Kratos.Model, project_parameters: Kratos.Parameters):
def Initialize(self):
CallOnAll(self.__list_of_model_part_controllers, ModelPartController.ImportModelPart)
CallOnAll(self.__list_of_model_part_controllers, ModelPartController.Initialize)
CallOnAll(self.optimization_problem.GetListOfExecutionPolicies(), ExecutionPolicyDecorator.Initialize)
for process_type in self.__algorithm.GetProcessesOrder():
CallOnAll(self.optimization_problem.GetListOfProcesses(process_type), Kratos.Process.ExecuteInitialize)
CallOnAll(self.optimization_problem.GetListOfExecutionPolicies(), ExecutionPolicyDecorator.Initialize)

self.__algorithm.Initialize()

Expand Down
Loading
Loading