Skip to content

How Define ODE With Piecewise Function? #52

@FFFxueGawaine

Description

@FFFxueGawaine

Excuse me, I want to solve a single DOF oscillator with a nonlinear spring, whose nonlinear stiffness is modelled as Piecewise Function. How I define ODE?

I try use sympy.Piecewise. While there were no errors and warnings reported, the results appeared to be wrong.

I appended a picture of the correct result calculated by scipy.solve_ivp. The ODE is

p = [1, 0.1, 100, 1000,  0]
def impact_1dof(t, x, p):
    return [x[1],
            1 / p[0] * ( - p[1] * x[1] - p[2] * x[0] - p[3] * (x[0] > p[4]) * (x[0] - p[4]))]

The reslut is correct as follow.

微信图片_20240628184310

Next is code using sunode, and its reslut.

I need your help, if you understand what's going on. THANKS.

 
def SDOF(t, y, p):
 
   
    # nonlinear_damper = pt.switch(y.x > p.delta1, p.cnl * y.v, 0.0)
    return {
        'x':  y.v,
        'v': -1/p.m*(p.k*y.x+p.c*y.v +p.knl* sp.Piecewise((0, y.x <= 0), (y.x, y.x > 0)))
    }


problem = SympyProblem(
    params={
        'm': (),
        'k': (),
        'c': (),
        'knl':(),
      
       
           },
    states={
        'x': (),
        'v': (),
    },
    rhs_sympy=SDOF,
    derivative_params=[
        ('m',),
        ('c',),
        ('k',),
        ('knl',),
         
    ],
)

solver = Solver(problem, sens_mode=None, solver='BDF')
tvals = np.linspace(0, 5,10000)
 
y0 = np.zeros((), dtype=problem.state_dtype)
y0['x'] = -0.1
y0['v'] = 0
# We can also specify the parameters by name:
solver.set_params_dict({
    'm': 1 ,
    'k': 100,
    'c': 0.1,
    'knl': 100,
    
})
output = solver.make_output_buffers(tvals)
solver.solve(t0=0, tvals=tvals, y0=y0, y_out=output)

# We can convert the solution to an xarray Dataset
solx= solver.as_xarray(tvals, output).solution_x 
solv = solver.as_xarray(tvals, output).solution_v
plt.figure()
plt.plot(solx,solv)

微信图片_20240628184830

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions