0
$\begingroup$

Hello there I am modeling a 3 mass spring system with the displacements between masses $\delta12$ and $\delta23$ are as follow. I want to partially evaluate the displacement $\delta12$ for the conditions given in Whenevent (when $\delta12$ = 0.05 and x1'(t)< x2'(t)< x3'(t) ) and then break the evaluation of $\delta12$ only when the conditions is satisfied but continue evaluation of NDSolve

m1 = 1;
m2 = 1;
m3 = 1;    
\[Delta]12 = (x1[t] - x2[t] + 0.1) ;
\[Delta]23 = (x2[t] - x3[t] + 0.1) ;
F12 = 100 \[Delta]12 + Sin[10 t];
F23 = 150 \[Delta]23 + Sin[10 t];
eq1 = (m1) x1''[t] == - F12;
eq2 = (m2 ) x2''[t] == F12 - F23;
eq3 = (m3 ) x3''[t] == F23;
des = {eq1, eq2, eq3};
ICs = {x1[0] == 0, x2[0] == 0.1, x3[0] == 0.2, x1'[0] == 0.4, 
x2'[0] == 0, x3'[0] == 0};
sol = NDSolve[{des, ICs, 
WhenEvent[(x1[t] - x2[t] + 0.1) == 0.1 && 
 x1'[t] < x2'[t] < x3'[t], Print[t]]}, {x1[t], x2[t], x3[t], 
x1'[t], x2'[t], x3'[t]}, {t, 0, 5}, MaxSteps -> Infinity, 
AccuracyGoal -> 5, 
Method -> {Automatic, "DiscontinuityProcessing" -> False}]
Plot[Evaluate[{x1'[t], x2'[t], x3'[t]} /. sol, {t, 0, 5}]]
Plot[Evaluate[{\[Delta]12, \[Delta]23} /. sol, {t, 0, 5}]]
Plot[Evaluate[{D[\[Delta]12, t], D[\[Delta]23, t]} /. sol, {t, 0, 5}]]
$\endgroup$
1
$\begingroup$

I'm not sure what the question is asking, but here is a way to get the solutions midway, at the time of the event (i.e. "break the evaluation"). I don't think it's possible to remove variables from the NDSolve integration, other than to stop integration and restart with a new command that picks up at the stopping point. So they will continue to be integrated, but they can be ignored.

Block[{
  m1 = 1,
  m2 = 1,
  m3 = 1,
  δ12 = (x1[t] - x2[t] + 0.1),
  δ23 = (x2[t] - x3[t] + 0.1),
  F12 = 100 δ12 + Sin[10 t],
  F23 = 150 δ23 + Sin[10 t],
  eq1 = (m1) x1''[t] == -F12,
  eq2 = (m2) x2''[t] == F12 - F23,
  eq3 = (m3) x3''[t] == F23},
 des = {eq1, eq2, eq3};
 ICs = {x1[0] == 0, x2[0] == 0.1, x3[0] == 0.2, x1'[0] == 0.4, 
   x2'[0] == 0, x3'[0] == 0};
 {sol} = NDSolve[{des, ICs, 
    WhenEvent[(x1[t] - x2[t] + 0.1) == 0.1 && x1'[t] < x2'[t] < x3'[t], 
     sol1 = NDSolve`ProcessSolutions[NDSolve`Self];  (* cull the current solutions *)
     "RemoveEvent"]},
   {x1, x2, x3, x1', x2', x3'}, {t, 0, 5}, 
   MaxSteps -> Infinity, AccuracyGoal -> 5, 
   Method -> {Automatic, "DiscontinuityProcessing" -> False}]
 ]
(*
  {{x1 -> InterpolatingFunction[{{0., 5.}}, <>], 
    ..., 
    Derivative[1][x3] -> InterpolatingFunction[{{0., 5.}}, <>]}}
*)

Here is the partial solution (where the event "break[s] the evaluation"):

sol1
(*
  {x1 -> InterpolatingFunction[{{0., 1.85508}}, <>], 
   ..., 
   Derivative[1][x3] -> InterpolatingFunction[{{0., 1.85508}}, <>]}
*)

NDSolve`Self refers to the NDSolve`StateData[] object representing the problem. One can read about such object in the tutorial Components and Data Structures.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.