0

In Julia, I want to use DifferentialEquations.jl package to solve

\ddot{u} + f(u,\dot{u},p) = g(t)

where g(t) is in given as a vector of values at equidistanced instants of time t.

This situation is different from one in https://diffeq.sciml.ai/stable/tutorials/ode_example/ where the forcing function M(t) is continuous.

A solution to this situation is to interpolate g(t) as suggested in

solve system of ODEs with read in external forcing

However, I do not want to interpolate the forcing function g(t) but want to try callback command instead.

For a 2-order linear SDOF system subjected to ground motion, I already tried

using DifferentialEquations

rhs = rand(10); # ground accel.
deltat = 0.02;

function affect!(integrator)
  integrator.p[3] = rhseval(integrator.t)
end

cb = PeriodicCallback(affect!,deltat)

function rhseval(x)
    return rhs[floor(Int,x/deltat)+1]
end

function sdof!(du,u,p,t)
    omg = p[1]
    zeta = p[2]
    du[1] = u[2]
    du[2] = - omg^2 * u[1] - 2zeta * omg * u[2] - p[3]
end

disp0 = 0;
velo0 = 0;
u0 = [disp0, velo0];

tspan = (0.0,0.1);

p = [2pi,0.02,0];

prob = ODEProblem(sdof!,u0,tspan,p,callback = cb)
sol = solve(prob,abstol = 1e-8, reltol = 1e-8,saveat=0.02)

but the obtained results are unsatisfactory.

Is there other way, not to interpolate g(t), but to use callback, i.e PeriodicCallback, DiscreteCallback?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Chris
  • 93
  • 6
  • 1
    As you have `g(t)` in `p[3]`, should it not rather be `du[2] = ... + p[3]`? Does correcting this sign error give the expected results? – Lutz Lehmann Feb 26 '21 at 06:37
  • Thank you for your suggestion. I checked my post and found that I mistyped the equation of motion for an SDOF structure subjected to a ground motion. It should be corrected as \ddot{u} + f(u,\dot{u},p) = - g(t). However, the posted Julia code still presents correctly the above equation of motion. The obtained results are of big difference from ones in a book. Probably it is due to my code itself (wrong use of commands). – Chris Feb 27 '21 at 09:21
  • 1
    But did you try out the sign variants? The sign error might not be on your side. Also, using random numbers from a standard unit distribution might not be what was used in the book. // Your code in itself looks straight-forward, if you get no compilation errors it should do what you intend it to do. You could try to construct an artificial example where you know the exact solution. – Lutz Lehmann Feb 27 '21 at 09:27
  • Due to the limited space allowed for a comment, I will follow the conversation by answering my own question as below. – Chris Feb 27 '21 at 16:33
  • Is there an actual question left here or is it just user error? – Chris Rackauckas Feb 28 '21 at 11:45
  • 1
    It is just my error. I misunderstood the correct use of the package. From the help of Lutz Lehmann, I found that the code I posted before does not reflect the problem I wanted to solve. Since it would be better to assume linear piecewise variations of ground accelerations than constant piecewise ones, I have to revise my code following https://stackoverflow.com/questions/49428939/solve-system-of-odes-with-read-in-external-forcing. Thank you. – Chris Feb 28 '21 at 14:22

1 Answers1

0

This issues seems to have just been user error addressed in the comments.

Chris Rackauckas
  • 18,645
  • 3
  • 50
  • 81