Skip to content

Part III - Download simulation results

Download the jupyter notebook

Simulation results💡

Once your simulation has finished, you probably want to analyse the results with, for example, the ThreediToolbox or threedigrid. In this notebook we'll show you how you can download the result files of a 3Di simulation.

Let's first create the client to connect to the threedi-api:

import requests
from getpass import getpass
from pathlib import Path
from threedi_api_client.api import ThreediApi
from threedi_api_client.versions import V3Api


# Provide authentication details
API_HOST = "https://api.3di.live"
USERNAME = input("Username")
PASSWORD = getpass("Password")

config = {
    "THREEDI_API_HOST": API_HOST,
    "THREEDI_API_USERNAME": USERNAME,
    "THREEDI_API_PASSWORD": PASSWORD
}

api_client: V3Api = ThreediApi(config=config)

Now let's select a simulation we're interested in. Make sure it is finished simulating. We will select the simulation we created in the previous notebook.

simulation = api_client.simulations_list(
    name="my simulation with events", user__username=USERNAME
).results[-1]
status = api_client.simulations_status_list(simulation.id)

print(simulation)
print(f"status: {status}")
assert status.name == 'finished'

Now that we have our finished simulation, let's see which result files are available:

result_files = api_client.simulations_results_files_list(simulation.id)

for result in result_files.results:
    print(result)

Let's download all the results so we can analyse them locally:

download_folder = Path(f'Results {simulation.name}')
download_folder.mkdir(exist_ok=True)

for file in result_files.results:
    download_url = api_client.simulations_results_files_download(
        id=file.id, simulation_pk=simulation.id
    )

    file_path = download_folder / file.filename
    r = requests.get(download_url.get_url)
    with open(file_path, 'wb') as f:
        for chunk in r.iter_content(chunk_size=8192):
            f.write(chunk)

    print(f"Finished downloading {file.filename}")

The ThreediToolbox and threedigrid also require the "gridadmin.h5" file of the simulation. This is a model specific file so it's under the threedimodels-api. We'll also download this file:

threedi_model_id = simulation.threedimodel_id
download_url = api_client.threedimodels_gridadmin_download(threedi_model_id)

file_path = download_folder / "gridadmin.h5"
r = requests.get(download_url.get_url)
with open(file_path, "wb") as f:
    for chunk in r.iter_content(chunk_size=8192):
        f.write(chunk)

print(f"Finished downloading gridadmin.h5")