/* this is the main integrator routine for phase-plane It takes the steps looks at the interrupts, plots and stores the data It also loads the delay stuff if required */ #include #include #include #include "xpplim.h" #include "struct.h" #include "phsplan.h" #define ESCAPE 27 #define FIRSTCOLOR 30 #define MAX_LEN_SBOX 25 #define PARAM 1 #define IC 2 #define BMAXCOL 20 #define VOLTERRA 6 #define BACKEUL 7 #define RKQS 8 #define STIFF 9 #define GEAR 5 extern double ShootIC[4][MAXODE]; extern int ShootICFlag; extern int ShootIndex; typedef struct { Window base,upper; Window find,up,down,pgup,pgdn,home,end,left,right; Window first,last,restore,write,get; Window load,repl,unrepl,table,addcol,delcol; Window main; Window label[BMAXCOL]; Window time; int dataflag; int col0,row0,ncol,nrow; int maxrow,maxcol; float **data; int istart,iend; } BROWSER; extern BROWSER my_browser; double atof(); extern int NMarkov,STOCH_FLAG; extern int color_total,SCALEY,DCURY,PSFlag; int DelayErr; extern Window main_win,draw_win; extern Display *display; double get_ivar(); double MyData[MAXODE],MyTime; int MyStart; extern int DelayFlag,DCURY,NKernel; int RANGE_FLAG; extern int PAR_FOL,SHOOT; extern char upar_names[200][11]; extern double last_ic[MAXODE]; double LastTime; extern double DELAY; extern int R_COL; extern int colorline[11]; extern (*rhs)(); int PSLineStyle; struct { char item[30]; int steps,shoot,col,movie; double plow,phigh; } eq_range; struct { char item[30]; int steps,reset,oldic,index,cycle,type,movie; double plow,phigh; } range; (*solver)(); init_range() { eq_range.col=-1; eq_range.shoot=0; eq_range.steps=10; eq_range.plow=0.0; eq_range.phigh=1.0; eq_range.movie=0; sprintf(eq_range.item,upar_names[0]); range.type=0; range.index=0; range.steps=20; range.plow=0.0; range.phigh=1.0; range.reset=1; range.oldic=1; range.cycle=0; range.movie=0; sprintf(range.item,uvar_names[0]); init_shoot_range(upar_names[0]); } set_up_eq_range() { static char *n[]={"Range over","Steps","Start","End", "Shoot (Y/N)", "Stability col","Movie (Y/N)"}; char values[7][MAX_LEN_SBOX]; int status,i; static char *yn[]={"N","Y"}; sprintf(values[0],"%s",eq_range.item); sprintf(values[1],"%d",eq_range.steps); sprintf(values[2],"%g",eq_range.plow); sprintf(values[3],"%g",eq_range.phigh); sprintf(values[4],"%s",yn[eq_range.shoot]); sprintf(values[5],"%d",eq_range.col); sprintf(values[6],"%s",yn[eq_range.movie]); status=do_string_box(7,7,1,"Range Equilibria",n,values,45); if(status!=0){ strcpy(eq_range.item,values[0]); i=find_user_name(PARAM,eq_range.item); if(i<0){ err_msg("No such parameter"); return(0); } eq_range.steps=atoi(values[1]); if(eq_range.steps<=0)eq_range.steps=10; eq_range.plow=atof(values[2]); eq_range.phigh=atof(values[3]); if(values[4][0]=='Y'||values[4][0]=='y')eq_range.shoot=1; else eq_range.shoot=0; if(values[6][0]=='Y'||values[6][0]=='y')eq_range.movie=1; else eq_range.movie=0; eq_range.col=atoi(values[5]); if(eq_range.col<=1||eq_range.col>(NEQ+1))eq_range.col=-1; return(1); } return(0); } cont_integ() { double tetemp; double *x; double dif; if(INFLAG==0||FFT!=0||HIST!=0)return; tetemp=TEND; wipe_rep(); data_back(); if(new_float("Continue until:",&tetemp)==-1)return; x=&MyData[0]; tetemp=fabs(tetemp); if(fabs(MyTime)>=tetemp)return; dif=tetemp-fabs(MyTime); /* TEND=tetemp; */ integrate(&MyTime,x,dif,DELTA_T,1,NJMP,&MyStart); ping(); refresh_browser(storind); } set_up_range() { static char *n[]={"Range over","Steps","Start","End", "Reset storage (Y/N)", "Use old ic's (Y/N)","Cycle color (Y/N)","Movie(Y/N)"}; char values[8][MAX_LEN_SBOX]; int status,i; static char *yn[]={"N","Y"}; sprintf(values[0],"%s",range.item); sprintf(values[1],"%d",range.steps); sprintf(values[2],"%g",range.plow); sprintf(values[3],"%g",range.phigh); sprintf(values[4],"%s",yn[range.reset]); sprintf(values[5],"%s",yn[range.oldic]); sprintf(values[6],"%s",yn[range.cycle]); sprintf(values[7],"%s",yn[range.movie]); status=do_string_box(8,8,1,"Range Integrate",n,values,45); if(status!=0){ strcpy(range.item,values[0]); i=find_user_name(PARAM,range.item); if(i>-1){ range.type=PARAM; range.index=i; } else { i=find_user_name(IC,range.item); if(i<=-1){ err_msg("No such name!"); return(0); } range.type=IC; range.index=i; } range.steps=atoi(values[1]); if(range.steps<=0)range.steps=10; range.plow=atof(values[2]); range.phigh=atof(values[3]); if(values[4][0]=='Y'||values[4][0]=='y')range.reset=1; else range.reset=0; if(values[5][0]=='Y'||values[5][0]=='y')range.oldic=1; else range.oldic=0; if(values[6][0]=='Y'||values[6][0]=='y')range.cycle=1; else range.cycle=0; if(values[7][0]=='Y'||values[7][0]=='y')range.movie=1; else range.movie=0; /* printf("%s %d %d %d (%d %d) %f %f ", range.item, range.steps, range.reset,range.oldic,range.type,range.index, range.plow,range.phigh);*/ RANGE_FLAG=1; return(1); } return(0); } do_eq_range(x) double *x; { double parlo,parhi,dpar,temp; int npar,stabcol,i,j,ierr; char bob[50]; float stabinfo; if(set_up_eq_range()==0)return; wipe_rep(); data_back(); parlo=eq_range.plow; parhi=eq_range.phigh; npar=eq_range.steps; dpar=(parhi-parlo)/(double)npar; stabcol=eq_range.col; storind=0; DelayErr=0; ENDSING=0; PAR_FOL=1; PAUSER=0; SHOOT=eq_range.shoot; reset_browser(); if(eq_range.movie)reset_film(); for(i=0;i<=npar;i++) { if(eq_range.movie)clr_scrn(); temp=parlo+dpar*(double)i; set_val(eq_range.item,temp); PAR_FOL=1; sprintf(bob,"%s=%.16g",eq_range.item,temp); bottom_msg(2,bob); if(DelayFlag) do_delay_sing(x,NEWT_ERR,EVEC_ERR,BOUND,EVEC_ITER, NODE,&ierr,&stabinfo); else do_sing(x,NEWT_ERR,EVEC_ERR,BOUND,EVEC_ITER, NODE,&ierr,&stabinfo); if(eq_range.movie) if(film_clip()==0)err_msg("Out of film"); storage[0][storind]=temp; for(j=0;j0)storage[stabcol-1][storind]=stabinfo; storind++; if(ENDSING==1)break; } refresh_browser(storind); PAR_FOL=0; } swap_color(col,rorw) int *col,rorw; { if(rorw)MyGraph->color[0]=*col; else *col=MyGraph->color[0]; } set_cycle(flag,icol) int flag,*icol; { if(flag==0)return; MyGraph->color[0]=*icol+1; *icol=*icol+1; if(*icol==10)*icol=0; } do_range(x) double *x; { char esc; char bob[50]; int ivar=0,res=0,oldic=0; int nit=20,i,itype,cycle=0,icol=0; int color=MyGraph->color[0]; double t,dpar,plow=0.0,phigh=1.0,p; double temp; int ierr=0; if(set_up_range()==0)return(-1); MyStart=1; itype=range.type; ivar=range.index; res=range.reset; oldic=range.oldic; nit=range.steps; plow=range.plow; phigh=range.phigh; cycle=range.cycle; dpar=(phigh-plow)/(double)nit; get_ic(2,x); storind=0; PAUSER=0; if(range.type==PARAM)get_val(range.item,&temp); if(range.movie)reset_film(); for(i=0;i<=nit;i++) { if(range.movie)clr_scrn(); if(cycle)MyGraph->color[0]=icol+1; icol++; if(icol==10)icol=0; p=plow+dpar*(double)i; if(oldic==1){ get_ic(1,x); /* if(reset_memory()==0)break; */ if(DelayFlag){ /* restart initial data */ if(do_init_delay(DELAY)==0)break; } } t=T0; MyStart=1; POIEXT=0; if(res==1||STOCH_FLAG)storind=0; if(itype==IC)x[ivar]=p; else { set_val(range.item,p); re_evaluate_kernels(); } sprintf(bob,"%s=%.16g i=%d",range.item,p,i); bottom_msg(2,bob); if(integrate(&t,x,TEND,DELTA_T,1,NJMP,&MyStart)==1){ ierr=-1; break; } /* printf("storind = %d \n",storind); */ if(STOCH_FLAG) append_stoch(i,storind); if(range.movie) if(film_clip()==0)err_msg("Out of film"); } if(oldic==1)get_ic(1,x); else get_ic(0,x); if(range.type==PARAM)set_val(range.item,temp); MyGraph->color[0]=color; INFLAG=1; refresh_browser(storind); ping(); if(STOCH_FLAG) do_stats(nit,ierr); return(ierr); } find_equilibrium() { static char *n[]={"(R)ange","(G)o","(M)ouse"}; static char key[]="rgm"; char ch; int i,ierr; float xm,ym; int im,jm; int iv,jv; float stabinfo; double *x,oldtrans; Window temp=main_win; x=&MyData[0]; if(FFT||HIST||NKernel>0)return; ch=(char)pop_up_list(&temp,"Equilibria",n,key,3,12,1,10,6*DCURY+8); if(ch==27)return; STORFLAG=0; POIMAP=0; oldtrans=TRANS; TRANS=0.0; switch(ch){ case 'r': do_eq_range(x); return; case 'm': /* Get mouse values */ iv=MyGraph->xv[0]-1; jv=MyGraph->yv[0]-1; if(iv<0||iv>=NODE||jv<0||jv>=NODE||MyGraph->grtype>=5||jv==iv){ err_msg("Not in useable 2D plane..."); return; } /* get mouse click x,y */ get_ic(1,x); message_box(&temp,0,SCALEY-5*DCURY,"Click on guess"); if(get_mouse_xy(&im,&jm,draw_win)){ scale_to_real(im,jm,&xm,&ym); x[iv]=(double)xm; x[jv]=(double)ym; } XDestroyWindow(display,temp); break; case 'g': default: get_ic(2,x); break; } if(DelayFlag) do_delay_sing(x,NEWT_ERR,EVEC_ERR,BOUND,EVEC_ITER,NODE,&ierr,&stabinfo); else do_sing(x,NEWT_ERR,EVEC_ERR,BOUND,EVEC_ITER,NODE,&ierr,&stabinfo); TRANS=oldtrans; } do_init_data() { static char *n[]={"(R)ange","(L)ast","(O)ld","(G)o","(M)ouse","(S)hift","(N)ew", "s(H)oot","(F)ile","form(U)la"}; static char key[]="rlogmsnhfu"; char ch; char sr[20]; int i,si; double *x; FILE *fp; char icfile[100]; float xm,ym; int im,jm,oldstart,iv,jv; Window temp=main_win; oldstart=MyStart; MyStart=1; x=&MyData[0]; RANGE_FLAG=0; DelayErr=0; if(FFT||HIST)return; ch= (char)pop_up_list(&temp,"Integrate",n,key,10,11,3,10,3*DCURY+8); if(ch==27)return; data_back(); wipe_rep(); MyTime=T0; STORFLAG=1; POIEXT=0; storind=0; reset_browser(); switch(ch){ case 'r': /* do range */ do_range(x); return; case 's': case 'l': if(INFLAG==0){ ping(); err_msg("No prior solution"); return; } get_ic(0,x); if(ch=='s'){ T0=LastTime; MyTime=T0; } if(METHOD==VOLTERRA&&oldstart==0){ ch=(char)two_choice("No","Yes","Reset integrals?","ny",0,0,main_win); if(ch=='n')MyStart=oldstart; } break; case 'o': get_ic(1,x); if(DelayFlag){ /* restart initial data */ if(do_init_delay(DELAY)==0)return; } break; case 'm': iv=MyGraph->xv[0]-1; jv=MyGraph->yv[0]-1; if(iv<0||iv>=NODE||jv<0||jv>=NODE||MyGraph->grtype>=5||jv==iv){ err_msg("Not in useable 2D plane..."); return; } /* Get mouse values */ get_ic(1,x); message_box(&temp,0,SCALEY-5*DCURY,"Click on initial data"); if(get_mouse_xy(&im,&jm,draw_win)){ scale_to_real(im,jm,&xm,&ym); im=MyGraph->xv[0]-1; jm=MyGraph->yv[0]-1; x[iv]=(double)xm; x[jv]=(double)ym; last_ic[im]=x[im]; last_ic[jm]=x[jm]; } XDestroyWindow(display,temp); if(DelayFlag){ /* restart initial data */ if(do_init_delay(DELAY)==0)return; } break; case 'n': man_ic(); get_ic(2,x); break; case 'u': if(form_ic()==0)return; get_ic(2,x); break; case 'h': if(ShootICFlag==0){ err_msg("No shooting data available"); break; } sprintf(sr,"Which? (1-%d)",ShootIndex); si=1; new_int(sr,&si); si--; if(si=0){ for(i=0;i=TRANS&&STORFLAG==1&&POIMAP==0) { storage[0][0]=(float)MyTime; extra(x,MyTime,NODE,NEQ); for(i=0;invars; IXPLT=MyGraph->xv; IYPLT=MyGraph->yv; IZPLT=MyGraph->zv; if((METHOD==GEAR)&&(*start==1))*start=0; if(METHOD==0){ nit=tend; dt=dt/fabs(dt); } else nit=tend/fabs(dt); nit=(nit+nout-1)/nout; if(nit==0)return(rval); extra(x,*t,NODE,NEQ); /* Note this takes care of initializing Markov variables */ xv[0]=(float)*t; for(ieqn=1;ieqn<=NEQ;ieqn++)xv[ieqn]=(float)x[ieqn-1]; for(ip=0;ip TOR_PERIOD) { x[ieqn] -= TOR_PERIOD; for (ip = 0; ip < NPlots; ip++) { if (ieqn == IYPLT[ip]-1) oldypl[ip] -= (float) TOR_PERIOD; if (ieqn == IXPLT[ip]-1) oldxpl[ip] -= (float) TOR_PERIOD; if ((ieqn == IZPLT[ip]-1) && (PLOT_3D == 1)) oldzpl[ip] -= (float) TOR_PERIOD; } } if (x[ieqn] < 0) { x[ieqn] += TOR_PERIOD; for (ip = 0; ip < NPlots; ip++) { if (ieqn == IYPLT[ip]-1) oldypl[ip] += (float) TOR_PERIOD; if (ieqn == IXPLT[ip]-1) oldxpl[ip] += (float) TOR_PERIOD; if ((ieqn == IZPLT[ip]-1) && (MyGraph->ThreeDFlag == 1)) oldzpl[ip] += (float) TOR_PERIOD; } } } } } xvold[0]=xv[0]; for(ieqn=1;ieqn<(NEQ+1);ieqn++) { xvold[ieqn]=xv[ieqn]; xv[ieqn]=(float)x[ieqn-1]; if(fabs(x[ieqn-1])>BOUND) { if(RANGE_FLAG)break; sprintf(error_message," %s out of bounds at t = %f ", uvar_names[ieqn-1],*t); err_msg(error_message); break; } } /* This is where the progresser goes */ plot_command(nit,icount,cwidth); esc=my_abort(); { if(esc==ESCAPE) break; if(esc=='/'){rval=1;ENDSING=1;break;} } if(DelayErr){rval=1;ENDSING=1;DelayErr=0;break;} if(ieqn<(NEQ+1))break; tv=(float)*t; xv[0]=tv; if((POIMAP==2)&&!(POIVAR==0)) { pflag=0; if((oldx[POIVAR-1]x[POIVAR-1])&&!(POIEXT>0))POIEXT=-1; if( ( !(oldx[POIVAR-1]0) )|| ( !(oldx[POIVAR-1]>x[POIVAR-1]) && (POIEXT<0) ) ) { if(POISGN*POIEXT>=0) { /* We will intrpolate to get a good local extremum */ rhs(*t,x,xprime,NEQ); rhs(oldt,oldx,oldxprime,NEQ); dint=xprime[POIVAR-1]/(xprime[POIVAR-1]-oldxprime[POIVAR-1]); tv=(1-dint)**t+dint*oldt; xv[0]=tv; for(i=1;i<=NEQ;i++)xv[i]=dint*oldx[i-1]+(1-dint)*x[i-1]; pflag=1; } POIEXT=-POIEXT; } goto poi; } if(POIMAP==1) { if(POIVAR==0) { sect1=fmod(fabs(oldt),fabs(POIPLN)); sect=fmod(fabs(*t),fabs(POIPLN)); if(sect0)) { if((oldx[POIVAR-1]>POIPLN)&&!(x[POIVAR-1]>POIPLN)) { dint=(x[POIVAR-1]-POIPLN)/(x[POIVAR-1]-oldx[POIVAR-1]); tv=(1-dint)**t+dint*oldt; xv[0]=tv; for(i=1;i<=NEQ;i++)xv[i]=dint*oldx[i-1]+(1-dint)*x[i-1]; pflag=1; } else pflag=0; } } poi: for(i=0;iColorFlag) comp_color(xv+1,xvold+1,NODE,(float)fabs(dt*NJMP)); do_plot(oldxpl,oldypl,oldzpl,xpl,ypl,zpl); XFlush(display); } for(ip=0;ip=nit&&count!=0)break; } LastTime=*t; return(rval); } do_plot(oldxpl,oldypl, oldzpl,xpl, ypl, zpl) float *oldxpl, *oldypl, *oldzpl,*xpl, *ypl,*zpl; { int ip,np=MyGraph->nvars; for(ip=0;ipColorFlag==0){ set_linestyle(MyGraph->color[ip]); } if(MyGraph->line[ip]==0) { if(MyGraph->ThreeDFlag==0) point_abs(xpl[ip],ypl[ip]); else point_3d(xpl[ip],ypl[ip],zpl[ip]); } else { if(MyGraph->ThreeDFlag==0){ line_abs(oldxpl[ip],oldypl[ip],xpl[ip],ypl[ip]); } else line_3d(oldxpl[ip],oldypl[ip],oldzpl[ip], xpl[ip],ypl[ip],zpl[ip]); } } } /* old restore is in restore.c */ restore(i1,i2) int i1,i2; { int ip,np=MyGraph->nvars; int ZSHFT,YSHFT,XSHFT; int i,j,kxoff,kyoff,kzoff; int iiXPLT,iiYPLT,iiZPLT; float oldxpl,oldypl,oldzpl,xpl,ypl,zpl; float v1[MAXODE],v2[MAXODE]; XSHFT=MyGraph->xshft; YSHFT=MyGraph->yshft; ZSHFT=MyGraph->zshft; if(i1xv[ip]; iiYPLT=MyGraph->yv[ip]; iiZPLT=MyGraph->zv[ip]; set_linestyle(MyGraph->color[ip]); oldxpl=my_browser.data[iiXPLT][kxoff]; oldypl=my_browser.data[iiYPLT][kyoff]; oldzpl=my_browser.data[iiZPLT][kzoff]; for(i=i1;i(float)(.5*TOR_PERIOD))oldxpl=xpl; if (fabs(oldypl-ypl)>(float)(.5*TOR_PERIOD))oldypl=ypl; if (fabs(oldzpl-zpl)>(float)(.5*TOR_PERIOD))oldzpl=zpl; } if(MyGraph->ColorFlag!=0&&i>i1){ for(j=0;jline[ip]==0){ if(MyGraph->ThreeDFlag==0)point_abs(xpl,ypl); else point_3d(xpl,ypl,zpl); } else { if(MyGraph->ThreeDFlag==0) line_abs(oldxpl,oldypl,xpl,ypl); else line_3d(oldxpl,oldypl,oldzpl,xpl,ypl,zpl); } oldxpl=xpl; oldypl=ypl; oldzpl=zpl; kxoff++; kyoff++; kzoff++; } } } /* Sets the color according to the velocity or z-value */ comp_color( v1,v2,n, dt) float *v1, *v2; int n; float dt; { int i,cur_color; float sum; float min_scale=(float)(MyGraph->min_scale); float color_scale=(float)(MyGraph->color_scale); if(MyGraph->ColorFlag==2)sum=v1[MyGraph->zv[0]]; else { for(i=0,sum=0.0;icolor_total)cur_color=color_total-1; cur_color+=FIRSTCOLOR; set_color(cur_color); } shoot(x,xg,evec,sgn) double *x,*xg,*evec; int sgn; { int i; double t=0.0; for(i=0;i