Free fall
Example 2: Velocity due to gravtity in free fall¶
The second example calculates the velocity of an object falling from the sky (without air resistance).
The standard gravity on earth is 9.81 m*s^(-2). Multiplicated by the fall time, we will get the velocity of the object after that time.
If you like equations more, you might recognize these from your physics class: $$ v = g \cdot t $$
In regular python code that you run locally it would look like this:
free_fall.py | |
---|---|
1 2 3 4 5 |
|
Now we want to be able to share that functionality via the lmrtfy web API. All we have to do is decide which variables are considered to be an input and a result of the computation:
free_fall_lmrtfy.py | |
---|---|
1 2 3 4 5 6 7 |
|
Now run $ python examples/velocity_from_gravity/calc_velocity.py
to generate the required profile.
This way you can also check if your code is actually working the way you expect it.
Deploying the script¶
To deploy, you simply run $ lmrtfy deploy examples/velocity_from_gravity/calc_velocity.py --local
.
Do not stop that process, because than you will not be able to submit a job.
Calling from code¶
Calling free_fall_lmrtfy
by code as easy as it was for the first example.
calc_free_fall.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 |
|
<your_namespace>
is your private namespace on LMRTFY, which is typically your nickname. Available namespaces are shown when importingcatalog
or when callingcatalog.update()
.
Note
You can also run help(free_fall_lmrtfy)
to see the corresponding help. Right now, only the
function signature is shown but in the future you will also be able to see the docstrings.
Calling from CLI¶
Open a new terminal in the same directory and run $ lmrtfy submit <profile_id>
. The profile_id has been
printed in the lmrtfy deploy
step. This does not work right out of the box, because you need to
specify a JSON file that contains the input parameters for your job. A template for that JSON should
have been printed in the CLI.
Create such a JSON file and name it input.json
and put values of the correct type into the values (no type conversion is
happening in the API, so if float
is required, you cannot input an int
). Alternatively, use
the provided input.json
in examples/free_fall/input.json
:
{
"argument_values": {
"time": 6.0
},
"argument_units" : {
"time": "s"
}
}
Now run $ lmrtfy submit <profile_id> examples/free_fall/input.json
. You will receive a job_id
which we will shortly need to fetch the results after they are computed.
After your job has run, you can get the results by running $ lmrtfy fetch <job_id>
<path to store results>
.
The results are downloaded and stored inside the specified path within a directory that has the
job_id
as its name.