ODE Event Detection in Parfor loop

조회 수: 12 (최근 30일)
James Penrod
James Penrod 2019년 8월 28일
댓글: Edric Ellis 2019년 8월 30일
I have an ODE45 function that uses an event detection function, and normally it works great! But, I'm trying to parallelize the process and putting the ode45 call inside the parfor loop fails to record the events. My basic code is below. When I use parfor rather than a simple for loop, the .te, .ye, and .ie fields of my struct are all blank (but are correctly populated with "for"), while the .y, and .t fields are fine.
State(1:100) = struct;
parfor i = 1:length(State)
tspan = 1:1000;
Opt = odeset('RelTol',1e-10,'AbsTol',1e-10,'Events',@myEvent);
[t,y,te,ye,ie] = ode45(@myOde, tspan, Y0, Opt);
State(i).t = t;
State(i).y = y;
State(i).te = te;
State(i).ye = ye;
State(i).ie = ie;
end
Is there a way to use Event Detection in a parfor loop? Am I just initializing something wrong, or do I need to try something other than parfor?
  댓글 수: 1
Edric Ellis
Edric Ellis 2019년 8월 29일
Your example is not executable, so I can't work out why this isn't working - please could you set this up as a "Minimal, Reproducible Example".

댓글을 달려면 로그인하십시오.

답변 (1개)

James Penrod
James Penrod 2019년 8월 29일
I've managed to resolve this myself, while trying to set up the Mininal, Reproducible Example (Thank you Edric for your time in pointing that out). The example I had come up with is below, and it appears that the issue was trying to use a global variable in the Event Function in the parfor loop. When I remove the "global R" from the event function and change "value = dist-R" to "value = dist-1" my final State struct is properly populated
State(1:10) = struct;
global R
R = 1;
parfor i = 1:length(State)
tspan = 1:10;
Y0 = [0;0;0;10;10;10;2;2;2;0;0;0];
Opt = odeset('RelTol',1e-10,'AbsTol',1e-10,'Events',@myEvent);
[t,y,te,ye,ie] = ode45(@myOde, tspan, Y0, Opt);
State(i).t = t;
State(i).y = y;
State(i).te = te;
State(i).ye = ye;
State(i).ie = ie;
end
function[xdot] = myOde(tsim,x0)
xdot = [x0(7:9);x0(10:12);zeros(6,1)];
end
function[value, isterminal, direction] = myEvent(t,y)
global R
X1 = y(1:3);
X2 = y(4:6);
dist = norm(X1-X2);
value = dist-R;
isterminal = 1;
direction = 0;
end
  댓글 수: 1
Edric Ellis
Edric Ellis 2019년 8월 30일
Glad you got it sorted (ah, the value of the "Minimal, Reproducible Example"!). As you have found out, global variables are not synchronized from client to workers, or across workers.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Parallel and Cloud에 대해 자세히 알아보기

태그

제품


릴리스

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by