def main(): code = ps.Axis(ps.Point(1, 1), 3, "x") code2 = ps.Axis(ps.Point(1, 1), 3, "y") model = ps.Composition(dict(x=code, y=code2)) fig = ps.Figure(0, 5, 0, 5, backend=MatplotlibBackend) fig.add(model) fig.show()
def main() -> None: d = make_dashpot(0) s = make_spring(0) M = ps.Rectangle(ps.Point(0, H), 4 * H, 4 * H).set_line_width(4) left_wall = ps.Rectangle(ps.Point(-L, 0), H / 10, L).set_fill_pattern( ps.Style.FillPattern.UP_LEFT_TO_RIGHT) ground = ps.Wall([ps.Point(-L / 2, 0), ps.Point(L, 0)], thickness=-H / 10) wheel1 = ps.Circle(ps.Point(H, H / 2), H / 2) wheel2 = wheel1.translate(ps.Point(2 * H, 0)) fontsize = 24 text_m = ps.Text("$m$", ps.Point(2 * H, H + 2 * H)) text_m.style.font_size = fontsize text_ku = ps.Text("$ku$", ps.Point(-L / 2, H + 4 * H)) text_ku.style.font_size = fontsize text_bv = ps.Text("$bu'$", ps.Point(-L / 2, H)) text_bv.style.font_size = fontsize x_axis = ps.Axis(ps.Point(2 * H, L), H, "$u(t)$") x_axis_start = ps.Line(ps.Point(2 * H, L - H / 4), ps.Point(2 * H, L + H / 4)).set_line_width(4) model = ps.Composition({ "spring": s, "mass": M, "left wall": left_wall, "ground": ground, "wheel1": wheel1, "wheel2": wheel2, "text_m": text_m, "text_ku": text_ku, "x_axis": x_axis, "x_axis_start": x_axis_start, }) fig = ps.Figure(-L, x_max, -1, L + H, backend=MatplotlibBackend) fig.add(model) damping = ps.Composition({"dashpot": d, "text_bv": text_bv}) # or fig = Composition(dict(fig=fig, dashpot=d, text_bv=text_bv)) fig.add(damping) fig.show()
u_points[i] = ps.Composition({"circle": circle, "u_point": text}) u_discrete = ps.Composition(u_points) i_lines = {} for i in range(1, len(t_mesh)): i_lines[i] = ps.Line(ps.Point(t_mesh[i - 1], u_values[i - 1]), ps.Point(t_mesh[i], u_values[i])).set_line_width(1) interpolant = ps.Composition(i_lines) x_axis_extent: float = t_mesh[-1] + 0.2 * t_axis_extent logging.info(x_axis_extent) axes = ps.Composition({ "x": ps.Axis( ps.Point(0.0, 0.0), x_axis_extent, "$t$", label_spacing=(1 / 45.0, -1 / 30.0), ), "y": ps.Axis(ps.Point(0.0, 0.0), 0.8 * u_max, "$u$", rotation_angle=np.pi / 2), }) h = 0.03 * u_max # tickmarks height i_nodes = {} for i, t in enumerate(t_mesh): i_nodes[i] = ps.Composition({ "node": ps.Line(ps.Point(t, h), ps.Point(t, -h)), "name": ps.Text("$t_%d$" % i, ps.Point(t, -3.5 * h)), })
def main() -> None: u = ps.SketchyFunc3() Nt = 5 t_mesh = np.linspace(0, 6, Nt + 1) # Add 20% space to the left and 30% to the right of the coordinate system t_axis_extent = t_mesh[-1] - t_mesh[0] logging.info(t_axis_extent) t_min = t_mesh[0] - 0.2 * t_axis_extent logging.info(t_min) t_max = t_mesh[-1] + 0.3 * t_axis_extent logging.info(t_max) u_max = 1.3 * max([u(t) for t in t_mesh]) logging.info(u_max) u_min = -0.2 * u_max logging.info(u_max) r = 0.005 * (t_max - t_min) # radius of circles placed at mesh points # import random; random.seed(12) perturbations = [0, 0.1, 0.1, 0.2, -0.4, -0.1] u_points = {} u_values = [] for i, t in enumerate(t_mesh): u_value = u(t) + perturbations[i] u_values.append(u_value) circle = ps.Circle(ps.Point(t, u_value), r).set_fill_color(ps.Style.Color.BLACK) text = ps.Text( "$u^%d$" % i, ps.Point(t, u_value) + (ps.Point(0.0, 3 * r) if i > 0 else ps.Point(-3 * r, 0.0)), ) u_points[i] = ps.Composition({"circle": circle, "u_point": text}) u_discrete = ps.Composition(u_points) i_lines = {} for i in range(1, len(t_mesh)): i_lines[i] = ps.Line(ps.Point(t_mesh[i - 1], u_values[i - 1]), ps.Point(t_mesh[i], u_values[i])).set_line_width(1) interpolant = ps.Composition(i_lines) x_axis_extent: float = t_mesh[-1] + 0.2 * t_axis_extent logging.info(x_axis_extent) axes = ps.Composition({ "x": ps.Axis( ps.Point(0.0, 0.0), x_axis_extent, "$t$", ), "y": ps.Axis(ps.Point(0.0, 0.0), 0.8 * u_max, "$u$", rotation_angle=np.pi / 2), }) h = 0.03 * u_max # tickmarks height i_nodes = {} for i, t in enumerate(t_mesh): i_nodes[i] = ps.Composition({ "node": ps.Line(ps.Point(t, h), ps.Point(t, -h)), "name": ps.Text("$t_%d$" % i, ps.Point(t, -3.5 * h)), }) nodes = ps.Composition(i_nodes) fig = ps.Figure(t_min, t_max, u_min, u_max, backend=MatplotlibBackend) # Draw t_mesh with discrete u points illustration = ps.Composition(dict( u=u_discrete, mesh=nodes, axes=axes, )) fig.erase() fig.add(illustration) fig.show() # Add exact u line (u is a Spline Shape that applies 500 intervals by default # for drawing the curve) exact = u.set_line_style(ps.Style.LineStyle.DASHED).set_line_width(1) fig.add(exact) fig.show() # Add linear interpolant fig.add(interpolant) fig.show() # Linear interpolant without exact, smooth line fig.erase() fig.add(illustration) fig.add(interpolant) fig.show()
import logging import pysketcher as ps from pysketcher.backend.matplotlib import MatplotlibBackend logging.basicConfig(level=logging.INFO) code = ps.Axis(ps.Point(1, 1), 3, "x") code2 = ps.Axis(ps.Point(1, 1), 3, "y") model = ps.Composition(dict(x=code, y=code2)) fig = ps.Figure(0, 5, 0, 5, backend=MatplotlibBackend) fig.add(model) fig.show()
def main(): u = ps.SketchyFunc3() t_mesh = np.linspace(0, 6, Nt + 1) t_mesh_staggered = np.linspace(0.5 * (t_mesh[0] + t_mesh[1]), 0.5 * (t_mesh[-2] + t_mesh[-1]), Nt) # Add 20% space to the left and 30% to the right of the coordinate system t_axis_extent = t_mesh[-1] - t_mesh[0] t_min = t_mesh[0] - 0.2 * t_axis_extent t_max = t_mesh[-1] + 0.3 * t_axis_extent u_max = 1.3 * max([u(t) for t in t_mesh]) u_min = -0.2 * u_max r = 0.005 * (t_max - t_min) # radius of circles placed at mesh Points u_discrete = ps.Composition({ i: ps.Composition( dict( circle=ps.Circle(ps.Point(t, u(t)), r).set_fill_color(ps.Style.Color.BLACK), u_Point=ps.Text( "$u_%d$" % i, ps.Point(t, u(t)) + (ps.Point(0, 5 * r) if i > 0 else ps.Point(-5 * r, 0)), ), )) for i, t in enumerate(t_mesh) }) # u' = v # v = u.smooth.derivative(n=1) v = ps.SketchyFunc4() v_discrete = ps.Composition({ i: ps.Composition( dict( circle=ps.Circle(ps.Point(t, v(t)), r).set_fill_color(ps.Style.Color.RED), v_Point=ps.Text( r"$v_{%d/2}$" % (2 * i + 1), ps.Point(t, v(t)) + (ps.Point(0, 5 * r)), ), )) for i, t in enumerate(t_mesh_staggered) }) axes = ps.Composition( dict( x=ps.Axis(ps.Point(0, 0), t_mesh[-1] + 0.2 * t_axis_extent, "$t$"), y=ps.Axis(ps.Point(0, 0), 0.8 * u_max, "$u,v$", rotation_angle=np.pi / 2), )) h = 0.03 * u_max # tickmarks height u_nodes = ps.Composition({ i: ps.Composition( dict( node=ps.Line(ps.Point(t, h), ps.Point(t, -h)), name=ps.Text("$t_%d$" % i, ps.Point(t, -3.5 * h)), )) for i, t in enumerate(t_mesh) }) v_nodes = ps.Composition({ i: ps.Composition( dict( node=ps.Line(ps.Point(t, h / 1.5), ps.Point( t, -h / 1.5)).set_line_color(ps.Style.Color.RED), name=ps.Text(r"$t_{%d/2}$" % (2 * i + 1), ps.Point(t, -3.5 * h)), )) for i, t in enumerate(t_mesh_staggered) }) illustration = ps.Composition( dict(u=u_discrete, v=v_discrete, u_mesh=u_nodes, v_mesh=v_nodes, axes=axes)) fig = ps.Figure(t_min, t_max, u_min, u_max, backend=MatplotlibBackend) # Staggered t mesh and u and v Points fig.add(illustration) fig.show() # Exact u line (u is a Spline Shape that applies 500 intervals by default # for drawing the curve) u_exact = u.set_line_style(ps.Style.LineStyle.DASHED).set_line_width(1) fig.add(u_exact) fig.show() # v = Curve(u.xcoor, v(u.xcoor)) t_mesh_staggered_fine = np.linspace(t_mesh_staggered[0], t_mesh_staggered[-1], 501) t_mesh_staggered_points = [ ps.Point(x, v(x)) for x in t_mesh_staggered_fine ] v_exact = (ps.Curve(t_mesh_staggered_points).set_line_style( ps.Style.LineStyle.DASHED).set_line_width(1)) fig.add(v_exact) fig.show()
M = ps.Rectangle(ps.Point(0, H), 4 * H, 4 * H).set_line_width(4) left_wall = ps.Rectangle(ps.Point(-L, 0), H / 10, L).set_fill_pattern( ps.Style.FillPattern.UP_LEFT_TO_RIGHT ) ground = ps.Wall([ps.Point(-L / 2, 0), ps.Point(L, 0)], thickness=-H / 10) wheel1 = ps.Circle(ps.Point(H, H / 2), H / 2) wheel2 = wheel1.translate(ps.Point(2 * H, 0)) fontsize = 24 text_m = ps.Text("$m$", ps.Point(2 * H, H + 2 * H)) text_m.style.font_size = fontsize text_ku = ps.Text("$ku$", ps.Point(-L / 2, H + 4 * H)) text_ku.style.font_size = fontsize text_bv = ps.Text("$bu'$", ps.Point(-L / 2, H)) text_bv.style.font_size = fontsize x_axis = ps.Axis(ps.Point(2 * H, L), H, "$u(t)$", label_spacing=(0.04, -0.01)) x_axis_start = ps.Line( ps.Point(2 * H, L - H / 4), ps.Point(2 * H, L + H / 4) ).set_line_width(4) model = ps.Composition( { "spring": s, "mass": M, "left wall": left_wall, "ground": ground, "wheel1": wheel1, "wheel2": wheel2, "text_m": text_m, "text_ku": text_ku, "x_axis": x_axis,
dict( circle=ps.Circle(ps.Point(t, v(t)), r).set_fill_color(ps.Style.Color.RED), v_Point=ps.Text( r"$v_{%d/2}$" % (2 * i + 1), ps.Point(t, v(t)) + (ps.Point(0, 5 * r)), ), )) for i, t in enumerate(t_mesh_staggered) }) axes = ps.Composition( dict( x=ps.Axis( ps.Point(0, 0), t_mesh[-1] + 0.2 * t_axis_extent, "$t$", label_spacing=(1 / 45.0, -1 / 30.0), ), y=ps.Axis(ps.Point(0, 0), 0.8 * u_max, "$u,v$", rotation_angle=np.pi / 2), )) h = 0.03 * u_max # tickmarks height u_nodes = ps.Composition({ i: ps.Composition( dict( node=ps.Line(ps.Point(t, h), ps.Point(t, -h)), name=ps.Text("$t_%d$" % i, ps.Point(t, -3.5 * h)), ))