int MSNet::simulate(SUMOTime start, SUMOTime stop) { // the simulation loop std::string quitMessage = ""; myStep = start; do { if (myLogStepNumber) { preSimStepOutput(); } simulationStep(); if (myLogStepNumber) { postSimStepOutput(); } MSNet::SimulationState state = simulationState(stop); #ifndef NO_TRACI if (state!=SIMSTATE_RUNNING) { if (OptionsCont::getOptions().getInt("remote-port")!=0&&!traci::TraCIServer::wasClosed()) { state = SIMSTATE_RUNNING; } } #endif if (state!=SIMSTATE_RUNNING) { quitMessage = "Simulation End: " + getStateMessage(state); } } while (quitMessage==""); WRITE_MESSAGE(quitMessage); // exit simulation loop closeSimulation(start); return 0; }
void FormationWorld::update( ){ for( int i=0; i<per_frame; i++ ){ //n_moves=0; n_interactions=0; simulationStep( dt ); //simulationStep_semiBruteForce( dt ); //simulationStep_BruteForce( dt ); //printf( " ==== DONE sub_step %i v2max %f f2max %f n_moves %i n_inter %i \n", i, v2max, f2max, n_moves, n_interactions ); } }
//-------------------------------------------------------------- void ofApp::update() { for(int i = 0; i < 8; i++) { simulationStep(); } }
void Engine::run() { QTimer simulationTimer; connect(&simulationTimer, SIGNAL(timeout()), this, SLOT(simulationStep())); simulationTimer.start(1000 / FPS); QTimer fpsTimer; connect(&fpsTimer, SIGNAL(timeout()), this, SLOT(printFPS())); fpsTimer.start(1000); exec(); }
dReal evaluate_controller(Controller* controller,noveltyitem* ni,data_record* record,bool log, bool off) { vector<float> k; dReal fitness; int timestep=0; const int simtime=1500; create_world(controller,log); while (!creatures[0]->abort() && timestep<simtime) { simulationStep(); timestep++; if (timestep%100 == 0 && novelty_function % 2 == 1) { update_behavior(k,creatures[0]); } /* if (log && timestep%100==0) cout << creatures[0]->fitness() << endl;*/ } int time=timestep; //for (int x=timestep+1; x<=simtime; x++) // if (x%100==0) while (k.size()< (simtime/100*2)) update_behavior(k,creatures[0]); fitness=creatures[0]->fitness(); ((Biped*)creatures[0])->lft.push_back(timestep); ((Biped*)creatures[0])->rft.push_back(timestep); if (ni!=NULL) { //ni->time=time; ni->novelty_scale = 1.0; ni->data.push_back(k); ni->secondary=time; } if (record!=NULL) { dVector3 com; creatures[0]->CenterOfMass(com); record->ToRec[0]=fitness; record->ToRec[1]=com[0]; record->ToRec[2]=com[1]; record->ToRec[3]=com[2]; record->ToRec[4]=timestep; } destroy_world(); return fitness; }
// =========================== set controler ======================== void ControllerWidget::setController( CqMotorController* pController, CqSimulation* pSimulation ) { Q_ASSERT( pController ); Q_ASSERT( pSimulation ); _pController = pController; connect( pController, SIGNAL(destroyed()), SLOT(controllerDestroyed()) ); connect( pSimulation, SIGNAL(simulationStep()), SLOT(simulationStep())); simulationStep(); // to update gauges // also - set slider to current desired value sliderDesiredValue->blockSignals( true ); sliderDesiredValue->setValue( qRound( 100 * (_pController->getDesiredValue() - _pController->getMinValue() ) / ( _pController->getMaxValue() - _pController->getMinValue() ) ) ); sliderDesiredValue->blockSignals( false ); }
int MSNet::simulate(SUMOTime start, SUMOTime stop) { // report the begin when wished WRITE_MESSAGE("Simulation started with time: " + time2string(start)); // the simulation loop MSNet::SimulationState state = SIMSTATE_RUNNING; myStep = start; // preload the routes especially for TraCI loadRoutes(); #ifndef NO_TRACI #ifdef HAVE_PYTHON if (OptionsCont::getOptions().isSet("python-script")) { traci::TraCIServer::runEmbedded(OptionsCont::getOptions().getString("python-script")); closeSimulation(start); WRITE_MESSAGE("Simulation ended at time: " + time2string(getCurrentTimeStep())); WRITE_MESSAGE("Reason: Script ended"); return 0; } #endif #endif while (state == SIMSTATE_RUNNING) { if (myLogStepNumber) { preSimStepOutput(); } simulationStep(); if (myLogStepNumber) { postSimStepOutput(); } state = simulationState(stop); #ifndef NO_TRACI if (state != SIMSTATE_RUNNING) { if (OptionsCont::getOptions().getInt("remote-port") != 0 && !traci::TraCIServer::wasClosed()) { state = SIMSTATE_RUNNING; } } #endif } // report the end when wished WRITE_MESSAGE("Simulation ended at time: " + time2string(getCurrentTimeStep())); WRITE_MESSAGE("Reason: " + getStateMessage(state)); // exit simulation loop closeSimulation(start); return 0; }
int MSNet::simulate(SUMOTime start, SUMOTime stop) { OptionsCont &oc = OptionsCont::getOptions(); if(oc.getSimulationVerbosity()>1) std::cout<<"----> void MSNet::simulate(...)"<<std::endl; //if((oc.getString("net-file").find("munchen"))|| // (oc.getString("net-file").find("munich"))) if(oc.getSafeBool("ger")) setCity("munchen"); //else if(oc.getString("net-file").find("cambiano")) else if(oc.getSafeBool("ita")) setCity("cambiano"); else setCity("unknown"); // the simulation loop MSNet::SimulationState state = SIMSTATE_RUNNING; myStep = start; #ifndef NO_TRACI #ifdef HAVE_PYTHON if(OptionsCont::getOptions().isSet("python-script")) { traci::TraCIServer::runEmbedded(OptionsCont::getOptions().getString("python-script")); WRITE_MESSAGE("Simulation End: Script ended"); closeSimulation(start); return 0; } #endif #endif // Initialize weather conditions if(oc.isSet("fiet")) setCurrentEnvTemp(oc.getFloat("fiet")); else setCurrentEnvTemp(5.3); if(oc.isSet("fieh")) setCurrentEnvHum(oc.getFloat("fieh")); else setCurrentEnvHum(67.8); while(state == SIMSTATE_RUNNING) { if(myLogStepNumber) preSimStepOutput(); simulationStep(); // FIXME Custom wait //for(int i=0;i++<0x00003fff;)for(int j=0;j++<0x00000fff;); if(oc.getSafeBool("rwc") || oc.getSafeBool("rlwc")) updateWeather(); if(myLogStepNumber) postSimStepOutput(); state = simulationState(stop); #ifndef NO_TRACI if(state != SIMSTATE_RUNNING) { if(OptionsCont::getOptions().getInt("remote-port") != 0 && !traci::TraCIServer::wasClosed()) state = SIMSTATE_RUNNING; } #endif } WRITE_MESSAGE("Simulation End: " + getStateMessage(state)); // exit simulation loop closeSimulation(start); return 0; }
/*! \fn performSimulation(DATA* data, SOLVER_INFO* solverInfo) * * \param [ref] [data] * \param [ref] [solverInfo] * * This function performs the simulation controlled by solverInfo. */ int prefixedName_performSimulation(DATA* data, threadData_t *threadData, SOLVER_INFO* solverInfo) { TRACE_PUSH int retValIntegrator=0; int retValue=0; int i, retry=0; unsigned int __currStepNo = 0; SIMULATION_INFO *simInfo = &(data->simulationInfo); solverInfo->currentTime = simInfo->startTime; MEASURE_TIME fmt; fmtInit(data, &fmt); printAllVarsDebug(data, 0, LOG_DEBUG); /* ??? */ printSparseStructure(data, LOG_SOLVER); modelica_boolean syncStep = 0; /***** Start main simulation loop *****/ while(solverInfo->currentTime < simInfo->stopTime) { int success = 0; threadData->currentErrorStage = ERROR_SIMULATION; #ifdef USE_DEBUG_TRACE if(useStream[LOG_TRACE]) printf("TRACE: push loop step=%u, time=%.12g\n", __currStepNo, solverInfo->currentTime); #endif omc_alloc_interface.collect_a_little(); /* try */ #if !defined(OMC_EMCC) MMC_TRY_INTERNAL(simulationJumpBuffer) #endif { clear_rt_step(data); rotateRingBuffer(data->simulationData, 1, (void**) data->localData); modelica_boolean syncEventStep = solverInfo->didEventStep || syncStep; /***** Calculation next step size *****/ if(syncEventStep) { infoStreamPrint(LOG_SOLVER, 0, "offset value for the next step: %.16g", (solverInfo->currentTime - solverInfo->laststep)); } else { if (solverInfo->solverNoEquidistantGrid) { if (solverInfo->currentTime >= solverInfo->lastdesiredStep) { double tmpTime = solverInfo->currentTime; do { __currStepNo++; solverInfo->currentStepSize = (double)(__currStepNo*(simInfo->stopTime-simInfo->startTime))/(simInfo->numSteps) + simInfo->startTime - solverInfo->currentTime; }while(solverInfo->currentStepSize <= 0); } } else { __currStepNo++; } } solverInfo->currentStepSize = (double)(__currStepNo*(simInfo->stopTime-simInfo->startTime))/(simInfo->numSteps) + simInfo->startTime - solverInfo->currentTime; solverInfo->lastdesiredStep = solverInfo->currentTime + solverInfo->currentStepSize; /* if retry reduce stepsize */ if(0 != retry) { solverInfo->currentStepSize /= 2; } /***** End calculation next step size *****/ checkForSynchronous(data, solverInfo); /* check for next time event */ checkForSampleEvent(data, solverInfo); /* if regular output point and last time events are almost equals * skip that step and go further */ if (solverInfo->currentStepSize < 1e-15 && syncEventStep){ __currStepNo++; continue; } /* * integration step determine all states by a integration method * update continuous system */ retValIntegrator = simulationStep(data, threadData, solverInfo); if (S_OPTIMIZATION == solverInfo->solverMethod) break; syncStep = simulationUpdate(data, threadData, solverInfo); retry = 0; /* reset retry */ fmtEmitStep(data, threadData, &fmt, solverInfo->didEventStep); saveDasslStats(solverInfo); checkSimulationTerminated(data, solverInfo); /* terminate for some cases: * - integrator fails * - non-linear system failed to solve * - assert was called */ if(retValIntegrator) { retValue = -1 + retValIntegrator; infoStreamPrint(LOG_STDOUT, 0, "model terminate | Integrator failed. | Simulation terminated at time %g", solverInfo->currentTime); break; } else if(check_nonlinear_solutions(data, 0)) { retValue = -2; infoStreamPrint(LOG_STDOUT, 0, "model terminate | non-linear system solver failed. | Simulation terminated at time %g", solverInfo->currentTime); break; } else if(check_linear_solutions(data, 0)) { retValue = -3; infoStreamPrint(LOG_STDOUT, 0, "model terminate | linear system solver failed. | Simulation terminated at time %g", solverInfo->currentTime); break; } else if(check_mixed_solutions(data, 0)) { retValue = -4; infoStreamPrint(LOG_STDOUT, 0, "model terminate | mixed system solver failed. | Simulation terminated at time %g", solverInfo->currentTime); break; } success = 1; } #if !defined(OMC_EMCC) MMC_CATCH_INTERNAL(simulationJumpBuffer) #endif if (!success) /* catch */ { if(0 == retry) { retrySimulationStep(data, threadData, solverInfo); retry = 1; } else { retValue = -1; infoStreamPrint(LOG_STDOUT, 0, "model terminate | Simulation terminated by an assert at time: %g", data->localData[0]->timeValue); break; } } TRACE_POP /* pop loop */ } /* end while solver */ fmtClose(&fmt); TRACE_POP return retValue; }
void render(){ int i; // loop int j; // loop int k; // loop for(i=0; i<GROUP+5; ++i){ stats_groups_before[i] = 0; stats_groups_after[i] = 0; } if (PRINTING == 1){ // Show what we got printf("Particles before : \n"); for(i=0; i<PARTICLES; ++i){ printf(" :: [%p] [p#:%4d] [x : %3d] [y : %3d] [z : %3d] [g : %2d]\n", &part[i], i, part[i].x, part[i].y, part[i].z, part[i].group); } } // Simulation Cycles for(i=0; i<CYCLES; ++i){ // Each particle for (j=0; j<PARTICLES; ++j){ //Check with the rest of particles for collisions for (k=j+1; k<PARTICLES; ++k){ // Reaction / Particle Collision if (ballsTouching(&part[j], &part[k])) { particleReaction(&part[j], &part[k]); printf("Particle Collision!\n"); } } // Wall Collision if( part[j].x > MAXX || part[j].y > MAXY || part[j].z > MAXZ || part[j].x < MINX || part[j].y < MINY || part[j].z < MINZ ){ oppositeGroup(&part[j].group); } simulationStep(&part[j]); } glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POINTS); for (i=0; i<PARTICLES; ++i){ switch(part[i].group){ case 0: glColor3f(1.0f, 0.0f, 0.0f); break; case 1: glColor3f(0.0f, 1.0f, 0.0f); break; case 2: glColor3f(0.0f, 0.0f, 1.0f); break; case 3: glColor3f(0.5f, 0.5f, 0.0f); break; case 4: glColor3f(0.0f, 0.5f, 0.5f); break; case 5: glColor3f(0.5f, 0.0f, 0.5f); break; default: glColor3f(1.0f, 1.0f, 1.0f); break; } glVertex3f((float) part[i].x/MAXX - 0.5 ,(float) part[i].y/MAXY - 0.5, (float)part[i].z/MAXZ - 0.5); } glEnd(); glutSwapBuffers(); } // End simulation if (PRINTING == 1){ printf("Particles After\n"); for(i=0; i<PARTICLES; ++i){ printf(" :: [%p] [p#:%4d] [x : %3d] [y : %3d] [z : %3d] [g : %2d]\n", &part[i], i, part[i].x, part[i].y, part[i].z, part[i].group); ++stats_groups_after[part[i].group]; } k=0; printf("Total num of particle groups before:\n"); for(i=0; i<GROUP+5; ++i){ printf(" :: Group [%d] = %d\n", i, stats_groups_before[i]); k += stats_groups_before[i]; } printf("Total Particles : %d\n", k); k=0; printf("Total num of particle groups before:\n"); for(i=0; i<GROUP+5; ++i){ printf(" :: Group [%d] = %d\n", i, stats_groups_after[i]); k += stats_groups_after[i]; } printf("Total Particles : %d\n", k); } }
/*! \fn performSimulation(DATA* data, SOLVER_INFO* solverInfo) * * \param [ref] [data] * \param [ref] [solverInfo] * * This function performs the simulation controlled by solverInfo. */ int prefixedName_performSimulation(DATA* data, threadData_t *threadData, SOLVER_INFO* solverInfo) { TRACE_PUSH int retValIntegrator=0; int retValue=0; int i, retry=0, steadStateReached=0; unsigned int __currStepNo = 0; SIMULATION_INFO *simInfo = data->simulationInfo; solverInfo->currentTime = simInfo->startTime; MEASURE_TIME fmt; fmtInit(data, &fmt); printAllVarsDebug(data, 0, LOG_DEBUG); /* ??? */ if (!compiledInDAEMode) { printSparseStructure(&(data->simulationInfo->analyticJacobians[data->callback->INDEX_JAC_A].sparsePattern), data->simulationInfo->analyticJacobians[data->callback->INDEX_JAC_A].sizeRows, data->simulationInfo->analyticJacobians[data->callback->INDEX_JAC_A].sizeCols, LOG_SOLVER, "ODE sparse pattern"); } else { printSparseStructure(data->simulationInfo->daeModeData->sparsePattern, data->simulationInfo->daeModeData->nResidualVars, data->simulationInfo->daeModeData->nResidualVars, LOG_SOLVER, "DAE sparse pattern"); } if(terminationTerminate) { printInfo(stdout, TermInfo); fputc('\n', stdout); infoStreamPrint(LOG_STDOUT, 0, "Simulation call terminate() at initialization (time %f)\nMessage : %s", data->localData[0]->timeValue, TermMsg); data->simulationInfo->stopTime = solverInfo->currentTime; } else { modelica_boolean syncStep = 0; /***** Start main simulation loop *****/ while(solverInfo->currentTime < simInfo->stopTime || !simInfo->useStopTime) { int success = 0; threadData->currentErrorStage = ERROR_SIMULATION; #ifdef USE_DEBUG_TRACE if(useStream[LOG_TRACE]) { printf("TRACE: push loop step=%u, time=%.12g\n", __currStepNo, solverInfo->currentTime); } #endif /* check for steady state */ if (omc_flag[FLAG_STEADY_STATE]) { if (0 < data->modelData->nStates) { int i; double maxDer = 0.0; double currDer; for(i=data->modelData->nStates; i<2*data->modelData->nStates; ++i) { currDer = fabs(data->localData[0]->realVars[i] / data->modelData->realVarsData[i].attribute.nominal); if(maxDer < currDer) maxDer = currDer; } if (maxDer < steadyStateTol) { steadStateReached=1; infoStreamPrint(LOG_STDOUT, 0, "steady state reached at time = %g\n * max(|d(x_i)/dt|/nominal(x_i)) = %g\n * relative tolerance = %g", solverInfo->currentTime, maxDer, steadyStateTol); break; } } else throwStreamPrint(threadData, "No states in model. Flag -steadyState can only be used if states are present."); } omc_alloc_interface.collect_a_little(); /* try */ #if !defined(OMC_EMCC) MMC_TRY_INTERNAL(simulationJumpBuffer) #endif { printAllVars(data, 0, LOG_SOLVER_V); clear_rt_step(data); if (!compiledInDAEMode) /* do not use ringbuffer for daeMode */ rotateRingBuffer(data->simulationData, 1, (void**) data->localData); modelica_boolean syncEventStep = solverInfo->didEventStep || syncStep; /***** Calculation next step size *****/ if(syncEventStep) { infoStreamPrint(LOG_SOLVER, 0, "offset value for the next step: %.16g", (solverInfo->currentTime - solverInfo->laststep)); } else { if (solverInfo->solverNoEquidistantGrid) { if (solverInfo->currentTime >= solverInfo->lastdesiredStep) { do { __currStepNo++; solverInfo->currentStepSize = (double)(__currStepNo*(simInfo->stopTime-simInfo->startTime))/(simInfo->numSteps) + simInfo->startTime - solverInfo->currentTime; } while(solverInfo->currentStepSize <= 0); } } else { __currStepNo++; } } solverInfo->currentStepSize = (double)(__currStepNo*(simInfo->stopTime-simInfo->startTime))/(simInfo->numSteps) + simInfo->startTime - solverInfo->currentTime; solverInfo->lastdesiredStep = solverInfo->currentTime + solverInfo->currentStepSize; /* if retry reduce stepsize */ if (0 != retry) { solverInfo->currentStepSize /= 2; } /***** End calculation next step size *****/ checkForSynchronous(data, solverInfo); /* check for next time event */ checkForSampleEvent(data, solverInfo); /* if regular output point and last time events are almost equals * skip that step and go further */ if (solverInfo->currentStepSize < 1e-15 && syncEventStep){ __currStepNo++; continue; } /* * integration step determine all states by a integration method * update continuous system */ infoStreamPrint(LOG_SOLVER, 1, "call solver from %g to %g (stepSize: %.15g)", solverInfo->currentTime, solverInfo->currentTime + solverInfo->currentStepSize, solverInfo->currentStepSize); retValIntegrator = simulationStep(data, threadData, solverInfo); infoStreamPrint(LOG_SOLVER, 0, "finished solver step %g", solverInfo->currentTime); messageClose(LOG_SOLVER); if (S_OPTIMIZATION == solverInfo->solverMethod) break; syncStep = simulationUpdate(data, threadData, solverInfo); retry = 0; /* reset retry */ fmtEmitStep(data, threadData, &fmt, solverInfo); saveIntegratorStats(solverInfo); checkSimulationTerminated(data, solverInfo); /* terminate for some cases: * - integrator fails * - non-linear system failed to solve * - assert was called */ if (retValIntegrator) { retValue = -1 + retValIntegrator; infoStreamPrint(LOG_STDOUT, 0, "model terminate | Integrator failed. | Simulation terminated at time %g", solverInfo->currentTime); break; } else if(check_nonlinear_solutions(data, 0)) { retValue = -2; infoStreamPrint(LOG_STDOUT, 0, "model terminate | non-linear system solver failed. | Simulation terminated at time %g", solverInfo->currentTime); break; } else if(check_linear_solutions(data, 0)) { retValue = -3; infoStreamPrint(LOG_STDOUT, 0, "model terminate | linear system solver failed. | Simulation terminated at time %g", solverInfo->currentTime); break; } else if(check_mixed_solutions(data, 0)) { retValue = -4; infoStreamPrint(LOG_STDOUT, 0, "model terminate | mixed system solver failed. | Simulation terminated at time %g", solverInfo->currentTime); break; } success = 1; } #if !defined(OMC_EMCC) MMC_CATCH_INTERNAL(simulationJumpBuffer) #endif if (!success) { /* catch */ if(0 == retry) { retrySimulationStep(data, threadData, solverInfo); retry = 1; } else { retValue = -1; infoStreamPrint(LOG_STDOUT, 0, "model terminate | Simulation terminated by an assert at time: %g", data->localData[0]->timeValue); break; } } TRACE_POP /* pop loop */ } /* end while solver */ } /* end else */ fmtClose(&fmt); if (omc_flag[FLAG_STEADY_STATE] && !steadStateReached) { warningStreamPrint(LOG_STDOUT, 0, "Steady state has not been reached.\nThis may be due to too restrictive relative tolerance (%g) or short stopTime (%g).", steadyStateTol, simInfo->stopTime); } TRACE_POP return retValue; }