Skip to content

Commit

Permalink
Edit bounds dialog fixed, added showColoredMessage() to GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
mlauer154 committed Jan 29, 2024
1 parent 3c4ca06 commit 03678e7
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 388 deletions.
217 changes: 0 additions & 217 deletions pymead/core/constraint_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,220 +720,3 @@ def update_points(self, constraint: GeoCon, new_x: np.ndarray):

class OverConstrainedError(Exception):
pass


def main():
import matplotlib.pyplot as plt

p1 = Point(0.0, 0.0, "p1")
p2 = Point(0.3, 0.3, "p2")
p3 = Point(0.4, 0.6, "p3")
p4 = Point(0.8, -0.1, "p4")
p5 = Point(0.2, 0.6, "p5")
points = [p1, p2, p3, p4, p5]

original_x = [p.x().value() for p in points]
original_y = [p.y().value() for p in points]
plt.plot(original_x, original_y, ls="none", marker="o", color="indianred")
plt.gca().set_aspect("equal")

cnstr1 = DistanceConstraint(p1, p2, 1.0, "d1")
cnstr2 = DistanceConstraint(p2, p3, 0.8, "d2")
cnstr3 = DistanceConstraint(p2, p4, 0.4, "d3")
cnstr4 = DistanceConstraint(p1, p5, 1.2, "d4")
cnstr5 = DistanceConstraint(p3, p1, 0.6, "d5")
graph = ConstraintGraph()

graph.add_constraint(cnstr1)
graph.add_constraint(cnstr2)
graph.add_constraint(cnstr3)
graph.add_constraint(cnstr4)
graph.add_constraint(cnstr5)

print(f"{np.hypot(p1.x().value() - p2.x().value(), p1.y().value() - p2.y().value())}")
print(f"{np.hypot(p3.x().value() - p2.x().value(), p3.y().value() - p2.y().value())}")
print(f"{np.hypot(p1.x().value() - p3.x().value(), p1.y().value() - p3.y().value())}")

print(f"{p2 = }")

# new_x = [p.x().value() for p in points]
# new_y = [p.y().value() for p in points]
# plt.plot(new_x, new_y, ls="none", marker="s", color="mediumaquamarine", mfc="#aaaaaa88")
# plt.show()
#
# graph.move_point(p2, 0.8, 0.6)
#
# print(f"{np.hypot(p1.x().value() - p2.x().value(), p1.y().value() - p2.y().value())}")
# print(f"{np.hypot(p3.x().value() - p2.x().value(), p3.y().value() - p2.y().value())}")
# print(f"{np.hypot(p1.x().value() - p3.x().value(), p1.y().value() - p3.y().value())}")

new_x = [p.x().value() for p in points]
new_y = [p.y().value() for p in points]
plt.plot(original_x, original_y, ls="none", marker="o", color="indianred")
plt.plot(new_x, new_y, ls="none", marker="s", color="steelblue", mfc="#aaaaaa88")
plt.show()

pass


def main2():
import matplotlib.pyplot as plt
# Initialize the graph
g = ConstraintGraph()

# Add the points
p1 = Point(0.0, 0.0, "p1")
p2 = Point(0.4, 0.1, "p2")
p3 = Point(0.6, 0.0, "p3")
p4 = Point(0.2, 0.8, "p4")
p5 = Point(1.0, 1.3, "p5")
points = [p1, p2, p3, p4, p5]
for point in points:
g.add_point(point)

plt.plot([p.x().value() for p in points], [p.y().value() for p in points], ls="none", marker="o", mec="indianred", mfc="indianred", fillstyle="left", markersize=10)

# Add the constraints
d1 = DistanceConstraint(p1, p2, 0.2, "d1")
d2 = DistanceConstraint(p2, p4, 1.5, "d2")
d3 = DistanceConstraint(p2, p3, 0.5, "d3")
d4 = DistanceConstraint(p3, p5, 1.4, "d4")
perp3 = Perp3Constraint(p1, p2, p4, "L1")
parl = Parallel4Constraint(p2, p4, p3, p5, "//1")
aparl3 = AntiParallel3Constraint(p1, p2, p3, "ap1")
constraints = [d1, d2, d3, d4, perp3, parl, aparl3]
for constraint in constraints:
g.add_constraint(constraint)
plt.plot([p.x().value() for p in points], [p.y().value() for p in points], ls="none", marker="o",
mec="indianred", mfc="indianred", fillstyle="left", markersize=10)
plt.show()

d4.param().set_value(4.0)
d4.param().set_value(5.0)

plt.plot([p.x().value() for p in points], [p.y().value() for p in points], ls="none", marker="o",
mfc="steelblue", mec="steelblue", fillstyle="right", markersize=10)
for p in points:
plt.text(p.x().value() + 0.02, p.y().value() + 0.02, p.name())
plt.gca().set_aspect("equal")

plt.show()

labels = {p: p.name() for p in g.nodes}
subax1 = plt.subplot(121)
networkx.draw(g, with_labels=True, labels=labels)

# for node in networkx.dfs_preorder_nodes(g, source=p1):
# if isinstance(node, GeoCon):
# print(f"{node = }")

# cycle_basis = networkx.cycle_basis(g)
# # for c in cycle_basis:
# # print(f"{c = }")
# simple_cycles = networkx.chordless_cycles(g)
# for simple_cycle in simple_cycles:
# print(simple_cycle)

plt.show()
pass


def main3():
import matplotlib.pyplot as plt
# Initialize the graph
g = ConstraintGraph()

# Add the points
p1 = Point(0.0, 0.0, "p1")
p2 = Point(0.4, 0.1, "p2")
p3 = Point(0.6, 0.0, "p3")
points = [p1, p2, p3]
for point in points:
g.add_point(point)

plt.plot([p.x().value() for p in points], [p.y().value() for p in points], ls="none", marker="o", mec="indianred", mfc="indianred", fillstyle="left", markersize=10)

# Add the constraints
d1 = DistanceConstraint(p1, p2, 0.6, "d1")
d2 = DistanceConstraint(p2, p3, 1.0, "d2")
d3 = DistanceConstraint(p1, p3, 0.8, "d3")
constraints = [d1, d2, d3]
for constraint in constraints:
g.add_constraint(constraint)

plt.plot([p.x().value() for p in points], [p.y().value() for p in points], ls="none", marker="o", mfc="steelblue", mec="steelblue", fillstyle="right", markersize=10)
for p in points:
plt.text(p.x().value() + 0.02, p.y().value() + 0.02, p.name())
plt.gca().set_aspect("equal")

plt.show()


def main4():
import matplotlib.pyplot as plt
# Initialize the graph
g = ConstraintGraph()

# Add the points
p1 = Point(0.0, 0.0, "p1")
p2 = Point(1.0, 0.0, "p2")
p3 = Point(-0.05, 0.05, "p3")
# p4 = Point(0.5, 0.03, "p4")
points = [p1, p2, p3]
for point in points:
g.add_point(point)

plt.plot([p.x().value() for p in points], [p.y().value() for p in points], ls="none", marker="o", mec="indianred",
mfc="indianred", fillstyle="left", markersize=10)

# Add the constraints
d1 = DistanceConstraint(p1, p2, 1.0, "d1")
d2 = DistanceConstraint(p1, p3, 0.2, "d2")
pol = PointOnLineConstraint(p3, p1, p2, "pol1")
# d3 = DistanceConstraint(p1, p3, 0.8, "d3")
constraints = [d1, d2, pol]
for constraint in constraints:
g.add_constraint(constraint)

# d4.param.set_value(2.0)
# # g.compile_equation_for_entity_or_constraint(aparl3)
# x, info = g.initial_solve(d3)
# print(f"{info = }")
# g.update_points(aparl3, new_x=x)

plt.plot([p.x().value() for p in points], [p.y().value() for p in points], ls="none", marker="o", mfc="steelblue",
mec="steelblue", fillstyle="right", markersize=10)
for p in points:
plt.text(p.x().value() + 0.02, p.y().value() + 0.02, p.name())
plt.gca().set_aspect("equal")

plt.show()


def main5():
import matplotlib.pyplot as plt
g = ConstraintGraph()

# Add the points
p1 = Point(0.0, 0.0, "p1")
p2 = Point(0.4, 0.1, "p2")
p3 = Point(0.1, -0.1, "p3")
p4 = Point(0.5, -0.15, "p4")
points = [p1, p2, p3, p4]
for point in points:
g.add_point(point)

d1 = DistanceConstraint(p1, p2, 0.5, "d1")
d2 = DistanceConstraint(p3, p4, 0.55, "d2")
par = Parallel4Constraint(p1, p2, p3, p4, "par")
for constraint in [d1, d2, par]:
g.add_constraint(constraint)

plt.plot([p.x().value() for p in points], [p.y().value() for p in points], ls="none", marker="o", mfc="indianred")
plt.show()

g.remove_constraint(par)


if __name__ == "__main__":
main2()
11 changes: 8 additions & 3 deletions pymead/core/geometry_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


class GeometryCollection(DualRep):
def __init__(self):
def __init__(self, gui_obj=None):
"""
The geometry collection is the primary class in pymead for housing all the available fundamental geometry
types. Geometry, parameters, and constraints can be added using the nomenclature ``add_<object-name>()``.
Expand All @@ -39,6 +39,7 @@ def __init__(self):
"dims": {},
}
self.gcs = ConstraintGraph()
self.gui_obj = gui_obj
self.canvas = None
self.tree = None
self.selected_objects = {k: [] for k in self._container.keys()}
Expand Down Expand Up @@ -798,6 +799,10 @@ def add_constraint(self, constraint: GeoCon, assign_unique_name: bool = True, **
self.gcs.add_constraint(constraint, **constraint_kwargs)
except (OverConstrainedError, ValueError) as e:
self.remove_pymead_obj(constraint)
self.clear_selected_objects()
if self.gui_obj is not None:
self.gui_obj.showColoredMessage("Constraint cluster is over-constrained. Removing constraint...",
4000, "#eb4034")
raise e
return constraint

Expand Down Expand Up @@ -862,8 +867,8 @@ def get_metadata():
}

@classmethod
def set_from_dict_rep(cls, d: dict, canvas=None, tree=None):
geo_col = cls()
def set_from_dict_rep(cls, d: dict, canvas=None, tree=None, gui_obj=None):
geo_col = cls(gui_obj=gui_obj)
geo_col.canvas = canvas
geo_col.tree = tree
for name, desvar_dict in d["desvar"].items():
Expand Down
6 changes: 6 additions & 0 deletions pymead/core/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ def set_lower(self, lower: float):
if self._value is not None and self._value < self._lower:
self._value = self._lower

if self.tree_item is not None:
self.tree_item.treeWidget().itemWidget(self.tree_item, 1).setMinimum(self.lower())

def set_upper(self, upper: float):
"""
Sets the upper bound for the design variable. If called from outside ``DesVar.__init__()``, adjust the design
Expand All @@ -189,6 +192,9 @@ def set_upper(self, upper: float):
if self._value is not None and self._value > self._upper:
self._value = self._upper

if self.tree_item is not None:
self.tree_item.treeWidget().itemWidget(self.tree_item, 1).setMaximum(self.upper())

def get_dict_rep(self):
return {"value": float(self.value()) if self.dtype == "float" else int(self.value()),
"lower": self.lower(), "upper": self.upper(),
Expand Down
Loading

0 comments on commit 03678e7

Please sign in to comment.