1313# Local modules
1414from .pyOpt_MPI import MPI
1515from .pyOpt_constraint import Constraint
16- from .pyOpt_error import Error
1716from .pyOpt_objective import Objective
1817from .pyOpt_types import Dict1DType , Dict2DType , NumpyType
1918from .pyOpt_utils import (
@@ -232,13 +231,13 @@ def addVarGroup(
232231 self .finalized = False
233232 # Check that the nVars is > 0.
234233 if nVars < 1 :
235- raise Error (
234+ raise ValueError (
236235 f"The 'nVars' argument to addVarGroup must be greater than or equal to 1. The bad DV is { name } ."
237236 )
238237
239238 # Check that the type is ok
240239 if varType not in ["c" , "i" , "d" ]:
241- raise Error ("Type must be one of 'c' for continuous, 'i' for integer or 'd' for discrete." )
240+ raise ValueError ("Type must be one of 'c' for continuous, 'i' for integer or 'd' for discrete." )
242241
243242 value = _broadcast_to_array ("value" , value , nVars )
244243 lower = _broadcast_to_array ("lower" , lower , nVars , allow_none = True )
@@ -270,10 +269,10 @@ def addVarGroup(
270269 if name in self .variables :
271270 # Check that the variables happen to be the same
272271 if not len (self .variables [name ]) == len (varList ):
273- raise Error (f"The supplied name '{ name } ' for a variable group has already been used!" )
272+ raise KeyError (f"The supplied name '{ name } ' for a variable group has already been used!" )
274273 for i in range (len (varList )):
275274 if not varList [i ] == self .variables [name ][i ]:
276- raise Error (f"The supplied name '{ name } ' for a variable group has already been used!" )
275+ raise KeyError (f"The supplied name '{ name } ' for a variable group has already been used!" )
277276 # We we got here, we know that the variables we wanted to
278277 # add are **EXACTLY** the same so that's cool. We'll just
279278 # overwrite with the varList below.
@@ -459,7 +458,7 @@ def addConGroup(
459458 """
460459 self .finalized = False
461460 if name in self .constraints :
462- raise Error (f"The supplied name '{ name } ' for a constraint group has already been used." )
461+ raise KeyError (f"The supplied name '{ name } ' for a constraint group has already been used." )
463462
464463 # Simply add constraint object
465464 self .constraints [name ] = Constraint (name , nCon , linear , wrt , jac , lower , upper , scale )
@@ -550,7 +549,7 @@ def setDVsFromHistory(self, histFile, key=None):
550549 self .setDVs (hist [key ]["xuser" ])
551550 hist .close ()
552551 else :
553- raise Error (f"History file '{ histFile } ' not found!." )
552+ raise FileNotFoundError (f"History file '{ histFile } ' not found!." )
554553
555554 def printSparsity (self , verticalPrint = False ):
556555 """
@@ -1016,10 +1015,10 @@ def processXtoDict(self, x: ndarray) -> OrderedDict:
10161015 xg [dvGroup ] = x [..., istart ]
10171016 else :
10181017 xg [dvGroup ] = x [..., istart :iend ].copy ()
1019- except IndexError :
1020- raise Error ("Error processing x. There is a mismatch in the number of variables." )
1018+ except IndexError as e :
1019+ raise ValueError ("Error processing x. There is a mismatch in the number of variables." ) from e
10211020 if imax != self .ndvs :
1022- raise Error ("Error processing x. There is a mismatch in the number of variables." )
1021+ raise ValueError ("Error processing x. There is a mismatch in the number of variables." )
10231022 return xg
10241023
10251024 def processXtoVec (self , x : dict ) -> ndarray :
@@ -1053,10 +1052,10 @@ def processXtoVec(self, x: dict) -> ndarray:
10531052 x_array [..., istart ] = x [dvGroup ]
10541053 else :
10551054 x_array [..., istart :iend ] = x [dvGroup ]
1056- except IndexError :
1057- raise Error ("Error deprocessing x. There is a mismatch in the number of variables." )
1055+ except IndexError as e :
1056+ raise ValueError ("Error deprocessing x. There is a mismatch in the number of variables." ) from e
10581057 if imax != self .ndvs :
1059- raise Error ("Error deprocessing x. There is a mismatch in the number of variables." )
1058+ raise ValueError ("Error deprocessing x. There is a mismatch in the number of variables." )
10601059
10611060 return x_array
10621061
@@ -1084,13 +1083,13 @@ def processObjtoVec(self, funcs: Dict1DType, scaled: bool = True) -> NumpyType:
10841083 if objKey in funcs :
10851084 try :
10861085 f = np .squeeze (funcs [objKey ]).item ()
1087- except ValueError :
1088- raise Error (f"The objective return value, '{ objKey } ' must be a scalar!" )
1086+ except ValueError as e :
1087+ raise ValueError (f"The objective return value, '{ objKey } ' must be a scalar!" ) from e
10891088 # Store objective for printing later
10901089 self .objectives [objKey ].value = np .real (f )
10911090 fobj .append (f )
10921091 else :
1093- raise Error (f"The key for the objective, '{ objKey } ' was not found." )
1092+ raise KeyError (f"The key for the objective, '{ objKey } ' was not found." )
10941093
10951094 # scale the objective
10961095 if scaled :
@@ -1124,8 +1123,8 @@ def processObjtoDict(self, fobj_in: NumpyType, scaled: bool = True) -> Dict1DTyp
11241123 iObj = self .objectiveIdx [objKey ]
11251124 try :
11261125 fobj [objKey ] = fobj_in [iObj ]
1127- except IndexError :
1128- raise Error ("The input array shape is incorrect!" )
1126+ except IndexError as e :
1127+ raise ValueError ("The input array shape is incorrect!" ) from e
11291128 if scaled :
11301129 fobj = self ._mapObjtoOpt (fobj )
11311130 return fobj
@@ -1177,15 +1176,15 @@ def processContoVec(
11771176 if c .shape [- 1 ] == self .constraints [iCon ].ncon :
11781177 fcon [..., con .rs : con .re ] = c
11791178 else :
1180- raise Error (
1179+ raise ValueError (
11811180 f"{ len (fcon_in [iCon ])} constraint values were returned in { iCon } , "
11821181 + f"but expected { self .constraints [iCon ].ncon } ."
11831182 )
11841183
11851184 # Store constraint values for printing later
11861185 con .value = np .real (copy .copy (c ))
11871186 else :
1188- raise Error (f"No constraint values were found for the constraint '{ iCon } '." )
1187+ raise KeyError (f"No constraint values were found for the constraint '{ iCon } '." )
11891188
11901189 # Perform scaling on the original Jacobian:
11911190 if scaled :
@@ -1330,14 +1329,14 @@ def processObjectiveGradient(self, funcsSens: Dict2DType) -> NumpyType:
13301329 # Everything checks out so set:
13311330 gobj [iObj , ss [0 ] : ss [1 ]] = tmp
13321331 else :
1333- raise Error (
1332+ raise ValueError (
13341333 f"The shape of the objective derivative for dvGroup '{ dvGroup } ' is the incorrect length. "
13351334 + f"Expecting a shape of { (ss [1 ] - ss [0 ],)} but received a shape of { funcsSens [objKey ][dvGroup ].shape } ."
13361335 )
13371336 else :
1338- raise Error (f"The dvGroup key '{ dvGroup } ' is not valid" )
1337+ raise KeyError (f"The dvGroup key '{ dvGroup } ' is not valid" )
13391338 else :
1340- raise Error (f"The key for the objective gradient, '{ objKey } ', was not found." )
1339+ raise KeyError (f"The key for the objective gradient, '{ objKey } ', was not found." )
13411340 iObj += 1
13421341
13431342 # Note that we looped over the keys in funcsSens[objKey]
@@ -1420,21 +1419,20 @@ def processConstraintJacobian(self, gcon):
14201419 ndvs = ss [1 ] - ss [0 ]
14211420
14221421 gotDerivative = False
1423- try :
1424- if dvGroup in gcon [iCon ]:
1425- tmp = convertToCOO (gcon [iCon ][dvGroup ])
1426- gotDerivative = True
1427- except KeyError :
1428- raise Error (
1422+ if dvGroup in gcon [iCon ]:
1423+ tmp = convertToCOO (gcon [iCon ][dvGroup ])
1424+ gotDerivative = True
1425+ else :
1426+ raise KeyError (
14291427 f"The constraint Jacobian entry for '{ con .name } ' with respect to '{ dvGroup } ', "
14301428 + "as was defined in addConGroup(), was not found in constraint Jacobian dictionary provided."
14311429 )
14321430 if not gotDerivative :
14331431 # All keys for this constraint must be returned
14341432 # since the user has explicitly specified the wrt.
14351433 if not con .partialReturnOk :
1436- raise Error (
1437- f"Constraint '{ con .name } ' was expecting a jacobain with respect to dvGroup "
1434+ raise ValueError (
1435+ f"Constraint '{ con .name } ' was expecting a Jacobian with respect to dvGroup "
14381436 + f"'{ dvGroup } ' as was supplied in addConGroup(). "
14391437 + "This was not found in the constraint Jacobian dictionary"
14401438 )
@@ -1445,15 +1443,15 @@ def processConstraintJacobian(self, gcon):
14451443
14461444 # Now check that the Jacobian is the correct shape
14471445 if not (tmp ["shape" ][0 ] == con .ncon and tmp ["shape" ][1 ] == ndvs ):
1448- raise Error (
1446+ raise ValueError (
14491447 f"The shape of the supplied constraint Jacobian for constraint { con .name } with respect to { dvGroup } is incorrect. "
14501448 + f"Expected an array of shape ({ con .ncon } , { ndvs } ), but received an array of shape ({ tmp ['shape' ][0 ]} , { tmp ['shape' ][1 ]} )."
14511449 )
14521450
14531451 # Now check that supplied coo matrix has same length
14541452 # of data array
14551453 if len (tmp ["coo" ][2 ]) != len (con .jac [dvGroup ]["coo" ][2 ]):
1456- raise Error (
1454+ raise ValueError (
14571455 f"The number of nonzero elements for constraint group '{ con .name } ' with respect to { dvGroup } was not the correct size. "
14581456 + f"The supplied Jacobian has { len (tmp ['coo' ][2 ])} nonzero entries, but must contain { len (con .jac [dvGroup ]['coo' ][2 ])} nonzero entries."
14591457 )
0 commit comments