/* $OpenXM: OpenXM/src/asir-contrib/packages/src/gnuplot.rr,v 1.3 2010/08/03 10:32:43 takayama Exp $ */
/* Old: gnuplot. See Attic */
module gnuplot;
/* list of local functions */
localf get_Gnuplot_proc$
localf set_Gnuplot_proc$
localf find_proc $
localf start $
localf stop $
localf flush $
localf start_unix $
localf start_windows $
localf isAlive $
localf gnuplot $
localf demo0 $
localf clean $
localf check_arg_plotDots $
localf plotDots $
localf plot_dots $
localf plotRoots $
localf demo2 $
localf set_heat_N $
localf heat $
localf to_gnuplot_format_old $
localf to_gnuplot_format $
localf plot_function $
localf openfile $
localf closefile $
localf writeString $
localf complexToVec $
localf goutput $
localf initsm1 $
localf setenv $
#define PROCESS_MAX 20
localf init_Gnuplot_proc $
localf init_Gnuplot_proc$
localf init_Gnuplot_pid$
localf init_FileIsOpenInSm1$
localf init_Gnuplot_Roots$
localf init_Heat_N $
localf init_Gnuplot_callingMethod$
localf init_Plot_gnuplotexec$
/* static variables */
static Gnuplot_proc$
static Gnuplot_pid$
static FileIsOpenInSm1$
static Gnuplot_Roots$
static Heat_N $ /* Mesh $BJ,3d?t(B, Heat_N=5 */
static Gnuplot_callingMethod$
static Plot_gnuplotexec$
def init_Gnuplot_proc() {
Gnuplot_proc = -1$
}
init_Gnuplot_proc() $
def init_Gnuplot_pid() {
Gnuplot_pid = newvect(20) $
}
init_Gnuplot_pid() $
def init_FileIsOpenInSm1() {
FileIsOpenInSm1 = newvect(PROCESS_MAX)$
}
init_FileIsOpenInSm1() $
def init_Gnuplot_callingMethod() {
Gnuplot_callingMethod = -1$
}
init_Gnuplot_callingMethod() $
def init_Plot_gnuplotexec() {
Plot_gnuplotexec = ""$
}
init_Plot_gnuplotexec() $
/* For heat() */
def init_Heat_N() {
Heat_N = 20$ /* Mesh $BJ,3d?t(B, Heat_N=5 */
}
init_Heat_N() $
#define GNUPLOTL_FIND_PROC(P) P = getopt(proc);\
if (type(P) == -1) {\
P = find_proc();\
}
def get_Gnuplot_proc() {
return Gnuplot_proc;
}
def set_Gnuplot_proc(A) {
Gnuplot_proc = A;
}
def find_proc() {
/*! extern Gnuplot_proc; */
if (Gnuplot_proc == -1) {
A = ox_get_serverinfo();
/* Look for ox_sm1_for_gnuplot. Not yet written */
/* Start sm1 automatically if there is not ox_sm1 */
Gnuplot_proc = start();
}
return(Gnuplot_proc);
}
def start() {
extern Xm_unix;
if (ox_ostype()[0] == "windows" && Xm_unix == 0)
return start_windows(0);
else
return start_unix();
}
def flush(P) {
return P; /* do nothing. Compatibility for gnuplotf */
}
def setenv(Key,Value) {
if (Key == "gnuplot.callingMethod") {
Gnuplot_callingMethod = Value;
} else if (Key == "plot.gnuplotexec") {
Plot_gnuplotexec = Value;
}else {
error("Unknown Key --- gnuplot.callingMethod, plot.gnuplotexec");
}
}
def initsm1(P) {
sm1.sm1(P," [(parse) (gnuplot.sm1) pushfile] extension");
if (Gnuplot_callingMethod >= 0) {
sm1.sm1(P," /gnuplot.callingMethod "+rtostr(Gnuplot_callingMethod)+" def ");
}
if (Plot_gnuplotexec != "") {
sm1.sm1(P,Plot_gnuplotexec+" /plot.gnuplotexec set ");
}
}
def start_unix() {
/*! extern Gnuplot_pid; */
extern Xm_noX;
/*! extern Gnuplot_proc; */
extern Sm1_lib;
if (Xm_noX) {
P = ox_launch_nox(0,Sm1_lib+"/bin/ox_sm1_gnuplot");
}else{
P = ox_launch(0,Sm1_lib+"/bin/ox_sm1_gnuplot");
}
if (P < 0) error("ox_sm1_gnuplot is not found.");
if (P >= PROCESS_MAX) error("Too many process. Increase PROCESS_MAX in xm.");
T = 0;
if (T == -1) {
Gnuplot_pid[P] = -1;
print(" Output to stdout and a file for debugging. ");
}else{
if (Gnuplot_pid[P] == 0) {
initsm1(P);
sm1.sm1(P," plotstart.aux "); /* path for gnuplot_forox
is set to this function */
sm1.sm1(P," gnuplot.pid ");
Gnuplot_pid[P] = sm1.pop(P);
}
}
ox_check_errors(P);
Gnuplot_proc = P;
return(P);
}
def start_windows(S) {
extern Xm_debug_on_win_1;
if (!Xm_debug_on_win_1) {
error("The component gnuplot has not yet been implemented on Windows(native).");
}
}
def isAlive(P) {
/*! extern Gnuplot_pid; */
if (Gnuplot_pid[P] == -2) return(1);
if (Gnuplot_pid[P] == -1) return(0);
sm1.sm1(P," [(getchild)] extension ");
Gnuplot_pidList = sm1.pop(P);
N = length(Gnuplot_pidList);
for (I=0; I<N; I++) {
if (Gnuplot_pidList[I] == Gnuplot_pid[P]) return(1);
if (I == N-1) {
error("Error in isAlive. No gnuplot_forox");
}
}
}
def stop() {
if (Gnuplot_proc == -1) return 0;
GNUPLOTL_FIND_PROC(P) ;
sm1.sm1(P," plotstop.aux ");
ox_pop_cmo(P); /* Wait until plotstop.aux is done. */
return 0;
}
def gnuplot(S) {
/*! extern Gnuplot_pid; */
GNUPLOTL_FIND_PROC(P) ;
if (isAlive(P) == 1) {
if (Gnuplot_pid[P] == -1) {
print("$ "+S+" $"+" gnuplot pop ");
}else{
sm1.sm1(P,"$ "+S+" $"+" gnuplot pop ");
ox_check_errors2(P);
}
}else{
print("Gnuplot got an error by the previous command. Restarting gnuplot.");
sm1.sm1(P," plotstart.aux gnuplot.pid ");
Gnuplot_pid[P] = sm1.pop(P);
if (Gnuplot_pid[P] == 0) {
error("Gnuplot_pid is 0. We could not restart open gnuplot.");
}
print(["New Gnuplot_pid is ",Gnuplot_pid[P]]);
print("Trying your command again.");
return(gnuplot(S|proc=P));
}
}
def demo0() {
print(" Type in ctrl-C, t, y, ox_reset(0); [not ox_sync(0);] ");
for (I=0; I<500; I++) {
gnuplot("plot cos(x+" + rtostr(I) + ");");
}
}
def openfile(P,F,Mode) {
/*! extern FileIsOpenInSm1; */
sm1.sm1(P,"("+F+")" + " (" + Mode + ") file /asir.fp set ");
sm1.sm1(P," [(PrintDollar) 0] system_variable ");
FileIsOpenInSm1[P] = 1;
}
def closefile(P) {
/*! extern FileIsOpenInSm1; */
sm1.sm1(P," asir.fp closefile ");
FileIsOpenInSm1[P] = 0;
}
def writeString(P,S) {
sm1.push_int0(P,S);
sm1.sm1(P," asir.fp 2 1 roll writestring ");
}
def clean() {
shell("/bin/rm -rf /tmp/gnuplot-data-tmp.*");
}
def check_arg_plotDots(C,Style,Fname) {
if (type(C) != 4) {
error(Fname+": example: gnuplot.plot_dots(0,[[1.2, 3.0], [3.4, 5]]<== list of lists,0)");
}
N = length(C);
for (I=0; I<N; I++) {
if (type(C[I]) != 4) {
error(Fname+": example: gnuplot.plot_dots(0,[[1.2, 3.0],<== list of two elements [3.4, 5]],0)");
}
if (length(C[I]) != 2) {
error(Fname+": example: gnuplot.plot_dots(0,[[1.2, 3.0],<== list of two elements [3.4, 5]],0)");
}
if (! (type(C[I][0]) == 0 || type(C[I][0]) == 1)) {
error(Fname+": example: gnuplot.plot_dots(0,[[1.2,<== give a number 3.0], [3.4, 5]],0)");
}
}
return(1);
}
/* For the backward compatibility */
def plotDots(C,Style) {
return(plot_dots(C,Style));
}
/* plot_dots(0,[ [1.2, 3.0], [34,24.5] ],0); */
def plot_dots(C,Style) {
/*! extern Gnuplot_Roots; */
/*! extern Gnuplot_pid; */
GNUPLOTL_FIND_PROC(P);
if (type(C) == 0) { C = [ ] ; }
check_arg_plotDots(C,Style,"Error in plot_dots");
Tfile = "/tmp/gnuplot-data-tmp."+rtostr(Gnuplot_pid[P])+".t";
if (C == [ ]) {
/* Clean the data file. */
openfile(P,Tfile,"w");
closefile(P);
return(sm1.pop(P));
}else{
openfile(P,Tfile,"a");
A = " ";
for (I=0; I<length(C); I++) {
A = A + rtostr(C[I][0])+ " "+ rtostr(C[I][1])+ "\n";
/* print(A); */
}
Gnuplot_Roots = C;
writeString(P,A);
closefile(P);
/* draw */
if ( type(Style) != 7 ) {
gnuplot(" plot '"+ Tfile + "' ; "|proc=P);
}else{
gnuplot(" plot '"+ Tfile + "' with " + Style + " ; "|proc=P);
}
return;
}
}
def complexToVec(A) {
return([real(A),imag(A)]);
}
/* plotRoots(x^4-1) */
def plotRoots(F) {
GNUPLOTL_FIND_PROC(P);
B = vtol(pari(roots,F));
B = map(complexToVec,B);
print(B);
plot_dots([ ],0|proc=P);
plot_dots(B,0|proc=P);
return(B);
}
def demo2() {
gnuplot(" plot sin(x); ");
/* Do I need a wait ? */
for (I=0 ; I<10000; I++) {
fctr(x^2-1);
}
/* Do I need to flush the sm1 socket? */
plot_dots([[1.2, 3.4]],0);
}
/** heat equation **/
/* Texts/text-96/A5/text/heat.asir */
/* 1996, 4/20 $BG.EAF3J}Dx<0$r:9J,K!$G2r$/!#(B */
/* $B=i4|>r7o$O(B a(x) = x (0<x<0.5 $B$NHO0O(B), 1-x ( 0.5<x<1 $B$NHO0O(B). */
/* K = 0.02; $B;~4V:9J,(B K$B$NNc!#(B */
/* M = 3; $B;~4V%9%F%C%W?t(B M $B$NNc!#(B */
/* heat(0.02,3); $B$H<B9T$9$k!#(B*/
/* Ex: Heat_N=20; heat(0.001,30); CFL = 0.4 */
/* Ex: Heat_N=20; heat(0.003,30); CFL > 0.5 unstable */
def set_heat_N(A) {
Heat_N = A;
}
def heat(K,M) {
/*! extern Heat_N ; */
H = 1.0/Heat_N; /* $B6u4V:9J,(B */
print("Time difference K=",0); print(K);
print("CFL condition K/(H*H) (<= 0.5): ",0); print(K/(H*H));
A = newvect(Heat_N+1);
B = newvect(Heat_N+1);
A[0] = 0; A[Heat_N] = 0;
/* $B=i4|>r7o$N@_Dj(B */
for (Q=1; Q<Heat_N; Q++) {
if (Q <= idiv(Heat_N,2)) {
A[Q] = H*Q;
}else{
A[Q] = 1-H*Q;
}
}
print("Initial vector: ",0); print(A);
plot_dots([],0);
for (P=1; P<=M; P++) {
B[0] = 0; B[Heat_N]=0;
for (Q=1; Q<Heat_N; Q++) {
B[Q] = A[Q] + (K/(H*H))*(A[Q+1]-2*A[Q]+A[Q-1]);
}
print("Time=",0); print(P*K,0); print(": ",0); print(B);
/* code for DISPLAY */
C = [ ];
for (I=0; I<Heat_N+1; I++) {
C = append(C,[[I,B[I]]]);
}
/*plot_dots([ ],0); */
plot_dots(C,"lines");
/* or plot_dots(C,0); */
/*-------------------*/
A = B;
}
}
def goutput() {
GNUPLOTL_FIND_PROC(P) ;
T = getopt(file);
if (type(T) == -1 || type(T) != 7) {
print("Usage of goutput: gnuplot.goutput(|file=\"string\")")$
print(" gnuplot.goutput(|file=\"x11\")")$
print("Output device is set to X11")$
gnuplot("set terminal x11" | proc=P);
return(0);
}
if (T == "x11") {
print("Output device is set to X11")$
gnuplot("set terminal x11" | proc=P);
return(0);
}
gnuplot("set terminal postscript eps color"| prec=P);
gnuplot("set output \""+T+"\"" | prec=P);
print("Graphic output of GNUPLOT will be written to "+T+" as a Poscript file.");
return(0);
}
/* Replacing ^ by ** */
def to_gnuplot_format_old(F) {
G = rtostr(F);
GS=sm1.to_ascii_array(G);
N = length(GS);
GS = newvect(N,GS);
GS2 = newvect(2*N);
Times = sm1.to_ascii_array("*")[0];
Power = sm1.to_ascii_array("^")[0];
J = 0;
for (I=0; I<N; I++) {
GS2[J] = GS[I];
if (GS[I] == Power) {
GS2[J] = Times;
J++;
GS2[J] = Times;
}
J++;
}
/* print(GS2); */
return(sm1.from_ascii_array(vtol(GS2)));
}
/* Replacing ^ by ** */
def to_gnuplot_format(F) {
A = ctrl("fortran_output");
ctrl("fortran_output",1);
S = rtostr(F);
ctrl("fortran_output",A);
return(S);
}
def plot_function(F) {
GNUPLOTL_FIND_PROC(P) ;
if (type(F) == 4) { /* In case of list */
S = " ";
for (I=0; I<length(F)-1; I++) {
T = to_gnuplot_format(F[I]);
S = S+T+", ";
}
S = S+ to_gnuplot_format(F[length(F)-1]);
gnuplot("plot "+S | proc=P) ;
}else {
S = to_gnuplot_format(F);
gnuplot("plot "+S | proc=P) ;
}
return(0);
}
endmodule;
/* Added callbacks. */
add_handler("quit","gnuplot.stop")$
end$