static void start_integ(double nb) { char *str; printf("%s%d%s\n", "n=", (int) nb, " Rectangles\tTrapezes\tSimpson"); str = integ(1, nb); printf("%s\t%s\n", "F1:", str); free(str); str = integ(2, nb); printf("%s\t%s\n", "F2:", str); free(str); str = integ(3, nb); printf("%s\t%s\n", "F3:", str); free(str); str = integ(4, nb); printf("%s\t%s\n", "F4:", str); free(str); str = integ(5, nb); printf("%s\t%s\n", "F5:", str); free(str); str = integ(6, nb); printf("%s\t%s\n", "F6:", str); free(str); str = integ(7, nb); printf("%s\t%s\n", "F7:", str); free(str); str = integ(8, nb); printf("%s\t%s\n", "F8:", str); free(str); str = integ(9, nb); printf("%s\t%s\n", "F9:", str); free(str); str = integ(10, nb); printf("%s\t%s\n", "F10:", str); free(str); }
int main () { try { PendulumSystem sys; sys.addEventHandler(new ZeroVelocityHandler(sys)); sys.addEventHandler(PeriodicHandler::handler = new PeriodicHandler()); sys.addEventHandler(new ZeroPositionHandler(sys)); sys.addEventReporter(PeriodicReporter::reporter = new PeriodicReporter(sys)); sys.addEventReporter(new OnceOnlyEventReporter()); sys.addEventReporter(new DiscontinuousReporter()); sys.realizeTopology(); // Test with various intervals for the event handler and event reporter, ones that are either // large or small compared to the expected internal step size of the integrator. for (int i = 0; i < 4; ++i) { PeriodicHandler::handler->setEventInterval(i == 0 || i == 1 ? 0.01 : 2.0); PeriodicReporter::reporter->setEventInterval(i == 0 || i == 2 ? 0.015 : 1.5); // Test the integrator in both normal and single step modes. VerletIntegrator integ(sys); testIntegrator(integ, sys); integ.setReturnEveryInternalStep(true); testIntegrator(integ, sys); } cout << "Done" << endl; return 0; } catch (std::exception& e) { std::printf("FAILED: %s\n", e.what()); return 1; } }
inline Eigen::VectorXd integrate(const Model & model, const Eigen::VectorXd & q, const Eigen::VectorXd & v) { Eigen::VectorXd integ(model.nq); for( Model::JointIndex i=1; i<(Model::JointIndex) model.nbody; ++i ) { IntegrateStep::run(model.joints[i], IntegrateStep::ArgsType (q, v, integ) ); } return integ; }
void IBall_sim( Integrator_type Alg, std::ostream& dataout) { BALL ball; long tick; double sim_time; REGULA_FALSI rf; const double seconds_per_tick = 0.01; const double initial_angle = 30.0; const double initial_speed = 50.0; const int doing_dynamic_events = 1; dataout.width(16); dataout.precision(14); // ======================================== // Initialization // ======================================== tick = 0; sim_time = 0.0; ball.pos[0] = 0.0; ball.pos[1] = 0.0; ball.vel[0] = initial_speed * cos( initial_angle * RAD_PER_DEG); ball.vel[1] = initial_speed * sin( initial_angle * RAD_PER_DEG); Trick::Integrator *I = Trick::getIntegrator( Alg, 4, seconds_per_tick ); sim_time = tick * seconds_per_tick ; // Initialize Regula Falsi. reset_regula_falsi(sim_time, &rf); rf.error_tol = 1.0e-15; rf.mode = Any; // Note: We don't care what the tgo estimate is because here, // we are just initializing the bounds. rf.error = ball.pos[0]; regula_falsi(sim_time, &rf); // ======================================== // Simulation loop // ======================================== do { dataout << sim_time << " " << ball.pos[0] << " " << ball.pos[1] << std::endl; I->time = sim_time; // ###I### Integrate over the time step. integ(I, &ball); // Advance time. tick++; sim_time = tick * seconds_per_tick ; // If we are looking for roots ... if ( doing_dynamic_events ) { double tgo; // ###RF### Given the current error, estimate how far (in time) we are from a root. rf.error = ball.pos[0]; tgo = regula_falsi(sim_time, &rf); // If regula_falsi found a root in the last interval ... if ( tgo < seconds_per_tick) { int root_found = 0; double t_test = sim_time; // Iterate until we find the root. // NOTE: the regula_falsi function gives up and returns with tgo=0 if // it hasn't converged on a root after 20 iterations. while (! root_found) { // ###I### Integrate over the time-correction. I->dt = tgo; integ(I, &ball); t_test += tgo; // ###RF### Given the current error, estimate how far (in time) we are from the root. rf.error = ball.pos[0]; tgo = regula_falsi( t_test, &rf); // If the estimated time-to-go is less than the chosen tolerance, then we have our root. if (fabs( tgo) < rf.error_tol) { printf("ROOT@ %18.14g\n", t_test); root_found = 1; reset_regula_falsi(t_test, &rf); } } root_found = 0; // ###I### Integrate from t=t_test back (forward actually) to t=sim_time. I->dt = sim_time - t_test ; integ(I, &ball); // Restore the normal time-step. I->dt = seconds_per_tick; } } // End of doing_dynamic_events. } while (ball.pos[0] >= -3.0); dataout << sim_time << " " << ball.pos[0] << " " << ball.pos[1] << std::endl; delete( I); }
int main (int argc, char **argv) { int nt,nzt,nxt,nz,nxo,nxs,ns,nx,nr,is,ixo,ixs; int ls,jtr,mtr,tracl,mzmax; off_t nseek; float ft,fzt,fxt,fz,fx,fxo,fs,fxs,dt,dzt,dxt,dz,dx,dxo,ds,dxs, ext,ezt,es,ex,ez,xo,s,xs,xg,fpeak; float v0,dvz; float fmax,angmax,offmax,rmax,aperx; float **mig,**migi,***ttab,**tb,**pb,**cosb,**sigb,**tsum,**tt; char *infile="stdin",*outfile="stdout",*ttfile,*jpfile; FILE *infp,*outfp,*ttfp,*jpfp; Wavelet *w; /* hook up getpar to handle the parameters */ initargs(argc, argv); requestdoc(1); /* open input and output files */ if( !getparstring("infile",&infile)) { infp = stdin; } else if ((infp=fopen(infile,"r"))==NULL) err("cannot open infile=%s\n",infile); if( !getparstring("outfile",&outfile)) { outfp = stdout; } else { outfp = fopen(outfile,"w"); } efseeko(infp,(off_t) 0,SEEK_CUR); warn("Got A"); efseeko(outfp,(off_t) 0,SEEK_CUR); if( !getparstring("ttfile",&ttfile)) err("must specify ttfile!\n"); if ((ttfp=fopen(ttfile,"r"))==NULL) err("cannot open ttfile=%s\n",ttfile); if( !getparstring("jpfile",&jpfile)) { jpfp = stderr; } else jpfp = fopen(jpfile,"w"); /* get information for seismogram traces */ if (!getparint("nt",&nt)) nt = 501; if (!getparfloat("dt",&dt)) dt = 0.004; if (!getparfloat("ft",&ft)) ft = 0.0; if (!getparfloat("fmax",&fmax)) fmax = 1.0/(4*dt); fpeak = 0.2/dt; if (!getparint("nxs",&nxs)) nxs = 101; if (!getparfloat("dxs",&dxs)) dxs = 15; if (!getparfloat("fxs",&fxs)) fxs = 0.0; if (!getparint("nxo",&nxo)) nxo = 1; if (!getparfloat("dxo",&dxo)) dxo = 50; if (!getparfloat("fxo",&fxo)) fxo = 0.0; /* get traveltime table parameters */ if (!getparint("nxt",&nxt)) err("must specify nxt!\n"); if (!getparfloat("fxt",&fxt)) err("must specify fxt!\n"); if (!getparfloat("dxt",&dxt)) err("must specify dxt!\n"); if (!getparint("nzt",&nzt)) err("must specify nzt!\n"); if (!getparfloat("fzt",&fzt)) err("must specify fzt!\n"); if (!getparfloat("dzt",&dzt)) err("must specify dzt!\n"); if (!getparint("ns",&ns)) err("must specify ns!\n"); if (!getparfloat("fs",&fs)) err("must specify fs!\n"); if (!getparfloat("ds",&ds)) err("must specify ds!\n"); ext = fxt+(nxt-1)*dxt; ezt = fzt+(nzt-1)*dzt; es = fs+(ns-1)*ds; /* check source and receiver coordinates */ for (ixs=0; ixs<nxs; ++ixs) { xs = fxs+ixs*dxs; for (ixo=0; ixo<nxo; ++ixo) { xg = xs+fxo+ixo*dxo; if (fs>xs || es<xs || fs>xg || es<xg) err("shot or receiver lie outside of specified (x,z) grid\n"); } } /* get migration section parameters */ if (!getparint("nx",&nx)) err("must specify nx!\n"); if (!getparfloat("fx",&fx)) err("must specify fx!\n"); if (!getparfloat("dx",&dx)) err("must specify dx!\n"); if (!getparint("nz",&nz)) err("must specify nz!\n"); if (!getparfloat("fz",&fz)) err("must specify fz!\n"); if (!getparfloat("dz",&dz)) err("must specify dz!\n"); ex = fx+(nx-1)*dx; ez = fz+(nz-1)*dz; if(fxt>fx || ext<ex || fzt>fz || ezt<ez) err(" migration section is out of traveltime table!\n"); if (!getparfloat("v0",&v0)) v0 = 1500; if (!getparfloat("dvz",&dvz)) dvz = 0; if (!getparfloat("angmax",&angmax)) angmax = 60.; if (angmax<0.00001) err("angmax must be positive!\n"); mzmax = dx*sin(angmax*PI/180.)/dz; if(mzmax<1) mzmax = 1; if (!getparfloat("aperx",&aperx)) aperx = 0.5*nxt*dxt; if (!getparint("ls",&ls)) ls = 1; if (!getparint("mtr",&mtr)) mtr = 100; fprintf(jpfp,"\n"); fprintf(jpfp," Modeling parameters\n"); fprintf(jpfp," ================\n"); fprintf(jpfp," infile=%s \n",infile); fprintf(jpfp," outfile=%s \n",outfile); fprintf(jpfp," ttfile=%s \n",ttfile); fprintf(jpfp," \n"); fprintf(jpfp," nzt=%d fzt=%g dzt=%g\n",nzt,fzt,dzt); fprintf(jpfp," nxt=%d fxt=%g dxt=%g\n",nxt,fxt,dxt); fprintf(jpfp," ns=%d fs=%g ds=%g\n",ns,fs,ds); fprintf(jpfp," \n"); fprintf(jpfp," nz=%d fz=%g dz=%g\n",nz,fz,dz); fprintf(jpfp," nx=%d fx=%g dx=%g\n",nx,fx,dx); fprintf(jpfp," \n"); fprintf(jpfp," nxs=%d fxs=%g dxs=%g\n",nxs,fxs,dxs); fprintf(jpfp," nxo=%d fxo=%g dxo=%g\n",nxo,fxo,dxo); fprintf(jpfp," \n"); /* compute reference traveltime and slowness */ offmax = MAX(ABS(fxo),ABS(fxo+(nxo-1)*dxo)); rmax = MAX(es-fxt,ext-fs); rmax = MIN(rmax,0.5*offmax+aperx); nr = 2+(int)(rmax/dx); tb = ealloc2float(nzt,nr); pb = ealloc2float(nzt,nr); sigb = ealloc2float(nzt,nr); cosb = ealloc2float(nzt,nr); timeb(nr,nzt,dx,dzt,fzt,dvz,v0,tb,pb,sigb,cosb); fprintf(jpfp," nt=%d ft=%g dt=%g fpeak=%g \n",nt,ft,dt,fpeak); fprintf(jpfp," v0=%g dvz=%g \n",v0,dvz); fprintf(jpfp," aperx=%g angmax=%g offmax=%g\n",aperx,angmax,offmax); fprintf(jpfp," mtr=%d ls=%d\n",mtr,ls); fprintf(jpfp," ================\n"); fflush(jpfp); /* allocate space */ mig = ealloc2float(nz,nx); migi = ealloc2float(nz+2*mzmax,nx); ttab = ealloc3float(nzt,nxt,ns); tt = ealloc2float(nzt,nxt); tsum = ealloc2float(nzt,nxt); /* make wavelet */ makericker(fpeak,dt,&w); /* set constant segy trace header parameters */ memset((void *) &tr, 0, sizeof(segy)); tr.trid = 1; tr.counit = 1; tr.ns = nt; tr.dt = 1.0e6*dt; tr.delrt = 1.0e3*ft; fprintf(jpfp," read traveltime tables \n"); fflush(jpfp); /* compute traveltime residual */ for(is=0; is<ns; ++is){ nseek = (off_t) nxt*nzt*is; efseeko(ttfp,nseek*((off_t) sizeof(float)),SEEK_SET); fread(ttab[is][0],sizeof(float),nxt*nzt,ttfp); s = fs+is*ds; resit(nxt,fxt,dxt,nzt,nr,dx,tb,ttab[is],s); } fprintf(jpfp," read migration section \n"); fflush(jpfp); /* read migration section */ fread(mig[0],sizeof(float),nx*nz,infp); /* integrate migration section for constructing anti-aliasing filter */ integ(mig,nz,dz,nx,mzmax,migi); fprintf(jpfp," start synthesis ... \n"); fprintf(jpfp," \n"); fflush(jpfp); jtr = 0; /* loop over shots */ for (ixs=0,xs=fxs,tracl=0; ixs<nxs; ++ixs,xs+=dxs) { /* loop over offsets */ for (ixo=0,xo=fxo; ixo<nxo; ++ixo,xo+=dxo) { float as,res; int is; xg = xs+xo; /* set segy trace header parameters */ tr.tracl = tr.tracr = ++tracl; tr.fldr = 1+ixs; tr.tracf = 1+ixo; tr.offset = NINT(xo); tr.sx = NINT(xs); tr.gx = NINT(xg); as = (xs-fs)/ds; is = (int)as; if(is==ns-1) is=ns-2; res = as-is; if(res<=0.01) res = 0.0; if(res>=0.99) res = 1.0; sum2(nxt,nzt,1-res,res,ttab[is],ttab[is+1],tsum); as = (xg-fs)/ds; is = (int)as; if(is==ns-1) is=ns-2; res = as-is; if(res<=0.01) res = 0.0; if(res>=0.99) res = 1.0; sum2(nxt,nzt,1-res,res,ttab[is],ttab[is+1],tt); sum2(nxt,nzt,1,1,tt,tsum,tsum); fflush(jpfp); /* make one trace */ maketrace(tr.data,nt,ft,dt,xs,xg,mig,migi,aperx, nx,fx,dx,nz,fz,dz,mzmax,ls,angmax,v0,fmax,w, tb,pb,sigb,cosb,nr,tsum,nxt,fxt,dxt,nzt,fzt,dzt); /* write trace */ fputtr(outfp,&tr); jtr++; if((jtr-1)%mtr ==0 ){ fprintf(jpfp," generated trace %d\n",jtr); fflush(jpfp); } } } fprintf(jpfp," generated %d traces in total\n",jtr); fprintf(jpfp," \n"); fprintf(jpfp," output done\n"); fflush(jpfp); efclose(jpfp); efclose(outfp); free2float(tsum); free2float(tt); free2float(pb); free2float(tb); free2float(cosb); free2float(sigb); free3float(ttab); free2float(mig); free2float(migi); return(CWP_Exit()); }