Java Reference
In-Depth Information
=
1 / 2 m
v
2
=
1 / 2 kx 2 .
The kinetic energy is K
and the potential energy is P
So
data[4] = 0.5f * fMass * data[2]*data[2];
data[5] = 0.5f * fSpringCons * data[1]*data[1];
Once these values are calculated, we only need to store them in the SimData
object:
fSimData.storeData (data);
Then we return to the top of the while() loop and continue with the next
iteration.
There are a few minor details to deal with before we close out the start-
Simulation() method. First, recall that the simulation loads its data into
fSimData each time step and that the client periodically polls the server for
new data. During each client poll, we have arranged for the client to send a new
array of input data. For example, the client may wish to change the frequency
ω
used in the simulation, and we need a mechanism to communicate that change
to the running simulation. We haven't yet discussed the storeData() method,
but the most convenient way for the simulation to obtain a new input data array
from the client is as the return value when the simulation calls storeData() .
Therefore, we modify the previous line of code to make use of that return value.
That is, the code becomes
new - indata = fSimData.storeData (data);
where new - indata is a float[] array. To avoid confusion, we use the same
order of input data array elements as used during initializeSimulation() .
Perhaps some of those values should not be varied, in which case a completely
different array could be used. Instead of defining another array and another agree-
ment about the order of the data values, we simply agree by convention that only
some of the array values are subject to be changed. If any of the other values are
changed, the server is free to ignore them.
Some of the parameters that could be changed in this simulation are the ampli-
tude, the spring constant, or the frequency. Obviously a real spring would not
change its spring constant or natural frequency, but a more realistic simula-
tion might. One application for which this technique has been used simulates
blood flow in a human heart. Depending upon external stimuli, the heart rate can
change, corresponding to a change in frequency. In that case, the input array of
data contains a representation of the external stimuli that the server-side calcula-
tion uses. When the input data changes, the simulation responds and the heart rate
changes.
For illustration purposes we allow resetting A ,
ω
, and K but do not per-
mit changing
T or T max . Therefore we read only the first three values of the
Search WWH ::




Custom Search