Coverage for gpkit\constraints\tight.py: 0%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"Implements Tight"
2from .set import ConstraintSet
3from ..small_scripts import mag
4from ..small_scripts import appendsolwarning, initsolwarning
5from .. import SignomialsEnabled
8class Tight(ConstraintSet):
9 "ConstraintSet whose inequalities must result in an equality."
10 reltol = 1e-3
12 def __init__(self, constraints, *, reltol=None, **kwargs):
13 super().__init__(constraints)
14 self.reltol = reltol or self.reltol
15 self.__dict__.update(kwargs) # NOTE: for Berk's use in labelling
17 def process_result(self, result):
18 "Checks that all constraints are satisfied with equality"
19 super().process_result(result)
20 variables = result["variables"]
21 initsolwarning(result, "Unexpectedly Loose Constraints")
22 for constraint in self.flat():
23 with SignomialsEnabled():
24 leftval = constraint.left.sub(variables).value
25 rightval = constraint.right.sub(variables).value
26 rel_diff = mag(abs(1 - leftval/rightval))
27 if rel_diff >= self.reltol:
28 msg = ("Constraint [%.100s... %s %.100s...] is not tight:"
29 " the left hand side evaluated to %s but"
30 " the right hand side evaluated to %s"
31 " (Allowable error: %s%%, Actual error: %.2g%%)" %
32 (constraint.left, constraint.oper, constraint.right,
33 leftval, rightval,
34 self.reltol*100, mag(rel_diff)*100))
35 if hasattr(leftval, "magnitude"):
36 rightval = rightval.to(leftval.units).magnitude
37 leftval = leftval.magnitude
38 tightvalues = (leftval, constraint.oper, rightval)
39 appendsolwarning(msg, (rel_diff, tightvalues, constraint),
40 result, "Unexpectedly Loose Constraints")