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 pathlib import Path
from threedi_api_client.api import ThreediApi
from threedi_api_client.versions import V3Api
from threedi_api_client.files import download_file

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

PERSONAL_API_TOKEN = "your_personal_api_key_here"


config = {
    "THREEDI_API_HOST": API_HOST,
    "THREEDI_API_PERSONAL_API_TOKEN": PERSONAL_API_TOKEN
}

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
    download_file(download_url.get_url, file_path)
    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"

download_file(download_url.get_url, file_path)

print(f"Finished downloading gridadmin.h5")