def main() -> None: wheel1 = ( ps.Circle(ps.Point(w_1, R), R) .set_fill_color(ps.Style.Color.BLUE) .set_line_width(6) ) wheel2 = wheel1.translate(ps.Point(L, 0)) under = ps.Rectangle(ps.Point(w_1 - 2 * R, 2 * R), 2 * R + L + 2 * R, H) under.style.fill_color = ps.Style.Color.RED under.style.line_color = ps.Style.Color.RED over = ps.Rectangle(ps.Point(w_1, 2 * R + H), 2.5 * R, 1.25 * H).set_fill_color( ps.Style.Color.WHITE ) over.style.line_width = 14 over.style.line_color = ps.Style.Color.RED over.style.fill_pattern = ps.Style.FillPattern.UP_RIGHT_TO_LEFT ground = ps.Wall([ps.Point(w_1 - L, 0), ps.Point(w_1 + 3 * L, 0)], -0.3 * R) ground.style.fill_pattern = ps.Style.FillPattern.UP_LEFT_TO_RIGHT model = ps.Composition( { "wheel1": wheel1, "wheel2": wheel2, "under": under, "over": over, "ground": ground, } ) fig = ps.Figure( 0, w_1 + 2 * L + 3 * R, -1, 2 * R + 3 * H, backend=MatplotlibBackend ) fig.add(model) fig.show()
def main() -> None: c = ps.Point(w_1, R) wheel1 = ps.Circle(c, R) wheel2 = wheel1.translate(ps.Point(L, 0)) under = ps.Rectangle(ps.Point(w_1 - 2 * R, 2 * R), 2 * R + L + 2 * R, H) over = ps.Rectangle(ps.Point(w_1, 2 * R + H), 2.5 * R, 1.25 * H).set_fill_color(ps.Style.Color.WHITE) ground = ps.Wall( [ps.Point(w_1 - L, 0), ps.Point(w_1 + 3 * L, 0)], -0.3 * R) ground.style.fill_pattern = ps.Style.FillPattern.UP_RIGHT_TO_LEFT vehicle = ps.Composition({ "wheel1": wheel1, "wheel2": wheel2, "under": under, "over": over, "ground": ground, }) vehicle.style.line_color = ps.Style.Color.RED wheel1_dim = ps.LinearDimension("$w_1$", c + ps.Point(2, 0.25), c) hdp = w_1 + L + 3 * R # horizontal dimension position R_dim = ps.LinearDimension("$R$", ps.Point(hdp, 0), ps.Point(hdp, R)) H_dim = ps.LinearDimension("$H$", ps.Point(hdp, 2 * R), ps.Point(hdp, 2 * R + H)) H2_dim = ps.LinearDimension("$\\frac{5}{4}H$", ps.Point(hdp, 2 * R + H), ps.Point(hdp, 2 * R + (9 / 4) * H)) vdp = 2 * R + H + 3 / 2 * H R2_dim = ps.LinearDimension("$2R$", ps.Point(w_1 - 2 * R, vdp), ps.Point(w_1, vdp)) L_dim = ps.LinearDimension("$L$", ps.Point(w_1, vdp), ps.Point(w_1 + L, vdp)) R3_dim = ps.LinearDimension("$2R$", ps.Point(w_1 + L, vdp), ps.Point(w_1 + L + 2 * R, vdp)) dimensions = ps.Composition({ "wheel1_dim": wheel1_dim, "R_dim": R_dim, "H_dim": H_dim, "H2_dim": H2_dim, "R2_dim": R2_dim, "L_dim": L_dim, "R3_dim": R3_dim, }) model = ps.Composition({"vehicle": vehicle, "dimensions": dimensions}) figure = ps.Figure(0, w_1 + 2 * L + 3 * R, -1, 2 * R + 3 * H, backend=MatplotlibBackend) figure.add(model) figure.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()
P = ps.Point(W / 6, 0.85 * H) # rotation point a = 2 * np.pi / 9 # angle vertical = ps.Line(P, P - ps.Point(0, L)) path = ps.Arc(P, L, -np.pi / 2, a) theta = ps.ArcWithText(r"$\theta$", P, L / 4, -np.pi / 2, a, text_spacing=1 / 30.0) mass_pt = path.end rod = ps.Line(P, mass_pt) mass = ps.Circle(mass_pt, L / 20.0) rod_vec = rod.end - rod.start unit_rod_vec = rod_vec.unit_vector() mass_symbol = ps.Text("$m$", mass_pt + unit_rod_vec * (L / 10.0)) length = ps.DistanceWithText("$L$", P, mass_pt) # Displace length indication length = length.translate(rod_vec.normal() * (L / 15)) gravity = ps.Gravity(start=P + ps.Point(0.8 * L, 0), length=L / 3) def set_dashed_thin_blackline(*objects: ps.Shape): """Set linestyle of objects to dashed, black, width=1.""" for obj in objects: obj.set_line_style(ps.Style.LineStyle.DASHED)
def pendulum(theta, S, mg, drag) -> ps.Composition: """Draw a free body animation of a pendulum. params: theta: the angle from the vertical at which the pendulum is. S: the force exerted toward the pivot. mg: the force owing to gravity. drag: the force acting against the motion of the pendulum. return: A composition of the pendulum """ a = theta P = ps.Point(W / 2, 0.9 * H) # rotation point path = ps.Arc(P, L, -ps.Angle(np.pi / 2), a) mass_pt = path.end rod = ps.Line(P, mass_pt) theta = ps.AngularDimension(r"$\theta$", P + ps.Point(0, -L / 4), P + (mass_pt - P).unit_vector * (L / 4), P) theta.extension_lines = False mass = ps.Circle(mass_pt, L / 30.0).set_fill_color(ps.Style.Color.BLUE) rod_vec = rod.end - rod.start length = ps.LinearDimension("$L$", mass_pt, P) # Displace length indication length = length.translate(ps.Point(-np.cos(a), -np.sin(a)) * (L / 15.0)) length.style.line_width = 0.1 gravity_start = ps.Point(0.8 * L, 0) gravity = ps.Gravity(P + gravity_start, L / 3) dashed_thin_black_line = ps.Style() dashed_thin_black_line.line_style = ps.Style.LineStyle.DASHED dashed_thin_black_line.line_color = ps.Style.Color.BLACK dashed_thin_black_line.line_width = 1.0 path.style = dashed_thin_black_line vertical = ps.Line(rod.start, rod.start + ps.Point(0, -L)) vertical.style = dashed_thin_black_line rod.style = dashed_thin_black_line comp = ps.Composition({ "body": mass, "rod": rod, "vertical": vertical, "theta": theta, "path": path, "g": gravity, # "L": length, }) magnitude = 1.2 * L / 6 # length of a unit force in figure force = mg # constant (scaled eq: about 1) force *= magnitude mg_force = (ps.Force( "$mg$", mass_pt, mass_pt + ps.Point(0, 1) * force, text_position=ps.TextPosition.END, ) if force != 0 else None) force = S force *= magnitude rod_force = (ps.Force( "S", mass_pt, mass_pt - rod_vec.unit_vector * force, text_position=ps.TextPosition.END, ) if force != 0 else None) force = drag force *= magnitude air_force = (ps.Force( "", mass_pt, mass_pt - rod_vec.normal * force, ) if force != 0 else None) x0y0 = ps.Text("$(x_0,y_0)$", P + ps.Point(-0.4, -0.1)) ir = ps.Force( r"$\mathbf{i}_r$", P, P + rod_vec.unit_vector * (L / 10), text_position=ps.TextPosition.END, # spacing=ps.Point(0.015, 0) ) ith = ps.Force( r"$\mathbf{i}_{\theta}$", P, P + rod_vec.normal * (L / 10), text_position=ps.TextPosition.END, # spacing=ps.Point(0.02, 0.005) ) body_diagram = ps.Composition({ "mg": mg_force, "S": rod_force, "air": air_force, "ir": ir, "ith": ith, "origin": x0y0, }) comp = comp.merge(body_diagram) return comp
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
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 pysketcher as ps from pysketcher.backend.matplotlib import MatplotlibBackend R = 1 # radius of wheel L = 4 # distance between wheels H = 2 # height of vehicle body w_1 = 5 # position of front wheel # TODO : draw grids # drawing_tool.set_grid(True) c = ps.Point(w_1, R) wheel1 = ps.Circle(c, R) wheel2 = wheel1.translate(ps.Point(L, 0)) under = ps.Rectangle(ps.Point(w_1 - 2 * R, 2 * R), 2 * R + L + 2 * R, H) over = ps.Rectangle(ps.Point(w_1, 2 * R + H), 2.5 * R, 1.25 * H).set_fill_color(ps.Style.Color.WHITE) ground = ps.Wall([ps.Point(w_1 - L, 0), ps.Point(w_1 + 3 * L, 0)], -0.3 * R) ground.style.fill_pattern = ps.Style.FillPattern.UP_RIGHT_TO_LEFT vehicle = ps.Composition({ "wheel1": wheel1, "wheel2": wheel2, "under": under, "over": over, "ground": ground }) vehicle.style.line_color = ps.Style.Color.RED
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()
def make_spring(x): s_start = ps.Point(-L, 4 * H) s = ps.Spring(start=s_start, length=L + x, bar_length=3 * H / 2) s = s.rotate(-np.pi / 2, s_start) return s 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)$", 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(
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(
"""Illustrate forward, backward and centered finite differences in four figures.""" import pysketcher as ps from pysketcher.backend.matplotlib import MatplotlibBackend xaxis = 2 f = ps.SketchyFunc1("$u(t)$") x = 3 # center point where we want the derivative xb = 2 # x point used for backward difference xf = 4 # x point used for forward difference p = ps.Point(x, f(x)) # center point pf = ps.Point(xf, f(xf)) # forward point pb = ps.Point(xb, f(xb)) # backward point r = 0.1 # radius of circles placed at key points c = ps.Circle(p, r).set_line_color(ps.Style.Color.BLUE) cf = ps.Circle(pf, r).set_line_color(ps.Style.Color.RED) cb = ps.Circle(pb, r).set_line_color(ps.Style.Color.GREEN) # Points in the mesh p0 = ps.Point(x, xaxis) # center point pf0 = ps.Point(xf, xaxis) # forward point pb0 = ps.Point(xb, xaxis) # backward point tick = 0.05 axis = ps.Composition( { "hline": ps.Line(pf0 - ps.Point(3, 0), pb0 + ps.Point(3, 0)) .set_line_color(ps.Style.Color.BLACK) .set_line_width(1), "tick_m1": ps.Line(pf0 + ps.Point(0, tick), pf0 - ps.Point(0, tick))
import pysketcher as ps from pysketcher.backend.matplotlib import MatplotlibBackend R = 1 # radius of wheel L = 4 # distance between wheels H = 2 # height of vehicle body w_1 = 5 # position of front wheel wheel1 = (ps.Circle(ps.Point(w_1, R), R).set_fill_color(ps.Style.Color.BLUE).set_line_width(6)) wheel2 = wheel1.translate(ps.Point(L, 0)) under = ps.Rectangle(ps.Point(w_1 - 2 * R, 2 * R), 2 * R + L + 2 * R, H) under.style.fill_color = ps.Style.Color.RED under.style.line_color = ps.Style.Color.RED over = ps.Rectangle(ps.Point(w_1, 2 * R + H), 2.5 * R, 1.25 * H).set_fill_color(ps.Style.Color.WHITE) over.style.line_width = 14 over.style.line_color = ps.Style.Color.RED over.style.fill_pattern = ps.Style.FillPattern.UP_RIGHT_TO_LEFT ground = ps.Wall([ps.Point(w_1 - L, 0), ps.Point(w_1 + 3 * L, 0)], -0.3 * R) ground.style.fill_pattern = ps.Style.FillPattern.UP_LEFT_TO_RIGHT model = ps.Composition({ "wheel1": wheel1, "wheel2": wheel2, "under": under, "over": over, "ground": ground })