void do_world(void){ world_t *w = world_get(); world_setup_iterators(w); keyboard_frame(); next_time(); do_physics(w); do_think(w); do_graphics(w); do_sounds(w); do_garbage_collect(w); wait_frame(); }
inline void sc_simcontext::cycle( const sc_time& t) { sc_time next_event_time; m_in_simulator_control = true; m_runnable->toggle(); crunch(); trace_cycle( /* delta cycle? */ false ); m_curr_time += t; next_event_time = next_time(); if ( next_event_time != SC_ZERO_TIME && next_event_time <= m_curr_time) { SC_REPORT_WARNING(SC_ID_CYCLE_MISSES_EVENTS_, ""); } m_in_simulator_control = false; }
void initialize(const Real t) { if (dt <= 0.0) { throw std::invalid_argument( "A step interval must be positive."); } if (count == 0) { t0 = t; } else { while (next_time() < t) { ++count; } } }
void erts_time_remaining(SysTimeval *rem_time) { int ticks; #if !defined(ERTS_TIMER_THREAD) SysTimeval cur_time; #endif long elapsed; /* next_time() returns no of ticks to next timeout or -1 if none */ if ((ticks = next_time()) == -1) { /* timer queue empty */ /* this will cause at most 100000000 ticks */ rem_time->tv_sec = 100000; rem_time->tv_usec = 0; } else { /* next timeout after ticks ticks */ ticks *= CLOCK_RESOLUTION; #if defined(ERTS_TIMER_THREAD) elapsed = 0; #else erts_smp_mtx_lock(&erts_timeofday_mtx); get_tolerant_timeofday(&cur_time); cur_time.tv_usec = 1000 * (cur_time.tv_usec / 1000);/* ms resolution*/ elapsed = 1000 * (cur_time.tv_sec - last_delivered.tv_sec) + (cur_time.tv_usec - last_delivered.tv_usec) / 1000; erts_smp_mtx_unlock(&erts_timeofday_mtx); if (ticks <= elapsed) { /* Ooops, better hurry */ rem_time->tv_sec = rem_time->tv_usec = 0; return; } #endif rem_time->tv_sec = (ticks - elapsed) / 1000; rem_time->tv_usec = 1000 * ((ticks - elapsed) % 1000); } }
bool GillespieSimulator::step(const Real &upto) { if (upto <= t()) { return false; } if (upto >= next_time()) { step(); return true; } else { // no reaction occurs // set_dt(next_time() - upto); set_t(upto); last_reactions_.clear(); draw_next_reaction(); return false; } }
int main(int argc, char **argv) { // Traits typedefs // {{{ // typedef ::World< ::CyclicWorldTraits<Real> > world_type; // typedef EGFRDSimulator< ::EGFRDSimulatorTraitsBase<world_type> > // simulator_type; typedef ecell4::egfrd::EGFRDWorld world_type; typedef ecell4::egfrd::EGFRDSimulator simulator_type; typedef simulator_type::multi_type multi_type; // }}} // Constants // {{{ const ecell4::Real L(1e-6); const ecell4::Real3 edge_lengths(L, L, L); const ecell4::Integer3 matrix_sizes(3, 3, 3); const ecell4::Real volume(L * L * L); const ecell4::Integer N(60); const ecell4::Real kd(0.1), U(0.5); const ecell4::Real ka(kd * volume * (1 - U) / (U * U * N)); const ecell4::Real k2(ka), k1(kd); const ecell4::Integer dissociation_retry_moves(3); // }}} boost::shared_ptr<ecell4::NetworkModel> model(new ecell4::NetworkModel()); // add ::SpeciesType to ::ParticleModel // {{{ ecell4::Species sp1( std::string("A"), std::string("2.5e-09"), std::string("1e-12")); model->add_species_attribute(sp1); ecell4::Species sp2( std::string("B"), std::string("2.5e-09"), std::string("1e-12")); model->add_species_attribute(sp2); ecell4::Species sp3( std::string("C"), std::string("2.5e-09"), std::string("1e-12")); model->add_species_attribute(sp3); // }}} // ReactionRules // {{{ // A -> B + C k1 // {{{ ecell4::ReactionRule rr1( ecell4::create_unbinding_reaction_rule(sp1, sp2, sp3, k1)); model->add_reaction_rule(rr1); // }}} // B + C -> A k2 // {{{ ecell4::ReactionRule rr2( ecell4::create_binding_reaction_rule(sp2, sp3, sp1, k2)); model->add_reaction_rule(rr2); // }}} // }}} // Random Number Generator (Instanciate and Initialize) // {{{ // boost::shared_ptr<ecell4::GSLRandomNumberGenerator> boost::shared_ptr<ecell4::RandomNumberGenerator> rng(new ecell4::GSLRandomNumberGenerator()); rng->seed((unsigned long int)0); // rng->seed(time(NULL)); // }}} // World Definition // {{{ boost::shared_ptr<world_type> world(new world_type(edge_lengths, matrix_sizes, rng)); world->bind_to(model); // }}} // add ecell4::Species( ::SpeciesInfo) to ::World // {{{ // world->add_species(ecell4::Species("A")); // world->add_species(ecell4::Species("B")); // world->add_species(ecell4::Species("C")); // }}} // Thorow particles into world at random // {{{ world->add_molecules(ecell4::Species("A"), N); typedef std::vector<std::pair<ecell4::ParticleID, ecell4::Particle> > particle_id_pair_list; const particle_id_pair_list particles(world->list_particles()); for (particle_id_pair_list::const_iterator i(particles.begin()); i != particles.end(); ++i) { const ecell4::Real3 pos((*i).second.position()); std::cout << "(" << pos[0] << pos[1] << pos[2] << ")" << std::endl; } // }}} // Logger Settings // {{{ boost::shared_ptr< ::LoggerManager> logger_mng( new ::LoggerManager("dummy", ::Logger::L_WARNING)); ::LoggerManager::register_logger_manager( "ecell.EGFRDSimulator", logger_mng); // }}} // EGFRDSimulator instance generated // {{{ boost::shared_ptr<simulator_type> sim( new simulator_type(world, model, dissociation_retry_moves)); // sim->paranoiac() = true; sim->initialize(); // }}} // Simulation Executed // {{{ ecell4::Real next_time(0.0), dt(0.02); std::cout << sim->t() << "\t" << world->num_molecules_exact(sp1) << "\t" << world->num_molecules_exact(sp2) << "\t" << world->num_molecules_exact(sp3) << "\t" << std::endl; // for (int i(0); i < 10; i++) // for (int i(0); i < 100; i++) for (int i(0); i < 100; i++) { next_time += dt; while (sim->step(next_time)) { // if (sim->last_reactions().size() > 0) // { // std::cout << sim->t() << "\t" // << world->num_molecules_exact(sp1) << "\t" // << world->num_molecules_exact(sp2) << "\t" // << world->num_molecules_exact(sp3) << "\t" << std::endl; // } } std::cout << sim->t() << "\t" << world->num_molecules_exact(sp1) << "\t" << world->num_molecules_exact(sp2) << "\t" << world->num_molecules_exact(sp3) << "\t" << std::endl; } // }}} // world->save("test.h5"); // Statistics // {{{ int num_single_steps_per_type[simulator_type::NUM_SINGLE_EVENT_KINDS]; num_single_steps_per_type[simulator_type::SINGLE_EVENT_REACTION] = sim->num_single_steps_per_type(simulator_type::SINGLE_EVENT_REACTION); num_single_steps_per_type[simulator_type::SINGLE_EVENT_ESCAPE] = sim->num_single_steps_per_type(simulator_type::SINGLE_EVENT_ESCAPE); std::cout << (boost::format("%1%: %2% \n") % "SINGLE_EVENT_REACTION" % num_single_steps_per_type[simulator_type::SINGLE_EVENT_REACTION]); std::cout << (boost::format("%1%: %2% \n") % "SINGLE_EVENT_ESCAPE" % num_single_steps_per_type[simulator_type::SINGLE_EVENT_ESCAPE]); std::cout << (boost::format("%1%: %2% \n") % "PAIR_EVENT_SINGLE_REACTION_0" % sim->num_pair_steps_per_type( simulator_type::PAIR_EVENT_SINGLE_REACTION_0)); std::cout << (boost::format("%1%: %2% \n") % "PAIR_EVENT_SINGLE_REACTION_1" % sim->num_pair_steps_per_type( simulator_type::PAIR_EVENT_SINGLE_REACTION_1)); std::cout << (boost::format("%1%: %2% \n") % "PAIR_EVENT_COM_ESCAPE" % sim->num_pair_steps_per_type(simulator_type::PAIR_EVENT_COM_ESCAPE)); std::cout << (boost::format("%1%: %2% \n") % "PAIR_EVENT_IV_UNDETERMINED" % sim->num_pair_steps_per_type( simulator_type::PAIR_EVENT_IV_UNDETERMINED)); std::cout << (boost::format("%1%: %2% \n") % "PAIR_EVENT_IV_ESCAPE" % sim->num_pair_steps_per_type(simulator_type::PAIR_EVENT_IV_ESCAPE)); std::cout << (boost::format("%1%: %2% \n") % "PAIR_EVENT_IV_REACTION" % sim->num_pair_steps_per_type(simulator_type::PAIR_EVENT_IV_REACTION)); std::cout << (boost::format("%1%: %2% \n") % "NONE" % sim->num_multi_steps_per_type(multi_type::NONE)); std::cout << (boost::format("%1%: %2% \n") % "ESCAPE" % sim->num_multi_steps_per_type(multi_type::ESCAPE)); std::cout << (boost::format("%1%: %2% \n") % "REACTION" % sim->num_multi_steps_per_type(multi_type::REACTION)); // }}} // { // boost::scoped_ptr<world_type> // world2(new world_type(ecell4::Real3(1, 2, 3), ecell4::Integer3(3, 6, 9))); // std::cout << "edge_lengths:" << world2->edge_lengths()[0] << " " << world2->edge_lengths()[1] << " " << world2->edge_lengths()[2] << std::endl; // std::cout << "matrix_sizes:" << world2->matrix_sizes()[0] << " " << world2->matrix_sizes()[1] << " " << world2->matrix_sizes()[2] << std::endl; // std::cout << "num_particles: " << world2->num_particles() << std::endl; // world2->load("test.h5"); // std::cout << "edge_lengths:" << world2->edge_lengths()[0] << " " << world2->edge_lengths()[1] << " " << world2->edge_lengths()[2] << std::endl; // std::cout << "matrix_sizes:" << world2->matrix_sizes()[0] << " " << world2->matrix_sizes()[1] << " " << world2->matrix_sizes()[2] << std::endl; // std::cout << "num_particles: " << world2->num_particles() << std::endl; // } return 0; }
void run() { const Real world_size(1e-6); const Position3 edge_lengths(world_size, world_size, world_size); const Real volume(world_size * world_size * world_size); const Integer N(60); const std::string D("1e-12"), radius("2.5e-9"); // const Real kD( // 4 * M_PI * (2 * std::atof(D.c_str())) * (2 * std::atof(radius.c_str()))); const Real kd(0.1), U(0.5); const Real ka(kd * volume * (1 - U) / (U * U * N)); #if (STYPE == EGFRD_MODE) || (STYPE == BD_MODE) const Real k2(ka), k1(kd); #else const Real k2(ka * kD / (ka + kD)); const Real k1(k2 * kd / ka); #endif Species sp1("A", radius, D), sp2("B", radius, D), sp3("C", radius, D); ReactionRule rr1(create_unbinding_reaction_rule(sp1, sp2, sp3, k1)), rr2(create_binding_reaction_rule(sp2, sp3, sp1, k2)); boost::shared_ptr<NetworkModel> model(new NetworkModel()); model->add_species_attribute(sp1); model->add_species_attribute(sp2); model->add_species_attribute(sp3); model->add_reaction_rule(rr1); model->add_reaction_rule(rr2); boost::shared_ptr<GSLRandomNumberGenerator> rng(new GSLRandomNumberGenerator()); rng->seed(time(NULL)); #if STYPE == EGFRD_MODE const Integer matrix_size(3); boost::shared_ptr<world_type> world( new world_type(world_size, matrix_size, rng)); #elif STYPE == BD_MODE boost::shared_ptr<world_type> world(new world_type(edge_lengths, rng)); #elif STYPE == ODE_MODE boost::shared_ptr<world_type> world(new world_type(volume)); #else // STYPE == GILLESPIE_MODE boost::shared_ptr<world_type> world(new world_type(volume, rng)); #endif world->add_molecules(sp1, N); simulator_type sim(model, world); #if STYPE == BD_MODE sim.set_dt(1e-3); #endif Real next_time(0.0), dt(0.02); // sim.save_hdf5_init(std::string("mapk.hdf5")); std::cout << sim.t() << "\t" << world->num_molecules(sp1) << "\t" << world->num_molecules(sp2) << "\t" << world->num_molecules(sp3) << std::endl; for (unsigned int i(0); i < 100; ++i) { next_time += dt; while (sim.step(next_time)) {} std::cout << sim.t() << "\t" << world->num_molecules(sp1) << "\t" << world->num_molecules(sp2) << "\t" << world->num_molecules(sp3) << std::endl; // sim.save_hdf5(); } }
void sc_simcontext::simulate( const sc_time& duration ) { initialize( true ); if (sim_status() != SC_SIM_OK) { return; } sc_time non_overflow_time = sc_time(~sc_dt::UINT64_ZERO, false) - m_curr_time; if ( duration > non_overflow_time ) { SC_REPORT_ERROR(SC_ID_SIMULATION_TIME_OVERFLOW_, ""); return; } else if ( duration < SC_ZERO_TIME ) { SC_REPORT_ERROR(SC_ID_NEGATIVE_SIMULATION_TIME_,""); } m_in_simulator_control = true; sc_time until_t = m_curr_time + duration; m_until_event->cancel(); // to be on the safe side m_until_event->notify_internal( duration ); sc_time t; // IF DURATION WAS ZERO WE ONLY CRUNCH: // // We duplicate the code so that we don't add the overhead of the // check to each loop in the do below. if ( duration == SC_ZERO_TIME ) { m_runnable->toggle(); crunch( true ); if ( m_error ) return; if ( m_something_to_trace ) trace_cycle( /* delta cycle? */ false ); if ( m_forced_stop ) do_sc_stop_action(); return; } // NON-ZERO DURATION: EXECUTE UP TO THAT TIME: do { m_runnable->toggle(); crunch(); if ( m_error ) { m_in_simulator_control = false; return; } if ( m_something_to_trace ) { trace_cycle( false ); } // check for call(s) to sc_stop if ( m_forced_stop ) { do_sc_stop_action(); return; } do { t = next_time(); // PROCESS TIMED NOTIFICATIONS do { sc_event_timed* et = m_timed_events->extract_top(); sc_event* e = et->event(); delete et; if ( e != 0 ) { e->trigger(); } } while ( m_timed_events->size() && m_timed_events->top()->notify_time() == t ); } while ( m_runnable->is_empty() && t != until_t ); if ( t > m_curr_time ) m_curr_time = t; } while ( t != until_t ); m_in_simulator_control = false; }
void step(void) { step(next_time()); }