[BACK]Return to gnuplotf.rr CVS log [TXT][DIR] Up to [local] / OpenXM / src / asir-contrib / packages / src

File: [local] / OpenXM / src / asir-contrib / packages / src / gnuplotf.rr (download)

Revision 1.1, Sun Sep 28 01:31:18 2003 UTC (20 years, 8 months ago) by takayama
Branch: MAIN
CVS Tags: R_1_3_1-2, RELEASE_1_3_1_13b, RELEASE_1_2_3_12, RELEASE_1_2_3, RELEASE_1_2_2_KNOPPIX_b, RELEASE_1_2_2_KNOPPIX, KNOPPIX_2006, HEAD, DEB_REL_1_2_3-9

The module gnuplotf is an experimental module to call gnuplot
via file (not via TCP/IP).
If gnuplot.flush(n) is not called, gnuplot is not started.

/* $OpenXM: OpenXM/src/asir-contrib/packages/src/gnuplotf.rr,v 1.1 2003/09/28 01:31:18 takayama Exp $ */

module gnuplotf;

/* list of local functions */
localf start $
localf flush $
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 complexToVec $
localf goutput $

/* static variables */
static Com $
static Gnuplot_path $
static Gnuplot_file $
static Gnuplot_fn $
static Gnuplot_Roots$
static Heat_N $     /* Mesh $BJ,3d?t(B, Heat_N=5 */

Com = "" $
Gnuplot_path ="gnuplot4ox" $
Gnuplot_file = ".gnuplot-in.tmp" $
/* For heat() */
Heat_N = 20$     /* Mesh $BJ,3d?t(B, Heat_N=5 */

def start() {
  extern Xm_unix;
  /* BUG: Set gnuplot_path properly for windows. */
  return 1;
}

def gnuplot(S) {
   Com += S+"\n";
   /* if autopause, Com += "\npause 1;\n"; */
}

def flush(P) {
  Com += "\npause "+rtostr(P)+" ;\n";
  Fp = open_file(Gnuplot_file,"w");
  if (Fp < 0) error("Open error of the file "+Gnuplot_file);
  S = strtoascii(Com);
  for (I=0; I<length(S); I++) {
    put_byte(Fp,S[I]);
  }
  close_file(Fp);

  shell(Gnuplot_path+" <"+Gnuplot_file+" & ");
  A = Com;
  Com = "";
  return A;
}

def demo0() {
  for (I=0; I<500; I++) {
    gnuplot("plot cos(x+" + rtostr(I) + ");");
    gnuplot("pause 0.1;");
    if (I % 50 == 0) flush(1);
  }
}


def clean() {
  shell("rm -rf "+Gnuplot_file);
  shell("rm -rf "+Gnuplot_file+"[0-9]");
}

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) {
  if (type(C) == 0) { C = [ ] ; }
  check_arg_plotDots(C,Style,"Error in plot_dots");
  Tfile = Gnuplot_file+rtostr(Gnuplot_fn);
  Gnuplot_fn++;
  if (C == [ ]) {
    /* Clean the data file. */
    Fp=open_file(Tfile,"w");
    close_file(Fp);
    return 1;
  }else{
    Fp = open_file(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;
    A = strtoascii(A);
    for (I=0; I<length(A); I++) {
      put_byte(Fp,A[I]);
    }
    close_file(Fp);
    /* draw */
    if ( type(Style) != 7 ) {
       gnuplot(" plot '"+ Tfile + "' ; ");
       gnuplot("pause 1; ");
    }else{
       gnuplot(" plot '"+ Tfile + "' with " + Style + " ; ");
       gnuplot("pause 1; ");
    }
    return;
  }
}

def complexToVec(A) {
 return([real(A),imag(A)]);
}

/* plotRoots(x^4-1) */
def plotRoots(F) {
  B = vtol(pari(roots,F));
  B = map(complexToVec,B);
  print(B);
  plot_dots([ ],0);
  plot_dots(B,0);
  flush(5);
  return(B);
}


def demo2() {
  gnuplot(" plot sin(x); ");
  flush(1);
  for (I=0 ; I<10000; I++) {
     fctr(x^2-1);
  }
  plot_dots([[1.2, 3.4]],0);
  flush(1);
}

/** 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("$B;~4V:9J,(BK=",0); print(K);
  print("CFL$B>r7o(B 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("$B=i4|%Y%/%H%k(B: ",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() {
  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");
       return(0);
  }
  if (T == "x11") {
       print("Output device is set to X11")$
	   gnuplot("set terminal x11");
	   return(0);
  }
  gnuplot("set terminal postscript eps color");
  gnuplot("set output \""+T+"\"" );
  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=strtoascii(G);
    N = length(GS);
    GS = newvect(N,GS);
    GS2 = newvect(2*N);
    Times = strtoascii("*")[0];
    Power = strtoascii("^")[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) {
  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);
  }else {
    S = to_gnuplot_format(F);
    gnuplot("plot "+S);
  }
  return(0);
}

endmodule;

end$