Search code examples
cplotreal-timegnuplotusleep

gnuplot and usleep on C


i'm trying to do a realtime plot on Linux. Everything runs ok, it's just that when the program ends, there's a lot of CPU usage for a while. Besides, because i use a very short time on usleep (like 0.3 sec) i don't know why the bash shell is dominated by the gnuplot interface, even for others bash windows i try to open, so i cannot use ctrl+c to stop the program.

Is there any other function i could use instead of usleep? Or something i could correct to avoid the gnuplot dominance over the windows?

Thanks in advance and sorry for my bad english

I let the code here

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float s=10.;
float r=28.;
float b=8.0/3.0;
/* Definimos las funciones */
float f(float x,float y,float z){
    return s*(y-x);
}
float g(float x,float y,float z){
    return x*(r-z)-y;
}
float h(float x,float y,float z){
    return x*y-b*z;
}
FILE *output;
FILE *gp;

int main(){
    gp = popen("gnuplot -","w");
    output = fopen("lorenzgplot.dat","w");
    float t=0.; 
    float dt=0.01;
    float tf=30;
    float x=3.;
    float y=2.;
    float z=0.;
    float k1x,k1y,k1z, k2x,k2y,k2z,k3x,k3y,k3z,k4x,k4y,k4z;
    fprintf(output,"%f %f %f \n",x,y,z);
    fprintf(gp, "splot '/home/david/documents/lorenzgplot.dat' with lines \n");
/* Ahora Runge Kutta de orden 4 */  
    while(t<tf){
        /* RK4 paso 1 */
        k1x = f(x,y,z)*dt;
        k1y = g(x,y,z)*dt;
        k1z = h(x,y,z)*dt;
        /* RK4 paso 2 */
        k2x = f(x+0.5*k1x,y+0.5*k1y,z+0.5*k1z)*dt;
        k2y = g(x+0.5*k1x,y+0.5*k1y,z+0.5*k1z)*dt;
        k2z = h(x+0.5*k1x,y+0.5*k1y,z+0.5*k1z)*dt;
        /* RK4 paso 3 */
        k3x = f(x+0.5*k2x,y+0.5*k2y,z+0.5*k2z)*dt;
        k3y = g(x+0.5*k2x,y+0.5*k2y,z+0.5*k2z)*dt;
        k3z = h(x+0.5*k2x,y+0.5*k2y,z+0.5*k2z)*dt;
        /* RK4 paso 4 */
        k4x = f(x+k3x,y+k3y,z+k3z)*dt;
        k4y = g(x+k3x,y+k3y,z+k3z)*dt;
        k4z = h(x+k3x,y+k3y,z+k3z)*dt;
        /* Actualizamos las variables y el tiempo */
        x += (k1x/6.0 + k2x/3.0 + k3x/3.0 + k4x/6.0);
        y += (k1y/6.0 + k2y/3.0 + k3y/3.0 + k4y/6.0);
        z += (k1z/6.0 + k2z/3.0 + k3z/3.0 + k4z/6.0);
        /* finalmente escribimos sobre el archivo */

        fprintf(output,"%f %f %f \n",x,y,z);
        fflush(output); 
        usleep(100000);
        fprintf(gp, "replot \n");
        fflush(gp);
        t += dt;
    }
    fclose(gp);
    fclose(output);
    return 0;
}

Solution

  • consider:

    gp = popen("nice gnuplot -","w");
    

    will prevent gnuplot from eating your system somewhat, but have you thought of running your runge-kutta code before even starting gnuplot? Write output to a file then feed the file to gnuplot in a background job.

    e.g., this all goes int one script that you can nice and/or use with at if you want to.

    runge-kutta > rkfile.dat
    cat rkfile.dat | gnuplot - &
    wait
    

    at example:

    at -k now <<!
     cd /path/to/rk
     nice ./my_rkscript.sh
    !
    

    All of these are some of the ways to keep a job from eating your current interactive environment. This way a "window" is not tied up, so to speak. The example script needs redirection of stderr and some other improvements that you add if you want.