/
loose.py
30 lines (26 loc) · 1.27 KB
/
loose.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"Implements Loose"
from .set import ConstraintSet
from ..small_scripts import appendsolwarning, initsolwarning
class Loose(ConstraintSet):
"ConstraintSet whose inequalities must result in an equality."
senstol = 1e-5
raiseerror = False
def __init__(self, constraints, *, senstol=None):
super().__init__(constraints)
self.senstol = senstol or self.senstol
def process_result(self, result):
"Checks that all constraints are satisfied with equality"
super().process_result(result)
initsolwarning(result, "Unexpectedly Tight Constraints")
for constraint in self.flat():
c_senss = result["sensitivities"]["constraints"].get(constraint, 0)
if c_senss >= self.senstol:
cstr = ("Constraint [ %.100s... %s %.100s... )"
% (constraint.left, constraint.oper, constraint.right))
msg = ("%s is not loose: it has a sensitivity of %+.4g."
" (Allowable sensitivity: %.4g)" %
(cstr, c_senss, self.senstol))
appendsolwarning(msg, (c_senss, constraint), result,
"Unexpectedly Tight Constraints")
if self.raiseerror:
raise RuntimeWarning(msg)