void convert::force(int nn,double D,double rho,double Cd,double Cm){ // Define Pi // const double PI = 4.0*atan(1.0); // To cover-up for previous lazyness are we now making local // copies of the fields we need - sorry! // std::vector<double> tmp_; tmp_.resize(nt); for (int i=0;i<nt;i++){ tmp_[i] = eta[i*nx*ny+nn]; } // deta/dt // std::vector<double> detadt; detadt.resize(nt); gradient(detadt,tmp_,dt,nt); // dz/dt // Double2d dzdt(boost::extents[nz][nt]); for (int k=0;k<nz;k++){ for (int i=0;i<nt;i++){ dzdt[k][i] = detadt[i]*sigma[k]; } } // du/dt on the sigma grid // Double2d dudt(boost::extents[nz][nt]); for (int k=0;k<nz;k++){ for (int i=0;i<nt;i++){ tmp_[i] = u[i*nx*ny*nz+nn*nz+k]; } gradient(&dudt[k][0],tmp_,dt,nt); // To-do: this is super sluppy and error prone } // Acceleration // Double2d acc(boost::extents[nz][nt]); for (int k=0;k<nz;k++){ for (int i=0;i<nt;i++){ acc[k][i] = dudt[k][i]-uz[i*nx*ny*nz+nn*nz+k]*dzdt[k][i]; } } // The inline force // double dz,Fd,Fi; int index; F.resize(nt); for (int i=0;i<nt;i++){ F[i] = 0; for (int k=1;k<nz-1;k++){// we ignore the ghost points index = i*nx*ny*nz+nn*nz+k; dz = (sigma[k+1]-sigma[k])*(eta[i*nx*ny+nn]+h[nn]); Fd = 0.5*rho*Cd*D*(u[index]*std::abs(u[index])+u[index+1]*std::abs(u[index+1]))/2; Fi = rho*Cm*(PI/4)*pow(D,2)*(acc[k][i]+acc[k+1][i])/2; F[i] += (Fd+Fi)*dz; } } QFileInfo fileInfo = QFileInfo(fileName); std::ofstream morisonForce; morisonForce.open(fileInfo.baseName().toLatin1() + ".force"); morisonForce << "time F" << std::endl; QVector<double> QV_t,QV_F; QV_t.resize(nt); QV_F.resize(nt); for (int i=1;i<nt-1;i++){ morisonForce << std::setiosflags(std::ios::fixed) << std::setprecision(10) << t[i] << " " << F[i] << std::endl; QV_t[i] = t[i]; QV_F[i] = F[i]; } morisonForce.close(); // plot solution QCustomPlot *cPlot = new QCustomPlot; QWidget *plotWindow = new QWidget; QHBoxLayout *plotWindow_layout = new QHBoxLayout; plotWindow_layout->addWidget(cPlot); plotWindow->setLayout(plotWindow_layout); plotWindow->resize(800,400); plotWindow->show(); cPlot->clearGraphs(); cPlot->addGraph(); cPlot->graph()->setData(QV_t,QV_F); cPlot->xAxis->setLabel("Time, t s"); cPlot->yAxis->setLabel("Inline force, F N"); QVector<double>::iterator Xmin = std::min_element(QV_t.begin(), QV_t.end()); QVector<double>::iterator Xmax = std::max_element(QV_t.begin(), QV_t.end()); QVector<double>::iterator Ymin = std::min_element(QV_F.begin(), QV_F.end()); QVector<double>::iterator Ymax = std::max_element(QV_F.begin(), QV_F.end()); cPlot->xAxis->setRange(*Xmin,*Xmax); cPlot->yAxis->setRange(*Ymin,*Ymax); cPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables); cPlot->replot(); }
int main(int argc, char **argv){ FILE *fid; DIR* dir; char fname[300]; long int i,j,nz,elem; double aver[1000],fcollv[1000],sfrv[1000],zv[1000]; float *halo_mass; double zmin,zmax,dz,redshift; if(argc==1 || argc > 5) { printf("Generates SFRD using nonlinear halo boxes\n"); printf("usage: get_SFR base_dir [zmin] [zmax] [dz]\n"); printf("base_dir contains simfast21.ini\n"); exit(1); } get_Simfast21_params(argv[1]); if(global_use_Lya_xrays==0) {printf("Lya and xray use set to false - no need to calculate SFR\n");exit(0);} if(argc > 2) { zmin=atof(argv[2]); if (zmin < global_Zminsfr) zmin=global_Zminsfr; if(argc > 3) { zmax=atof(argv[3]); if(zmax>global_Zmaxsim) zmax=global_Zmaxsim; if(argc==5) dz=atof(argv[4]); else dz=global_Dzsim; }else { zmax=global_Zmaxsim; dz=global_Dzsim; } zmin=zmax-dz*ceil((zmax-zmin)/dz); /* make sure (zmax-zmin)/dz is an integer so that we get same redshifts starting from zmin or zmax...*/ }else { zmin=global_Zminsfr; zmax=global_Zmaxsim; dz=global_Dzsim; } printf("\nCalculating SFRD between z=%f and z=%f with step %f\n",zmin,zmax,dz); #ifdef _OMPTHREAD_ omp_set_num_threads(global_nthreads); printf("Using %d threads\n",global_nthreads); #endif /* Create directory SFR */ sprintf(fname,"%s/SFR",argv[1]); if((dir=opendir(fname))==NULL) { printf("Creating SFR directory\n"); if(mkdir(fname,(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))!=0) { printf("Error creating directory!\n"); exit(1); } } if(!(halo_mass=(float *) malloc(global_N3_smooth*sizeof(float)))) { printf("Problem...\n"); exit(1); } nz=0; printf("Calculating halo mass average...\n"); for(redshift=zmin;redshift<(zmax+dz/10);redshift+=dz){ zv[nz]=redshift; sprintf(fname, "%s/Halos/halonl_z%.3f_N%ld_L%.0f.dat",argv[1],redshift,global_N_smooth,global_L); if((fid=fopen(fname,"rb"))==NULL){ printf("\nError opening %s\n",fname); exit(1); } elem=fread(halo_mass,sizeof(float),global_N3_smooth,fid); fclose(fid); aver[nz]=0.; for(i=0;i<global_N3_smooth;i++) aver[nz]+=halo_mass[i]; fcollv[nz]=aver[nz]/(global_rho_m*global_L3); /* Msun/(Mpc/h)^3 */ aver[nz]/=global_N3_smooth; nz++; } printf("Interpolation for SFRD...\n"); gsl_interp_accel *acc = gsl_interp_accel_alloc (); gsl_spline *spline = gsl_spline_alloc (gsl_interp_cspline, nz); gsl_spline_init (spline, zv, fcollv, nz); sprintf(fname,"%s/Output_text_files",argv[1]); if((dir=opendir(fname))==NULL) { printf("Creating Output_text_files directory\n"); if(mkdir(fname,(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))!=0) { printf("Error creating directory!\n"); exit(1); } } sprintf(fname,"%s/Output_text_files/sfrd_av_N%ld_L%.0f.dat",argv[1],global_N_smooth,global_L); if((fid=fopen(fname,"a"))==NULL){ printf("\nError opening output %s file...\n",fname); exit(1); } for(i=nz-1;i>=0;i--) { sfrv[i]=gsl_spline_eval_deriv(spline, zv[i], acc)*dzdt(zv[i])/3.171E-8*global_rho_b*global_fstar; /* SFRD in Msun/(Mpc/h)^3/year */ if(sfrv[i]<0.) sfrv[i]=0.; fprintf(fid,"%f %.8E\n",zv[i],sfrv[i]); } fclose(fid); gsl_spline_free (spline); gsl_interp_accel_free (acc); printf("Writing SFRD files...\n"); for(i=0;i<nz;i++) { sprintf(fname, "%s/Halos/halonl_z%.3f_N%ld_L%.0f.dat",argv[1],zv[i],global_N_smooth,global_L); if((fid=fopen(fname,"rb"))==NULL){ printf("\nError opening %s\n",fname); exit(1); } elem=fread(halo_mass,sizeof(float),global_N3_smooth,fid); fclose(fid); if(aver[i]>0.) for(j=0;j<global_N3_smooth;j++) halo_mass[j]*=sfrv[i]/aver[i]; else for(j=0;j<global_N3_smooth;j++) halo_mass[j]=0.; sprintf(fname, "%s/SFR/sfrd_z%.3f_N%ld_L%.0f.dat",argv[1],zv[i],global_N_smooth,global_L); if((fid=fopen(fname,"wb"))==NULL){ printf("\nError opening output sfrd file... Chech if path is correct...\n"); } elem=fwrite(halo_mass,sizeof(float),global_N3_smooth,fid); fclose(fid); } exit(0); }
double dtdz(double z) { return (double)(1./dzdt(z)); }