int main() { // Standard deviation of truncated normal distribution in proposal function double proposal_stddev = 0.1; // Side length of the box double side_length = 15.0; // Number of oppositely charged particle pairs unsigned particle_num = 20; // Thermodynamic beta double beta = 300.0; // Number of samples std::size_t sample_num = 100000; // Output bool save_output = true; std::string filename = "out.tsv"; // Convenience typedefs using Ensemble = CanonicalEnsemble<Particle2D>; using PotentialPtr = double (*)(const Particle2D &, const Particle2D &); auto initial_state = random_state(side_length, particle_num); auto proposal_function = unif_proposal_function(proposal_stddev, side_length); // Simulate samples of the canonical ensemble using the Random-Walk // Metropolis-Algorithm Ensemble ensemble(initial_state, beta, static_cast<PotentialPtr>(coulomb_core), proposal_function); std::vector<Ensemble::State> samples; samples.reserve(sample_num); std::size_t accepted_cnt = 0; for (std::size_t i = 0; i < sample_num; ++i) { if (ensemble.step()) { ++accepted_cnt; } samples.push_back(ensemble.get_state()); } std::cout << "Acceptance probability: " << static_cast<double>(accepted_cnt) / sample_num << std::endl; // Save output to .tsv if (save_output) { std::ofstream os(filename); for (auto it = samples.cbegin(), end = samples.cend(); os && it != end; ++it) { for (const auto &particle : *it) { os << particle.q << "\t" << particle.x << "\t" << particle.y << "\t"; } os << "\n"; } } return 0; }
void CEnsemble3dExt::Ensemble3Dvolume(const char* folder_name) { _init_temporal(); int k = 0, max_nodule_size = 35; int start = seedz, z = seedz; int org_x = seedx, org_y = seedy, org_z = seedz; for(z=start; z>=0; z--) { //printf ("%s\n", slices_name[z].c_str()); cout<<z<<":"<<seedx<<','<<seedy<<endl; aslice = _dcm.getSllice(z); // cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );// Create a window for display. // cv::imshow( "Display window", cv::Mat(aslice) ); // Show our image inside it. // // cv::waitKey(0); outmask = cvCreateImage(cvGetSize(aslice),8,1); cvZero(outmask); //cvShowImage("in", aslice); //cvWaitKey(0); CvPoint seed = cvPoint(seedx,seedy); //cvSmooth(aslice, aslice); CEnsemble ensemble(aslice, seed, outmask); ensemble.doEnsembleSegmentation(); SaveResult(folder_name, z); if(!UpdateSeed(z, BACKWARD)) break; k ++; if(k > max_nodule_size) break; cvReleaseImage(&outmask); } seedx = org_x; seedy = org_y; seedz = org_z; k = 0; for(z=start+1; z<slices_name.size(); z++) { ////printf ("%s\n", slices_name[i].c_str()); cout<<z<<":"<<seedx<<','<<seedy<<endl; aslice = _dcm.getSllice(z); outmask = cvCreateImage(cvGetSize(aslice),8,1); cvZero(outmask); CvPoint seed = cvPoint(seedx,seedy); //cvSmooth(aslice, aslice); CEnsemble ensemble(aslice, seed, outmask); ensemble.doEnsembleSegmentation(); SaveResult(folder_name, z); if(!UpdateSeed(z, FORWARD)) break; k ++; if(k > max_nodule_size) break; //cvWaitKey(0); cvReleaseImage(&outmask); } _saveToNiffty(_temporal, folder_name ); }
int main(int argc, char *argv[]) { namespace po=boost::program_options; po::options_description desc("Well-mixed SIR with demographics."); int64_t individual_cnt=100000; int64_t infected_cnt=individual_cnt*0.1; int64_t recovered_cnt=individual_cnt*0.8; int run_cnt=1; size_t rand_seed=1; // Time is in years. std::vector<Parameter> parameters; parameters.emplace_back(Parameter{SIRParam::Beta0, "beta0", 400, "main infection rate"}); parameters.emplace_back(Parameter{SIRParam::Beta1, "beta1", 0.6, "seasonality ratio"}); parameters.emplace_back(Parameter{SIRParam::SeasonalPhase, "phase", 0, "seasonality phase start between (0,1]"}); parameters.emplace_back(Parameter{SIRParam::Gamma, "gamma", 365/14.0, "recovery rate"}); parameters.emplace_back(Parameter{SIRParam::Birth, "birth", 1/70.0, "crude rate, before multiplying by number of individuals"}); parameters.emplace_back(Parameter{SIRParam::Mu, "mu", 1/70.0, "death rate"}); double end_time=30.0; bool exacttraj=true; bool exactinfect=false; int thread_cnt=1; std::string log_level; std::string data_file("sirexp.h5"); bool save_file=false; std::string translation_file; bool test=false; desc.add_options() ("help", "show help message") ("threadcnt,j", po::value<int>(&thread_cnt)->default_value(thread_cnt), "number of threads") ("runcnt", po::value<int>(&run_cnt)->default_value(run_cnt), "number of runs") ("size,s", po::value<int64_t>(&individual_cnt)->default_value(individual_cnt), "size of the population") ("infected,i", po::value<int64_t>(&infected_cnt), "number of infected") ("recovered,r", po::value<int64_t>(&recovered_cnt), "number of recovered") ("seed", po::value<size_t>(&rand_seed)->default_value(rand_seed), "seed for random number generator") ("endtime", po::value<double>(&end_time)->default_value(end_time), "how many years to run") ("exacttraj", po::value<bool>(&exacttraj)->default_value(exacttraj), "save trajectory only when it changes by a certain amount") ("exactinfect", po::value<bool>(&exactinfect)->default_value(exactinfect), "set true to use exact distribution for seasonal infection") ("datafile", po::value<std::string>(&data_file)->default_value(data_file), "Write to this data file.") ("save", po::value<bool>(&save_file)->default_value(save_file), "Add data to file instead of erasing it with new data.") ("loglevel", po::value<std::string>(&log_level)->default_value("info"), "Set the logging level to trace, debug, info, warning, error, or fatal.") ("translate", po::value<std::string>(&translation_file)->default_value(""), "write file relating place ids to internal ids") ("info", "show provenance of program") ; for (auto& p : parameters) { desc.add_options()(p.name.c_str(), po::value<double>(&p.value)->default_value(p.value), p.description.c_str()); } po::variables_map vm; auto parsed_options=po::parse_command_line(argc, argv, desc); po::store(parsed_options, vm); po::notify(vm); if (vm.count("help")) { std::cout << desc << std::endl; return 0; } std::map<std::string,std::string> compile_info { {"VERSION", VERSION}, {"COMPILETIME", COMPILETIME}, {"CONFIG", CFG} }; if (vm.count("info")) { for (auto& kv : compile_info) { std::cout << kv.second << "\n\n"; } return 0; } afidd::LogInit(log_level); if (test) { double tolerance=TestSeasonal(0.6); std::cout << "Seasonal tolerance " << tolerance << std::endl; } std::map<SIRParam,double*> params; for (auto& pm : parameters) { params[pm.kind]=&pm.value; } // Birthrate is not frequency-dependent. It scales differently // which creates a fixed point in the phase plane. (*params[SIRParam::Birth])*=individual_cnt; if (std::abs(*params[SIRParam::Beta1])>1) { std::cout << "beta1 should be fractional, between 0 and 1: beta1=" << *params[SIRParam::Beta1] << std::endl; return -4; } if (vm.count("infected") and !vm.count("recovered") || !vm.count("infected") and vm.count("recovered")) { std::cout << "You have so set the total and I and R, not just some of them." << std::endl; return -3; } else if (!vm.count("infected") && !vm.count("recovered")) { double b=*params[SIRParam::Beta0]; double m=*params[SIRParam::Mu]; double g=*params[SIRParam::Gamma]; double B=*params[SIRParam::Birth]; // Long-time averages for fixed forcing for ODE model. int64_t susceptible_start=std::floor((m+g)*individual_cnt/b); infected_cnt=std::floor(individual_cnt*(b-m-g)*m/(b*(m+g))); recovered_cnt=individual_cnt-(susceptible_start+infected_cnt); } int64_t susceptible_cnt=individual_cnt-(infected_cnt+recovered_cnt); assert(susceptible_cnt>0); if (susceptible_cnt<0) { BOOST_LOG_TRIVIAL(error)<<"Number of susceptibles is "<<susceptible_cnt; return -2; } std::vector<int64_t> sir_init{susceptible_cnt, infected_cnt, recovered_cnt}; BOOST_LOG_TRIVIAL(info)<<"Starting with sir="<<sir_init[0]<<" "<<sir_init[1] <<" "<<sir_init[2]; for (auto& showp : parameters) { BOOST_LOG_TRIVIAL(info)<<showp.name<<" "<<showp.value; } HDFFile file(data_file); if (!file.Open(!save_file)) { BOOST_LOG_TRIVIAL(error)<<"could not open output file: "<<data_file; return -1; } file.WriteExecutableData(compile_info, parsed_options, sir_init); auto runnable=[=](RandGen& rng, size_t single_seed, size_t idx)->void { std::shared_ptr<TrajectoryObserver> observer=0; if (exacttraj) { observer=std::make_shared<TrajectorySave>(); } else { observer=std::make_shared<PercentTrajectorySave>(); } SIR_run(end_time, sir_init, parameters, *observer, rng, exactinfect); file.SaveTrajectory(parameters, single_seed, idx, observer->Trajectory()); }; Ensemble<decltype(runnable)> ensemble(runnable, thread_cnt, run_cnt, rand_seed); ensemble.Run(); BOOST_LOG_TRIVIAL(debug)<<"Finished running ensemble."; file.Close(); return 0; }
/* main routine */ int main(int argc, char *argv[]){ FILELog::ReportingLevel() = logINFO; VARIABLES vars; vars = commandline_input(argc, argv); clock_t init, final; int number_of_particles = vars.num; double timestep = vars.dt; bool use_T2 = vars.use_T2; // = false (no T2 decay), = true (T2 decay) int num_of_repeat = vars.num_of_repeat; //Number of times to repeat simulation. For every repeat all data is flushed and we start the simulation again. int number_of_timesteps; number_of_timesteps = (int)ceil(vars.gs/timestep); double permeability = 0.0; double radius = .00535; double d = sqrt( 2.0*PI*radius*radius/( sqrt(3.0)*.79 ) ); //double lattice_size = 0.00601922026995422789215053328651; double grad_duration = 4.5; double D_extra = 2.5E-6; double D_intra = 1.0E-6; double T2_e = 200; double T2_i = 200; FILE_LOG(logINFO) << "f = " << 2.0*PI*radius*radius/(sqrt(3.0)*d*d) << std::endl; Vector3 xhat(1.0,0.0,0.0); Vector3 yhat(0.0,1.0,0.0); Vector3 zhat(0.0,0.0,1.0); Lattice<Cylinder_XY> lattice(D_extra, T2_e, permeability); lattice.setLatticeVectors(d,2.0*d*0.86602540378443864676372317075294,d,xhat,yhat,zhat); lattice.addBasis(Cylinder_XY(d/2.0, 0.0, radius, T2_i, D_intra, 1)); lattice.addBasis(Cylinder_XY(0.0, d*0.86602540378443864676372317075294, radius, T2_i, D_intra, 2)); lattice.addBasis(Cylinder_XY(d/2.0, 2.0*d*0.86602540378443864676372317075294, radius, T2_i, D_intra, 3)); lattice.addBasis(Cylinder_XY(d, d*0.86602540378443864676372317075294, radius, T2_i, D_intra, 4)); double gspacings [] = { 8.0 , 10.5 , 14.0 , 18.5 , 24.5 , 32.5 , 42.5 , 56.5 }; double bvals [] = {108780, 154720, 219040, 301730, 411980, 558990, 742420, 1000000 }; double G [9]; for (int kk = 0; kk < num_of_repeat; kk++) { vector<PGSE> measurements_x; vector<PGSE> measurements_y; vector<PGSE> measurements_z; for (int i = 0; i < 8; i++){ double echo_time = 2.0*grad_duration + gspacings[i]; G[0] = 0.0; G[i+1] = sqrt(bvals[i]/(GAMMA*GAMMA*grad_duration*grad_duration*(grad_duration + gspacings[i] - (grad_duration/3.0)))); measurements_x.push_back(PGSE(grad_duration,gspacings[i], timestep, G[0], echo_time, number_of_particles, xhat)); measurements_y.push_back(PGSE(grad_duration,gspacings[i], timestep, G[0], echo_time, number_of_particles, yhat)); measurements_z.push_back(PGSE(grad_duration,gspacings[i], timestep, G[0], echo_time, number_of_particles, zhat)); measurements_x.push_back(PGSE(grad_duration,gspacings[i], timestep, G[i+1], echo_time, number_of_particles, xhat)); measurements_y.push_back(PGSE(grad_duration,gspacings[i], timestep, G[i+1], echo_time, number_of_particles, yhat)); measurements_z.push_back(PGSE(grad_duration,gspacings[i], timestep, G[i+1], echo_time, number_of_particles, zhat)); } vector<double> lnsignal(2); vector<double> b(2); cout << " trial = " << kk << endl; Particles ensemble(number_of_particles,timestep, use_T2); lattice.initializeUniformly(ensemble.getGenerator() , ensemble.getEnsemble() ); for (int k = 0; k < measurements_x.size();k++){ measurements_x[k].updatePhase(ensemble.getEnsemble(), 0.0); measurements_y[k].updatePhase(ensemble.getEnsemble(), 0.0); measurements_z[k].updatePhase(ensemble.getEnsemble(), 0.0); } for (int i = 1; i <= number_of_timesteps; i++){ ensemble.updateposition(lattice); for (int k = 0; k < measurements_x.size();k++){ measurements_x[k].updatePhase(ensemble.getEnsemble(), i*timestep); measurements_y[k].updatePhase(ensemble.getEnsemble(), i*timestep); measurements_z[k].updatePhase(ensemble.getEnsemble(), i*timestep); } } for (int i = 0; i < 8*2; i+=2){ double ADCx, ADCy, ADCz, diff_time; lnsignal[0] = log(measurements_x[i].get_signal()); b[0] = measurements_x[i].get_b(); lnsignal[1] = log(measurements_x[i+1].get_signal()); b[1] = measurements_x[i+1].get_b(); ADCx = -1.0*linear_regression(lnsignal,b); //std::cout << b[0] << " " << lnsignal[0] << " " << b[1] << " " << lnsignal[1] << " "; lnsignal[0] = log(measurements_y[i].get_signal()); b[0] = measurements_y[i].get_b(); lnsignal[1] = log(measurements_y[i+1].get_signal()); b[1] = measurements_y[i+1].get_b(); ADCy = -1.0*linear_regression(lnsignal,b); //std::cout << b[0] << " " << lnsignal[0] << " " << b[1] << " " << lnsignal[1] << " "; lnsignal[0] = log(measurements_z[i].get_signal()); b[0] = measurements_z[i].get_b(); lnsignal[1] = log(measurements_z[i+1].get_signal()); b[1] = measurements_z[i+1].get_b(); ADCz = -1.0*linear_regression(lnsignal,b); //std::cout << b[0] << " " << lnsignal[0] << " " << b[1] << " " << lnsignal[1] << std::endl; diff_time = measurements_x[i].get_DT(); std::cout << i << " " << diff_time << " " << ADCx << " " << ADCy << " " << ADCz << " " << b[1] << " " << G[1] << std::endl; } } final= clock()-init; //final time - intial time