question

iceagesimulator avatar image
0 Likes"
iceagesimulator asked Natalie White commented

Set stop time with code

I want to use code to set the start time and stop time, because later in the future my model will have various modes that the user can choose from (in a GUI comboList dropdown), and those modes will correlate to a different start and stop times (different points throughout a calendar year to test).


I have successfully been able to set the start time with code using the following:

DateTime startTime = DateTime("6/1/2023 08:00:00","%m/%d/%Y %H:%M:%S");
function_s(getmodelunit(START_TIME_NODE), "setDateTime", startTime);

I tried using something similar for stop time:

DateTime stopTime = DateTime("6/30/2023 17:00:00","%m/%d/%Y %H:%M:%S");
function_s(getmodelunit(STOP_TIME_NODE), "setDateTime", stopTime);

but this would have been too easy and it did not work.

Then I tried looking up where the stopTimes are located in the Tree to see if I could access the treeNode property, but I was unsuccessful.

This code was some of my failed attempts:

DateTime stopTime = DateTime("6/30/2023 18:01:00","%m/%d/%Y %H:%M:%S");
treenode stopTimeNode = getmodelunit(STOP_TIME_NODE);
stopTimeNode.value = convert(stopTime, DateTime, FS_DATETIME);
Model.find("Tools/ModelUnits/ModelDateTimes/stopTimes/1/dateTime") = stopTime;

How do I go about setting the stop time for the model with code. (I understand there can be multiple stop times, so I just want to always be able to change the first stop time and only have one stop time).

flexsim-stoptime.jpg

SetStopTime.fsm

FlexSim 23.1.1
codestop time
flexsim-stoptime.jpg (130.8 KiB)
setstoptime.fsm (39.8 KiB)
· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Julie Weller avatar image Julie Weller commented ·

Hi @iceagesimulator, was one of iceagesimulator's or Jordan Johnson's or Jason Lightfoot's answers helpful? If so, please click the "Accept" button at the bottom of the one that best answers your question. Or if you still have questions, add a comment and we'll continue the conversation.

If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always comment back to reopen your question.

0 Likes 0 ·
Natalie White avatar image Natalie White commented ·

Hi @iceagesimulator, was one of iceagesimulator's or Jordan Johnson's or Jason Lightfoot's answers helpful? If so, please click the "Accept" button at the bottom of the one that best answers your question. Or if you still have questions, add a comment and we'll continue the conversation.

If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always comment back to reopen your question.

0 Likes 0 ·
Jason Lightfoot avatar image
0 Likes"
Jason Lightfoot answered Felix Möhlmann commented
function_s(getmodelunit(STOP_TIME_NODE).last,"setModelTime",3000);

sets the stop time to 3000 units.

So to strip all others and enable just the one you set:

treenode stoptimes=getmodelunit(STOP_TIME_NODE);
while(stoptimes.subnodes.length>1)
    stoptimes.last.destroy();
function_s(stoptimes.last,"setModelTime",3000);
setsdtvalue(stoptimes.last,"enabled",1);
· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

iceagesimulator avatar image iceagesimulator commented ·

I used the code provided to change the time. It works to an extent. So let me describe where the shortcoming is.

1. I change a value (either 1 or 2) in a Global Table, in the second row labeled Stop Time

2. Then I run a script which will change the start and stop time, based on the condition of the value in the Global Table.

Upon doing this the "Stop Time" changes in the run time window, however the duration value does not automatically change. And if the model is ran at this point, the model will run to the previous duration amount.

3. In order for the duration number to update to the new stop time, I need to click the drop down arrow on the Run Time window and then the duration amount will update to reflect what the Stop Time shows in the Run Time window.


Is there a work around to get the duration value to automatically update when the Stop Time is programmatically changed? (Without having to manually click the drop down arrow on the Run Time window each time).

flexsim-stoptime2.jpg

SetStopTime_2.fsm

0 Likes 0 ·
Felix Möhlmann avatar image Felix Möhlmann iceagesimulator commented ·

While looking through the code behing the run time controls I found the stoptime() command.

Using that updates the display correctly.

1686230749430.png

1 Like 1 ·
1686230749430.png (30.8 KiB)
iceagesimulator avatar image
0 Likes"
iceagesimulator answered

After taking tidbits from all the helpful answers and comments, I have found a satisfactory way to set the start and stop time programmatically in my model. The below code is what I am using:

//Set model start time
DateTime startTime = DateTime("6/1/2023 07:00:00","%m/%d/%Y %H:%M:%S");
function_s(getmodelunit(START_TIME_NODE), "setDateTime", startTime);
    
//Set model stop time based on calculation to determine model duration
DateTime stopTime = DateTime("6/1/2023 08:05:00","%m/%d/%Y %H:%M:%S");
int s = stopTime.totalSeconds - startTime.totalSeconds;
stoptime(s);

The reason I am using the code like this, is because I can quickly see the DateTime format and know intuitively if the start and stop time is correct without having to do math in my head. The above simple code is then expanded to my actual model, where a user can select which month they want to run the model (12 values ranging from April 2023 to March 2024) from a comboBox drop down in a GUI. Then that drop down menu will change a value in a Global Table and call a user command which houses the following code:

/**Custom Code*/
//delcare variables
DateTime startTime;
DateTime stopTime;
int s;


//set the start and stop time variables, depending on value from table.
//The value in the table changes via the UI comboBox drop down menu.
switch(Table(TBL_MODEL_MODES)[MODE_MONTH_OPTION][1])
{
    case 1: 
        startTime = DateTime("1/1/2024 07:00:00","%m/%d/%Y %H:%M:%S");
        stopTime = DateTime("1/31/2024 17:00:00","%m/%d/%Y %H:%M:%S");
        break;
    case 2: 
        startTime = DateTime("2/1/2024 07:00:00","%m/%d/%Y %H:%M:%S");
        stopTime = DateTime("2/29/2024 17:00:00","%m/%d/%Y %H:%M:%S");
        break;
    case 3: 
        startTime = DateTime("3/1/2024 07:00:00","%m/%d/%Y %H:%M:%S");
        stopTime = DateTime("3/29/2024 17:00:00","%m/%d/%Y %H:%M:%S");
        break;
    case 4: 
        startTime = DateTime("4/3/2023 07:00:00","%m/%d/%Y %H:%M:%S");
        stopTime = DateTime("4/28/2023 17:00:00","%m/%d/%Y %H:%M:%S");
        break;
    case 5: 
        startTime = DateTime("5/1/2023 07:00:00","%m/%d/%Y %H:%M:%S");
        stopTime = DateTime("5/31/2023 17:00:00","%m/%d/%Y %H:%M:%S");
        break;
    case 6: 
        startTime = DateTime("6/1/2023 07:00:00","%m/%d/%Y %H:%M:%S");
        stopTime = DateTime("6/30/2023 17:00:00","%m/%d/%Y %H:%M:%S");
        break;
    case 7: 
        startTime = DateTime("7/3/2023 07:00:00","%m/%d/%Y %H:%M:%S");
        stopTime = DateTime("7/31/2023 17:00:00","%m/%d/%Y %H:%M:%S");
        break;
    case 8: 
        startTime = DateTime("8/1/2023 07:00:00","%m/%d/%Y %H:%M:%S");
        stopTime = DateTime("8/31/2023 17:00:00","%m/%d/%Y %H:%M:%S");
        break;
    case 9: 
        startTime = DateTime("9/1/2023 07:00:00","%m/%d/%Y %H:%M:%S");
        stopTime = DateTime("9/30/2023 17:00:00","%m/%d/%Y %H:%M:%S");
        break;
    case 10: 
        startTime = DateTime("10/2/2023 07:00:00","%m/%d/%Y %H:%M:%S");
        stopTime = DateTime("10/31/2023 17:00:00","%m/%d/%Y %H:%M:%S");
        break;
    case 11: 
        startTime = DateTime("11/1/2023 07:00:00","%m/%d/%Y %H:%M:%S");
        stopTime = DateTime("11/30/2023 17:00:00","%m/%d/%Y %H:%M:%S");
        break;
    case 12: 
        startTime = DateTime("12/1/2023 07:00:00","%m/%d/%Y %H:%M:%S");
        stopTime = DateTime("12/31/2023 17:00:00","%m/%d/%Y %H:%M:%S");
        break;
    default:
        startTime = DateTime("4/1/2023 07:00:00","%m/%d/%Y %H:%M:%S");
        stopTime = DateTime("4/30/2023 17:00:00","%m/%d/%Y %H:%M:%S");
}


//set the start time of the model
function_s(getmodelunit(START_TIME_NODE), "setDateTime", startTime);
//calculate the duration, in seconds, that the model should run
s = stopTime.totalSeconds - startTime.totalSeconds;
//set the stop time of the model
stoptime(s);
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jordan Johnson avatar image
0 Likes"
Jordan Johnson answered

Another alternative would be to use the stop() command to stop the model, instead of setting a stop time.

Approach 1: User Events

You can create a User Event at a specific time, and then use an if statement to check if the model should stop or not at that time.

https://docs.flexsim.com/en/23.1/Reference/Tools/UserEvents/UserEvents.html

Approach 2: Process Flow

You can also use a Scheduled Source in Process Flow to create a token at a specific time. That token could visit a Custom Code activity that calls the stop() command. Something like this might work:

1686231492998.png

Also, consider reading this documentation on creating events from table rows (assuming your stop times are stored in a global table):

https://docs.flexsim.com/en/23.1/ModelLogic/GlobalTables/UsingData/UsingData.html#creatingevents


1686231492998.png (75.3 KiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.