← Back to CouplingParametersPC documentation
CouplingParametersPC - Source Code¶
File: Distributed_Design_Optimizer/subsystem/couplingparameters/pc/CouplingParametersPC.py
# Copyright (C) The DistributedDesignOptimizer Contributors
# Licensed under the GNU General Public License v3.0. See LICENSE file for details.
"""Coupling weights module.
This module provides coupling weight parameters for the
Penalty Coordination method.
"""
import copy
from typing import List
from Distributed_Design_Optimizer.postprocess.terminal_print_tools import DDO_Color, Reset
from Distributed_Design_Optimizer.subsystem.tools import update_state_listprimitive
from Distributed_Design_Optimizer.subsystem.couplingparameters import SubSysCouplingParametersBasis
from Distributed_Design_Optimizer.middlelevel.pc import MiddleLevelCouplingPC
class CouplingParametersPC(SubSysCouplingParametersBasis):
"""Coupling weights for penalty coordination.
Extends SubSysCouplingParametersBasis with penalty weight vectors for
mapped-response/coupling-variable sides of the coupling circle and shared/target design variables.
"""
def __init__(self, id: str) -> None:
"""Initialize CouplingParametersPC.
Args:
id: Unique identifier for this coupling.
"""
super().__init__(id)
self._weights_mappedresponse_minus_copycouplingvariable: List[float] | None = None
self._weights_copymappedresponse_minus_couplingvariable: List[float] | None = None
self._weights_shareddesignvariable_minus_copytargetshareddesignvariable: List[float] | None = None
self._weights_copyshareddesignvariable_minus_targetshareddesignvariable: List[float] | None = None
def set_Weights_MappedResponse_Minus_CopyCouplingVariable(self, mappedresponse_minus_copycouplingvariable_in: List[float]) -> None:
"""Set the penalty weights for the mapped-response part of the coupling circle.
Args:
mappedresponse_minus_copycouplingvariable_in: Vector of penalty weights for mapped-response side.
"""
# copy.copy() used - List[float] is mutable. This prevents modifications
# in the caller from being reflected back to the class attribute.
self._weights_mappedresponse_minus_copycouplingvariable = copy.copy(mappedresponse_minus_copycouplingvariable_in)
def get_Weights_MappedResponse_Minus_CopyCouplingVariable(self) -> List[float] | None:
"""Get the penalty weights for the mapped-response part of the coupling circle.
Returns:
Vector of penalty weights for mapped-response side, or None if not set.
"""
# copy.copy() used - List[float] is mutable. This prevents modifications
# in the caller from being reflected back to the class attribute.
return copy.copy(self._weights_mappedresponse_minus_copycouplingvariable)
def set_Weights_CopyMappedResponse_Minus_CouplingVariable(self, copymappedresponse_minus_couplingvariable_in: List[float]) -> None:
"""Set the penalty weights for the coupling-variable part of the coupling circle.
Args:
copymappedresponse_minus_couplingvariable_in: Vector of penalty weights for coupling-variable side.
"""
# copy.copy() used - List[float] is mutable. This prevents modifications
# in the caller from being reflected back to the class attribute.
self._weights_copymappedresponse_minus_couplingvariable = copy.copy(copymappedresponse_minus_couplingvariable_in)
def get_Weights_CopyMappedResponse_Minus_CouplingVariable(self) -> List[float] | None:
"""Get the penalty weights for the coupling-variable part of the coupling circle.
Returns:
Vector of penalty weights for coupling-variable side, or None if not set.
"""
# copy.copy() used - List[float] is mutable. This prevents modifications
# in the caller from being reflected back to the class attribute.
return copy.copy(self._weights_copymappedresponse_minus_couplingvariable)
def set_Weights_SharedDesignVariable_Minus_CopyTargetSharedDesignVariable(self, shareddesignvariable_minus_copytargetshareddesignvariable_in: List[float]) -> None:
"""Set the weights for coordination of consistency constraint between shared design variables.
Args:
shareddesignvariable_minus_copytargetshareddesignvariable_in: Vector of weights for shared design variables.
"""
# copy.copy() used - List[float] is mutable. This prevents modifications
# in the caller from being reflected back to the class attribute.
self._weights_shareddesignvariable_minus_copytargetshareddesignvariable = copy.copy(shareddesignvariable_minus_copytargetshareddesignvariable_in)
def get_Weights_SharedDesignVariable_Minus_CopyTargetSharedDesignVariable(self) -> List[float] | None:
"""Get the weights for coordination of consistency constraints for shared design variables.
Returns:
Vector of weights for shared design variables, or None if not set.
"""
# copy.copy() used - List[float] is mutable. This prevents modifications
# in the caller from being reflected back to the class attribute.
return copy.copy(self._weights_shareddesignvariable_minus_copytargetshareddesignvariable)
def set_Weights_CopySharedDesignVariable_Minus_TargetSharedDesignVariable(self, copyshareddesignvariable_minus_targetshareddesignvariable_in: List[float]) -> None:
"""Set the weights for coordination of consistency constraint between target design variables.
Args:
copyshareddesignvariable_minus_targetshareddesignvariable_in: Vector of weights for target design variables.
"""
# copy.copy() used - List[float] is mutable. This prevents modifications
# in the caller from being reflected back to the class attribute.
self._weights_copyshareddesignvariable_minus_targetshareddesignvariable = copy.copy(copyshareddesignvariable_minus_targetshareddesignvariable_in)
def get_Weights_CopySharedDesignVariable_Minus_TargetSharedDesignVariable(self) -> List[float] | None:
"""Get the weights for coordination of consistency constraints for target design variables.
Returns:
Vector of weights for target design variables, or None if not set.
"""
# copy.copy() used - List[float] is mutable. This prevents modifications
# in the caller from being reflected back to the class attribute.
return copy.copy(self._weights_copyshareddesignvariable_minus_targetshareddesignvariable)
def CopyFromMiddleLevelCoupling(self, middlelevelcouplingIn: MiddleLevelCouplingPC) -> None:
"""Copy coupling data from a MiddleLevelCoupling object.
Args:
middlelevelcouplingIn: The MiddleLevelCoupling object to copy data from.
Raises:
ValueError: If middlelevelcouplingIn is not a MiddleLevelCoupling instance.
"""
if not isinstance(middlelevelcouplingIn, MiddleLevelCouplingPC):
raise ValueError(f"{DDO_Color}Error in CouplingParametersPC.CopyFromMiddleLevelCoupling(): type of MiddleLevelCoupling is not matching.{Reset}")
if middlelevelcouplingIn.get_MappedResponses() is not None:
self.set_Copy_MappedResponses(middlelevelcouplingIn.get_MappedResponses())
if middlelevelcouplingIn.get_CouplingVariable() is not None:
self.set_Copy_CouplingVariable(middlelevelcouplingIn.get_CouplingVariable())
if middlelevelcouplingIn.get_SharedDesignVariable() is not None:
self.set_Copy_SharedDesignVariables(middlelevelcouplingIn.get_SharedDesignVariable())
if middlelevelcouplingIn.get_TargetSharedDesignVariable() is not None:
self.set_Copy_TargetSharedDesignVariables(middlelevelcouplingIn.get_TargetSharedDesignVariable())
def CopyToMiddleLevelCoupling(self, middlelevelcouplingIn: MiddleLevelCouplingPC) -> None:
"""Copy coupling data to a MiddleLevelCoupling object.
Args:
middlelevelcouplingIn: The MiddleLevelCoupling object to copy data to.
Raises:
ValueError: If middlelevelcouplingIn is not a MiddleLevelCoupling instance.
"""
if not isinstance(middlelevelcouplingIn, MiddleLevelCouplingPC):
raise ValueError(f"{DDO_Color}Error in CouplingParametersPC.CopyToMiddleLevelCoupling(): type of MiddleLevelCoupling is not matching.{Reset}")
# No copy.copy() wrapper needed - the getter methods already return defensive copies,
# and the setter methods also create defensive copies of the input.
if self.get_MappedResponses() is not None:
middlelevelcouplingIn.set_MappedResponses(self.get_MappedResponses())
if self.get_CouplingVariable() is not None:
middlelevelcouplingIn.set_CouplingVariable(self.get_CouplingVariable())
if self.get_SharedDesignVariables() is not None:
middlelevelcouplingIn.set_SharedDesignVariable(self.get_SharedDesignVariables())
if self.get_TargetSharedDesignVariables() is not None:
middlelevelcouplingIn.set_TargetSharedDesignVariable(self.get_TargetSharedDesignVariables())
def update_state(self, other_coupling: 'CouplingParametersPC') -> None:
"""Update the state of this CouplingParametersPC with values from another instance.
This method is necessary for multiprocessing. When subsystems are executed in parallel
using multiprocessing.Pool, they are serialized and deserialized, creating new objects
in separate memory spaces. After parallel execution completes, this method updates
the original object's attribute values while preserving their memory addresses.
The update preserves memory addresses by modifying list contents in-place rather than
reassigning references. This is essential for maintaining object identity across the
multiprocessing boundary.
Args:
other_coupling: The source CouplingParametersPC containing updated values
from parallel execution.
"""
# Call the base class update_state to handle inherited attributes
super().update_state(other_coupling=other_coupling)
# Update all List[float] | None attributes using update_state_listprimitive
self._weights_mappedresponse_minus_copycouplingvariable: List[float] | None = update_state_listprimitive(self._weights_mappedresponse_minus_copycouplingvariable, other_coupling.get_Weights_MappedResponse_Minus_CopyCouplingVariable())
self._weights_copymappedresponse_minus_couplingvariable: List[float] | None = update_state_listprimitive(self._weights_copymappedresponse_minus_couplingvariable, other_coupling.get_Weights_CopyMappedResponse_Minus_CouplingVariable())
self._weights_shareddesignvariable_minus_copytargetshareddesignvariable: List[float] | None = update_state_listprimitive(self._weights_shareddesignvariable_minus_copytargetshareddesignvariable, other_coupling.get_Weights_SharedDesignVariable_Minus_CopyTargetSharedDesignVariable())
self._weights_copyshareddesignvariable_minus_targetshareddesignvariable: List[float] | None = update_state_listprimitive(self._weights_copyshareddesignvariable_minus_targetshareddesignvariable, other_coupling.get_Weights_CopySharedDesignVariable_Minus_TargetSharedDesignVariable())