// Copyright © 2017 - ESI Group - Paul Bignier
//
// Lecture de fichier ‘out’ provenant d’OpenFOAM. Champs ‘X’, ‘Y’, ‘Z’ et ‘P’ considérés.

fd = mopen("out.txt");
str = mgetl(fd);

Xstr = str(grep(str, "Solving for Ux"));
XfinalRes = strstr(Xstr, "Final residual");
X = zeros(XfinalRes);
for i=1:size(XfinalRes,"r")
    X(i, 1) = strtod(part(XfinalRes(i), 18:strcspn(XfinalRes(i),',')));
end
Ystr = str(grep(str, "Solving for Uy"));
YfinalRes = strstr(Ystr, "Final residual");
Y = zeros(YfinalRes);
for i=1:size(YfinalRes,"r")
    Y(i, 1) = strtod(part(YfinalRes(i), 18:strcspn(YfinalRes(i),',')));
end
Zstr = str(grep(str, "Solving for Uz"));
ZfinalRes = strstr(Zstr, "Final residual");
Z = zeros(ZfinalRes);
for i=1:size(ZfinalRes,"r")
    Z(i, 1) = strtod(part(ZfinalRes(i), 18:strcspn(ZfinalRes(i),',')));
end
//Tstr = str(grep(str, "Solving for T"));
//TfinalRes = strstr(Tstr, "Final residual");
//T = zeros(TfinalRes);
//for i=1:size(TfinalRes,"r")
//    T(i, 1) = strtod(part(TfinalRes(i), 18:strcspn(TfinalRes(i),',')));
//end
Pstr = str(grep(str, "Solving for p"));
PfinalRes = strstr(Pstr, "Final residual");
P = zeros(PfinalRes);
for i=1:size(PfinalRes,"r")
    P(i, 1) = strtod(part(PfinalRes(i), 18:strcspn(PfinalRes(i),',')));
end

plot(X)
plot(Y, "g")
plot(Z, "r")
//plot(T, "y")
plot(P, "black")
a = gca(); a.log_flags = "nln";
xtitle("Residuals");
xlabel("Iteration");
ylabel("Residual");
legends(["X" "Y" "Z" "P"], [2 15 5 1], opt="ur");

mclose(fd);
