# ODE model def ds(S: np.ndarray, t: float): dS = np.zeros_like(S) X = S[::2] V = S[1::2] dS[0::2] = V dS[1::2] = forces.eval(t, X, V) / mass return dS # initial state s0 = np.array([ 0.1, 0.2, 0.0, 1.5, 0.0, 0.0 # x, vx, y, vy, z, vz ]) time = np.linspace(0, 10, 2000) states = odeint(ds, s0, time) mayavi_plot_surfaces([SurfacePlot(trajectory=states[:, ::2], animate=True)]) mlab.view(0.0, 90.0, 5.0)
from gsurface import Plan, SurfaceGuidedMassSystem, build_s0 from gsurface.forces import CenterDirectedViscousFriction, ConstantDirectedViscousFriction from gsurface.forces import SpringDampingForce from gsurface.plotter import mayavi_plot_surfaces, SurfacePlot, Tyi # simulate a 2D spring damping system, we notice that the radial speed tends to 0 where rotational speed still constant plan = Plan(a=0.0, c=1.0) surface = plan.build_surface(*plan.mesh(50, 50)) model = SurfaceGuidedMassSystem(surface=plan, s0=build_s0(u0=0.5, du0=0.0, dv0=5), m=1.0, forces=[ ConstantDirectedViscousFriction( 1.0, np.array([1.0, 0.0, 0.0])), CenterDirectedViscousFriction(1.0), SpringDampingForce(100.0, 1.0, None, 1.0) ]) time = np.linspace(0, 10, 3000) data = model.solve(time) solutions = model.solutions(data, time) mayavi_plot_surfaces([SurfacePlot(surface, trajectory=solutions[Tyi])]) from gsurface.serialize import save_attached model_parsed = save_attached(model, __file__)
joint_sim = SurfaceGuidedInteractedMassSystems([ SurfaceGuidedMassSystem(surface=tore, s0=build_s0(0, 2 * j, 0.0, 1 + j), m=1.0, forces=[ ViscousFriction(0.2), Gravity(1.0, np.array([0.0, 0.0, -1.0])) ]) for j in range(SYSTEMS) ], {(i, i + 1): SpringInteraction(1.0) for i in range(SYSTEMS - 1)}) time = np.linspace(0, 25, 2000) data = joint_sim.solve(time) solutions = joint_sim.solutions(data, time) colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0), (1, 0, 1), (0, 1, 0), (0, 1, 1), (1, 1, 1)] NCOLORS = len(colors) mayavi_plot_surfaces([ SurfacePlot( tore_mesh, # surface i == 0, trajectory=solution[Tyi], trajectoryColor=colors[i % NCOLORS], showTrajectory=True) for i, solution in enumerate(solutions) ])
import numpy as np from gsurface import SurfaceGuidedMassSystem, build_s0 from gsurface.forces import Gravity, SpringForce, ViscousFriction from gsurface.plotter import mayavi_plot_surfaces, SurfacePlot, Tyi from gsurface.surface import Cylinder mass = 1.0 # kg system = SurfaceGuidedMassSystem( surface=Cylinder(R=1.0).setlims(v_ll=-1, v_ul=1), solid=mass, forces=Gravity(mass, g=np.array([0.0, 0.0, -1.0])) + SpringForce(stiffness=1.0, clip=np.array([-0.5, 0.0, 0.0])) + ViscousFriction(0.1), s0=build_s0(0, 0.5, 0, 0), ) time = np.linspace(0, 50.0, 1000) data = system.solve(time) solutions = system.solutions(data, time) smesh = system.surface.build_surface(*system.surface.mesh(40, 40)) mayavi_plot_surfaces( [SurfacePlot(smesh, showSurface=True, trajectory=solutions[Tyi])])
# check surface definition print(para, para.check(20, 20)) # create model model = SurfaceGuidedMassSystem(surface=para, s0=s0, m=1.0, forces=[ Gravity(m=1.0, g=np.array([0.0, 0.0, -2])), ViscousFriction(mu=0.5) ]) # solve time = np.linspace(0, 20, 2000) data = model.solve(time) # calculate other physics quantities solutions = model.solutions(data, time) trajectory = solutions[Tyi] # plot matplotlib_plot_solutions(time, data, solutions) mayavi_plot_surfaces( [SurfacePlot(surface, trajectory=trajectory, animate=True)]) mlab.view(0, 30, 5.0, np.zeros((3, )))
from gsurface import Tyi from gsurface.advanced.structure import SurfaceGuidedStructureSystem, TriangleStructure from gsurface.forces import ViscousFriction from gsurface.plotter import mayavi_plot_surfaces, SurfacePlot, mlab from gsurface.surface import Plan surface = Plan(0.0, 0.0, 1.0) mesh = surface.build_surface(*surface.mesh(50, 50)) structure = TriangleStructure(totalMass=4.0, stiffness=1000.0, mu=10.0, l0=0.5) structure[1, 2].l0 = 0.8 structure.nodes[0].mass = 100.0 model = SurfaceGuidedStructureSystem(surface, structure, structureForces=[ViscousFriction(1.0)]) time = np.linspace(0, 2, 1000) states = model.solve(time) solutions = list(model.solutions(states, time)) mayavi_plot_surfaces([ SurfacePlot(mesh, showSurface=(i == 0), trajectory=solutions[i][Tyi][::5]) for i in range(model.degree) ]) mlab.view(0, 0, 5.0, np.zeros((3, )))
surface = Tore(0.5, 1.0) mesh = surface.build_surface(*surface.mesh(50, 50)) snake = BowStructure(SIZE, 1.0, stiffness=1000.0, mu=10.0, l0=1.0) snake.interactions[(0, SIZE - 1)].stiffness = 100.0 sim = SurfaceGuidedStructureSystem(surface, snake, s0=nprand(4*SIZE), structureForces=[ ViscousFriction(0.01) ]) sim.s0[1::4] = 1.0 time = np.linspace(0, 40, 1000) data = sim.solve(time) solutions = sim.solutions(data, time) model_parsed = save_attached(sim, __file__) mayavi_plot_surfaces([ SurfacePlot( smesh=mesh, showSurface= i == 0, trajectory=solution[Tyi], trajectoryColor=rgb[i % len(rgb)], showTrajectory=True ) for i, solution in enumerate(solutions) ])
from gsurface import SurfaceGuidedMassSystem, Tyi, build_s0 from gsurface.forces import ViscousFriction, SpringForce model = SurfaceGuidedMassSystem( surface=surface, s0=build_s0(u0=2.5, du0=-8.0, dv0=5.0), m=1.0, forces=[ SpringForce(10.0), ViscousFriction(1.0) ] ) from gsurface.plotter import mayavi_plot_surfaces, SurfacePlot surface = NewSurface() surface_mesh = surface.build_surface(*surface.mesh(50, 50)) time = np.linspace(0, 10, 1000) data = model.solve(time) solutions = model.solutions(data, time) mayavi_plot_surfaces([ SurfacePlot(surface_mesh, trajectory=solutions[Tyi]) ])
# plan = Plan.from_rotations(0.0, np.pi, 0.0) # equivalent définition plan.shiftvector = np.array([0.0, 0.0, 1.0]) surface = plan.build_surface(*plan.mesh(50, 50)) model = SurfaceGuidedMassSystem(surface=plan, s0=build_s0(0.5, 0.0, 0.0, 0.0), m=1.0, forces=[ Gravity(m=1.0, g=np.array([0.0, 0.0, -10.0])), SpringDampingForce(stiffness=1000.0, mu=10.0, clip=np.array( [0.0, 0.0, 0.0]), l0=0.5), ViscousFriction(mu=0.5) ]) time = np.linspace(0, 10, 1000) states = model.solve(time) solutions = model.solutions(states, time) mayavi_plot_surfaces([ SurfacePlot(surface, trajectory=solutions[Tyi]), ]) mlab.points3d(0.0, 0.0, 0.0, color=(0, 1, 0), scale_factor=0.05) mlab.view(0, 90, 6.0, [0.0, 0.0, 0.0])