How to measure time for which resource is fully occupied? - simulation

I have a resource named "Docks", its capacity is 14, I want to calculate the total time for which all the 14 docks are busy? Is there any way, please help.

I will propose a solution that may not be the most efficient one. However, it will at least guide you in the right direction. One way to do it is to create an event with the following code, noting that timeBusy would be a variable of type double:
if( resourcePool.busy() == resourcePool.size() ) {
timeBusy++;
}
Make the event of type cyclic. If you make it cyclic every second, the timeBusy variable would give you the time all resources are busy in seconds. The issue with this method is:
The smaller the cycle time, the slower the model becomes (although this line of code may not be that of an issue)
The bigger the cycle time, the lower the accuracy of the measured time.
Alternatively, a more advanced solution would be to use the On Seize and On Release fields of the resource pool, where you can add your code to check the number of busy units each time a resource is seized or released only and not every cycle time. The main idea of this method would be to use three variables: startTime and totalTime of type double and start of type boolean with an initial value of false. So On Seize you would write:
if( resourcePool.busy() == resourcePool.size() && !start ) {
startTime = time();
start = true;
}
And On Release:
if( resourcePool.busy() == resourcePool.size() - 1 && start ) {
totalTime += time() - startTime;
start = false;
}
You may need to validate the results, but this should at least put you in the right direction.
The only downside of this method is that results are only updated when a resource is seized or released. So if for example, all your resources are seized for a long time, you may not see any changes to the variables.
Depending on your model, you can judge which method works better for you.
One final note is that you can opt to have a hybrid method to ensure live updates and 100% accuracy.
I would personally go for the first method if your model is relatively simple.

Related

How can I be sure that I am accessing the correct value to use in Swift

This is a basic question for me, I want to know, can I be 100% sure that after updating a variable to new value is 100% safe to access and read it in next line of code? or maybe it would take some internal delay of processing and it is not safe to use in this way.
The use case would be updating and reading values that they are happing in very very short time almost zero nano sec delay like gestures.
for example I want update value2 with exact same value of value1, but I do not know it is a safe way, therefore I am using second function that I am declaring a constant for updating value1 and value2. The downside of second function as you know is using an extra constant.
var value1: Int = 0
var value2: Int = 0
func valueFunction1() {
value1 = 50
value2 = value1
}
func valueFunction2() {
let value: Int = 50
value1 = value
value2 = value
}
These questions come up when you are writing concurrent code (code that runs on multiple threads at the same time.) In that case you have to take special precautions to make sure another thread doesn't change values out from under your running code.
Assuming you are not writing multi-threaded code (And based on the very basic nature of your questions, you should avoid that for now) you can be absolutely certain that variables take on their new values as soon as your change them. Any given function runs "synchronously" (top to bottom) and finishes each line before moving on to the next. (Compilers and CPUs can play some games with your code that stretch those rules a little, but the burden is on the compiler to make sure that variables have the correct values by the time the next line is executed.)

Call a function every X seconds when activated - Scala

I have a variable which is going to help me check if 10 seconds has passed.
private val currTime: Long = System.currentTimeMillis()
private val time: Long = 10000
I want something where, if a function foo is called, it will reset currTime to be the current time. That's the easy part which I can do. The tough part, and part 2 of this, is that if currTime at any point exceeds time, I want to change a boolean (say isHappy) from true to false - this can be in a function called bar.
I come from C#, so i'm not sure how to do this - typically we'd use a dispatcher timer but i have no idea how to achieve this is Scala.
I've seen scheduler examples and fixed interval examples, but this is more of a moving target... a condition that can be met at any time.
In simpler terms, it's like having 2 ints. Int A and Int B. When the sum of A and B is over 10, a function is triggered. A and B are forever changing in different functions in that class. How can we create a trigger?

What is the order that reactive statements are executed in?

Let's say we have the following component script
let x = 0;
function increase() {
x += 1;
}
$: console.log(x)
$: if (x == 4) {
x = 0;
}
https://svelte.dev/repl/2bca979673114afea9fc6b37434653a3?version=3.29.0
Intuitively I would expect from cotinually calling increase, is to have the values 0,1,2,3,4,1,2,3,4,... logged to the console. But this is not the case. Instead, the second reactive statement runs first, and 4 is never logged; it is 0. Switching the order of the reactive statements has no effect.
How do you make the logging statement execute before the other one and what is the reason that it runs second, in the first place?
The order in which reactive statements that depends on the same variables are run is not specified / guaranteed by Svelte. You have to build your components with the expectation that they can be run in any order, and make sure that your code doesn't depend on any specific order.
You must not rely on a given order you observe in a specific case either -- it can change following a change in Svelte's internal implementation (e.g. new optimisations), or when the code of your own component changes (causing a different compiler output, that can change the order of reactive blocks for hard-to-anticipate reasons).
The reasons why this happens are highly technical (i.e. I don't really know), but they boil down to ease of implementation / optimization of the compiler. Reactive statements are a kind of async operations, that typically can't guarantee order of execution.
If you really need 2 operations to happen in a specific order then, for peace of mind, you need them in the same reactive block.
$: {
console.log(x) // op 1
if (x == 4) x = 0 // op 2
}

Pause JModelica and Pass Incremental Inputs During Simulation

Hi Modelica Community,
I would like to run two models in parallel in JModelica but I'm not sure how to pass variables between the models. One model is a python model and the other is an EnergyPlusToFMU model.
The examples in the JModelica documentation has the full simulation period inputs defined prior to the simulation of the model. I don't understand how one would configure a model that pauses for inputs, which is a key feature of FMUs and co-simulation.
Can someone provide me with an example or piece of code that shows how this could be implemented in JModelica?
Do I put the simulate command in a loop? If so, how do I handle warm up periods and initialization without losing data at prior timesteps?
Thank you for your time,
Justin
Late answer, but in case it is picked up by others...
You can indeed put the simulation into a loop, you just need to keep track of the state of your system, such that you can re-init it at every iteration. Consider the following example:
Ts = 100
x_k = x_0
for k in range(100):
# Do whatever you need to get your input here
u_k = ...
FMU.reset()
FMU.set(x_k.keys(), x_k.values())
sim_res = FMU.simulate(
start_time=k*Ts,
final_time=(k+1)*Ts,
input=u_k
)
x_k = get_state(sim_res)
Now, I have written a small function to grab the state, x_k, of the system:
# Get state names and their values at given index
def get_state(fmu, results, index):
# Identify states as variables with a _start_ value
identifier = "_start_"
keys = fmu.get_model_variables(filter=identifier + "*").keys()
# Now, loop through all states, get their value and put it in x
x = {}
for name in keys:
x[name] = results[name[len(identifier):]][index]
# Return state
return x
This relies on setting "state_initial_equations": True compile option.

Slow Performance When Using Scalaz Task

I'm looking to improve the performance of a Monte Carlo simulation I am developing.
I first did an implementation which does the simulation of each paths sequentially as follows:
def simulate() = {
for (path <- 0 to 30000) {
(0 to 100).foreach(
x => // do some computation
)
}
}
This basically is simulating 30,000 paths and each path has 100 discretised random steps.
The above function runs very quickly on my machine (about 1s) for the calculation I am doing.
I then thought about speeding it up even further by making the code run in a multithreaded fashion.
I decided to use Task for this and I coded the following:
val simulation = (1 |-> 30000 ).map(n => Task {
(1 |-> 100).map(x => // do some computation)
})
I then use this as follows:
Task.gatherUnordered(simulation).run
When I kick this off, I know my machine is doing a lot of work as I can
see that in the activity monitor and also the machine fan is going ballistic.
After about two minutes of heavy activity on the machine, the work it seems
to be doing finishes but I don't get any value returned (I am expected a collection
of Doubles from each task that was processed).
My questions are:
Why does this take longer than the sequential example? I am more
than likely doing something wrong but I can't see it.
Why don't I get any returned collection of values from the tasks that are apparently being processed?
I'm not sure why Task.gatherUnordered is so slow, but if you change Task.gatherUnordered to Nondeterminism.gatherUnordered everything will be fine:
import scalaz.Nondeterminism
Nondeterminism[Task].gatherUnordered(simulation).run
I'm going to create an issue on Github about Task.gatherUnordered. This definitely should be fixed.