Skip to content

Commit

Permalink
Debug stopSimulation function and changeSimulationTime v2
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@9046 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Parham Vasaiely authored and Parham Vasaiely committed May 19, 2011
1 parent e8c0ca0 commit 88793ab
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 112 deletions.
185 changes: 101 additions & 84 deletions c_runtime/interactive/omi_Calculation.cpp
Expand Up @@ -26,7 +26,7 @@

using namespace std;

bool debugCalculation = false; //Set true to print out comments which describes the program flow to the console
bool debugCalculation = true; //Set true to print out comments which describes the program flow to the console
bool forZero = true; //The first calculation must start from 0 to 0 (in OpenModelica the solver calculates from 0 - 2.220446049250313e-13)
bool* p_forZero = 0; //The first calculation must start from 0 to 0 (in OpenModelica the solver calculates from 0 - 2.220446049250313e-13)

Expand All @@ -53,80 +53,97 @@ int calculate() {
string method;
string outputFormat;

getSimulationStartData(&stepSizeORG, &outputSteps, &tolerance, &method, &outputFormat);
getSimulationStartData(&stepSizeORG, &outputSteps, &tolerance, &method, &outputFormat);
//TODO 20100217 pv catch correct stepSize value for calculation loop
if(debugCalculation)
{
cout << "Calculation:\tFunct.: calculate\tData 1: start: " << start << " stop: " << stop << " stepSize: " << stepSizeORG << " outputSteps: " << outputSteps << " method: " << method << " outputFormat: " << outputFormat; fflush(stdout);
cout << "Calculation:\tFunct.: calculate\tData 1: start: " << start << " stop: " << stop << " stepSize: " << stepSizeORG << " outputSteps: " << outputSteps << " method: " << method << " outputFormat: " << outputFormat; fflush(stdout);
}

if (method == std::string("euler") || method == std::string("rungekutta")
|| method == std::string("dassl")) {
set_timeValue(start);
set_forceEmit(0);
} else {
set_lastEmittedTime(start);
set_forceEmit(0);
}
if (method == std::string("euler") || method == std::string("rungekutta") || method == std::string("dassl")) {
set_timeValue(start);
set_forceEmit(0);
} else {
set_lastEmittedTime(start);
set_forceEmit(0);
}

initDelay(start);

while (!calculationInterrupted) { //TODO 20100210 pv Interrupt is not implemented yet
mutexSimulationStatus->Lock(); // Lock to see the simulation status.
if(simulationStatus == SimulationStatus::STOPPED)
{
// If the simulation should stop, unlock and break out of the loop.
mutexSimulationStatus->Unlock();
break;
}

if(simulationStatus == SimulationStatus::RUNNING)
{
// If the simulation should continue, increase the semaphore.
waitForResume->Post();
}
// Unlock and see if we need to wait for resume or not.
mutexSimulationStatus->Unlock();
waitForResume->Wait();

if (forZero) {
stop = 2.220446049250313e-13; //This value equals 0 in modelica
stepSize = 2.220446049250313e-13;
set_stepSize(stepSize);
forZero = false;
} else {
//TODO 20100210 pv testing rungekutter...
if (method == std::string("euler") || method == std::string("rungekutta") || method == std::string("dassl")){
stop = get_timeValue() + stepSize;
start = get_timeValue();
if (debugCalculation){
cout << "Calculation:\tFunct.: calculate\tData 2: p_SimStepData_from_Calculation->forTimeStep: " << p_SimStepData_from_Calculation->forTimeStep << " --------------------" << endl; fflush(stdout);
cout << "Calculation:\tFunct.: calculate\tData 3: start " << start << " stop: " << stop << endl; fflush(stdout);
}


} else {
stop = get_lastEmittedTime() + stepSize;
start = get_lastEmittedTime();
}
}

retVal = callSolverFromOM(method, outputFormat, start, stop, stepSize, outputSteps,
tolerance);

if (retVal != 0){
cout << "omi_Calculation: error occurred while calculating" << endl; fflush(stdout);
return 1;
}

stepSize = stepSizeORG;
set_stepSize(stepSize);
createSSDEntry(method);
calculationInterrupted = false;
setResultData(p_SimStepData_from_Calculation); //ssd(tn) as parameter
mutexSimulationStatus->Lock(); // Lock to see the simulation status.
if(simulationStatus == SimulationStatus::STOPPED)
{
// If the simulation should stop, unlock and break out of the loop.
mutexSimulationStatus->Unlock();
if (debugCalculation) {
cout << "Calculation:\tFunct.: calculate\tMessage: Simulation Stopped set forZero = true" << endl; fflush( stdout);
}
forZero = true;
//break;
}

if(simulationStatus == SimulationStatus::SHUTDOWN)
{
// If the simulation should stop, unlock and break out of the loop.
mutexSimulationStatus->Unlock();
if (debugCalculation) {
cout << "Calculation:\tFunct.: calculate\tMessage: Simulation Stopped set forZero = true" << endl; fflush( stdout);
}
break;
}

if(simulationStatus == SimulationStatus::RUNNING)
{
// If the simulation should continue, increase the semaphore.
waitForResume->Post();
}
// Unlock and see if we need to wait for resume or not.
mutexSimulationStatus->Unlock();

if (debugCalculation) {
cout << "Calculation:\tFunct.: calculate\tMessage: Waiting" << endl; fflush( stdout);
}
waitForResume->Wait();
if (debugCalculation) {
cout << "Calculation:\tFunct.: calculate\tMessage: Starting calculation" << endl; fflush( stdout);
}
if (forZero) {
stop = 2.220446049250313e-13; //This value equals 0 in modelica
stepSize = 2.220446049250313e-13;
set_stepSize(stepSize);
forZero = false;
} else {
//TODO 20100210 pv testing rungekutter...
if (method == std::string("euler") || method == std::string("rungekutta") || method == std::string("dassl")){
stop = get_timeValue() + stepSize;
start = get_timeValue();
if (debugCalculation){
cout << "Calculation:\tFunct.: calculate\tData 2: p_SimStepData_from_Calculation->forTimeStep: " << p_SimStepData_from_Calculation->forTimeStep << " --------------------" << endl; fflush(stdout);
cout << "Calculation:\tFunct.: calculate\tData 3: start " << start << " stop: " << stop << endl; fflush(stdout);
}
} else {
stop = get_lastEmittedTime() + stepSize;
start = get_lastEmittedTime();
}
}

retVal = callSolverFromOM(method, outputFormat, start, stop, stepSize, outputSteps,
tolerance);

if (retVal != 0){
cout << "Calculation:\tFunct.: calculate\tMessage: omi_Calculation: error occurred while calculating" << endl; fflush(stdout);
return 1;
}

stepSize = stepSizeORG;
set_stepSize(stepSize);
createSSDEntry(method);
calculationInterrupted = false;
setResultData(p_SimStepData_from_Calculation); //ssd(tn) as parameter
}
//if (debugCalculation)
cout << "*** calculation end: calculationInterrupted -> " << calculationInterrupted << endl; fflush(stdout);
cout << "Calculation:\tFunct.: calculate\tMessage: Calculation end: calculationInterrupted -> " << calculationInterrupted << endl; fflush(stdout);
//return retVal; //TODO 20100210 pv Implement the return value correctly
return 0;
}
Expand All @@ -135,45 +152,45 @@ int calculate() {
* Asks the ServiceInterface for the last simulation results to put into the simulation step data structure
*/
void createSSDEntry(string method) {
fillSimulationStepDataWithValuesFromGlobalData(method,
p_SimStepData_from_Calculation);

p_sdnMutex->Lock();
long nStates = p_simdatanumbers->nStates;
long nAlgebraic = p_simdatanumbers->nAlgebraic;
long nParameters = p_simdatanumbers->nParameters;
p_sdnMutex->Unlock();
if (debugCalculation)
//printSSDCalculation(nStates, nAlgebraic, nParameters);
if (debugCalculation)
cout << "Calculation:\tFunct.: createSSDEntry\tData: p_SimStepData_from_Calculation->forTimeStep: " << p_SimStepData_from_Calculation->forTimeStep << " --------------------" << endl; fflush(stdout);

fillSimulationStepDataWithValuesFromGlobalData(method,
p_SimStepData_from_Calculation);

p_sdnMutex->Lock();
long nStates = p_simdatanumbers->nStates;
long nAlgebraic = p_simdatanumbers->nAlgebraic;
long nParameters = p_simdatanumbers->nParameters;
p_sdnMutex->Unlock();
if (debugCalculation)
//printSSDCalculation(nStates, nAlgebraic, nParameters);
if (debugCalculation)
cout << "Calculation:\tFunct.: createSSDEntry\tData: p_SimStepData_from_Calculation->forTimeStep: "
<< p_SimStepData_from_Calculation->forTimeStep << " --------------------" << endl; fflush( stdout);
}

/**
* Only for debugging
* Prints out the actual calculated Simulation Step Data structure
*/
void printSSDCalculation(long nStates, long nAlgebraic, long nParameters) {
cout << "OutPutSSD-CALCULATION***********" << endl; fflush(stdout);
cout << "p_SimStepData_from_Calculation->forTimeStep: " << p_SimStepData_from_Calculation->forTimeStep << " --------------------" << endl; fflush(stdout);
cout << "Calculation:\tFunct.: printSSDCalculation\tMessage: OutPutSSD-CALCULATION***********" << endl; fflush(stdout);
cout << "Calculation:\tFunct.: printSSDCalculation\tData: p_SimStepData_from_Calculation->forTimeStep: " << p_SimStepData_from_Calculation->forTimeStep << " --------------------" << endl; fflush(stdout);

cout << "---Parmeters--- " << endl; fflush(stdout);
cout << "Calculation:\tFunct.: printSSDCalculation\tMessage: Parmeters--- " << endl; fflush(stdout);
for (int t = 0; t < nParameters; t++)
{
cout << t << ": " << p_simDataNames_SimulationResult->parametersNames[t]<< ": " << p_SimStepData_from_Calculation->parameters[t] << endl; fflush(stdout);
}

if (nAlgebraic > 0) {
cout << "---Algebraics---" << endl; fflush(stdout);
cout << "Calculation:\tFunct.: printSSDCalculation\tMessage: Algebraics---" << endl; fflush(stdout);
for (int t = 0; t < nAlgebraic; t++)
{
cout << t << ": " << p_simDataNames_SimulationResult->algebraicsNames[t]<< ": " << p_SimStepData_from_Calculation->algebraics[t] << endl; fflush(stdout);
}
}

if (nStates > 0) {
cout << "---States---" << endl; fflush(stdout);
cout << "Calculation:\tFunct.: printSSDCalculation\tMessage: States---" << endl; fflush(stdout);
for (int t = 0; t < nStates; t++)
{
cout << t << ": " << p_simDataNames_SimulationResult->statesNames[t]<< ": " << p_SimStepData_from_Calculation->states[t] << endl; fflush(stdout);
Expand All @@ -190,7 +207,7 @@ THREAD_RET_TYPE threadSimulationCalculation(THREAD_PARAM_TYPE lpParam){

if(debugCalculation)
{
cout << "*****Calculation Thread Start*****" << endl; fflush(stdout);
cout << "Calculation:\tMessage: Calculation Thread Start*****" << endl; fflush(stdout);
}

p_sdnMutex->Lock();
Expand All @@ -216,7 +233,7 @@ THREAD_RET_TYPE threadSimulationCalculation(THREAD_PARAM_TYPE lpParam){

if(debugCalculation)
{
cout << "*****Calculation Thread End*****" << endl; fflush(stdout);
cout << "Calculation:\tMessage: Calculation Thread End*****" << endl; fflush(stdout);
}

return (THREAD_RET_TYPE_NO_API)retValue;
Expand Down
51 changes: 27 additions & 24 deletions c_runtime/interactive/omi_Control.cpp
Expand Up @@ -284,21 +284,21 @@ void connectToControlServer(Socket* p_sock)
* Re-initializes the whole simulation runtime so the simulation can start from beginning
*/
void reInitAll() {
SimStepData* p_ssdAtSimulationTime = getResultDataFirstStart();
if (debugLevelControl > 0)
{
cout << "Control:\tFunct.: reInitAll\tData: p_ssdAtChangedSimulationTime->forTimeStep: " << p_ssdAtSimulationTime->forTimeStep << endl; fflush(stdout);
}
setGlobalSimulationValuesFromSimulationStepData(p_ssdAtSimulationTime);
resetSRDFAfterChangetime(); //Resets the SRDF Array and the producer and consumer semaphores
resetSSDArrayWithNullSSD(); //overrides all SSD Slots with nullSSD elements
if (debugLevelControl > 0)
{
cout << "Control:\tFunct.: reInitAll\tData: globalData->lastEmittedTime: " << get_lastEmittedTime() << endl; fflush(stdout);
cout << "Control:\tFunct.: reInitAll\tData: globalData->timeValue: " << get_timeValue() << endl; fflush(stdout);
}
SimStepData* p_ssdAtSimulationTime = getResultDataFirstStart();
if (debugLevelControl > 0)
{
cout << "Control:\tFunct.: reInitAll\tData: p_ssdAtChangedSimulationTime->forTimeStep: " << p_ssdAtSimulationTime->forTimeStep << endl; fflush(stdout);
}
setGlobalSimulationValuesFromSimulationStepData(p_ssdAtSimulationTime);
resetSRDFAfterChangetime(); //Resets the SRDF Array and the producer and consumer semaphores
resetSSDArrayWithNullSSD(); //overrides all SSD Slots with nullSSD elements
if (debugLevelControl > 0)
{
cout << "Control:\tFunct.: reInitAll\tData: globalData->lastEmittedTime: " << get_lastEmittedTime() << endl; fflush(stdout);
cout << "Control:\tFunct.: reInitAll\tData: globalData->timeValue: " << get_timeValue() << endl; fflush(stdout);
}

*p_forZero = true;
*p_forZero = true;
}

/**
Expand Down Expand Up @@ -479,24 +479,27 @@ void pauseSimulation()
void stopSimulation(void) {
if (status.compare("stop") != 0)
{
pauseSimulation();
pauseSimulation();

reInitAll();
reInitAll();

status = "stop";
mutexSimulationStatus->Lock();
simulationStatus = SimulationStatus::STOPPED;
mutexSimulationStatus->Unlock();
status = "stop";

cout << "Control:\tFunct.: stopSimulation\tMessage: stop done" << endl; fflush(stdout);
}
else
{
cout << "Control:\tFunct.: stopSimulation\tMessage: already stopped" << endl; fflush(stdout);
}
cout << "Control:\tFunct.: stopSimulation\tMessage: stop done" << endl; fflush(stdout);
}
else
{
cout << "Control:\tFunct.: stopSimulation\tMessage: already stopped" << endl; fflush(stdout);
}
}

void endSimulation()
{
mutexSimulationStatus->Lock();
simulationStatus = SimulationStatus::STOPPED;
simulationStatus = SimulationStatus::SHUTDOWN;
mutexSimulationStatus->Unlock();
shutDown();
}
Expand Down
3 changes: 2 additions & 1 deletion c_runtime/interactive/omi_Control.h
Expand Up @@ -25,7 +25,8 @@ struct SimulationStatus
{
RUNNING,
PAUSED,
STOPPED
STOPPED,
SHUTDOWN
};
};

Expand Down
12 changes: 9 additions & 3 deletions c_runtime/interactive/omi_Transfer.cpp
Expand Up @@ -294,11 +294,17 @@ void doTransfer(long nStates, long nAlgebraic, long nParameters) {
mutexSimulationStatus->Lock(); // Lock to see the simulation status.
if(simulationStatus == SimulationStatus::STOPPED)
{
// If the simulation should stop, unlock and break out of the loop.
mutexSimulationStatus->Unlock();
break;
// If the simulation should stop, unlock and break out of the loop.
mutexSimulationStatus->Unlock();
}

if(simulationStatus == SimulationStatus::SHUTDOWN)
{
// If the simulation should stop, unlock and break out of the loop.
mutexSimulationStatus->Unlock();
break;
}

if(simulationStatus == SimulationStatus::RUNNING)
{
// If the simulation should continue, increase the semaphore.
Expand Down

0 comments on commit 88793ab

Please sign in to comment.