#!/usr/bin/env bash # Stop on failed commands, unset variables, or failed pipeline stages. set -euo pipefail : "${VANELLUS_API_KEY:?Set VANELLUS_API_KEY before running this script}" request_file="${1:-request.json}" result_file="${2:-simulation-results.zip}" # Validate local paths before submitting a simulation that may use credits. if [ ! -f "$request_file" ]; then echo "Request file does not exist: $request_file" >&2 exit 1 fi if [ -e "$result_file" ]; then echo "Result file already exists: $result_file" >&2 exit 1 fi # Submit the request and retain any accepted-request warnings for the user. submission="$(curl --fail-with-body --silent --show-error \ -H "X-API-Key: $VANELLUS_API_KEY" \ -H "Content-Type: application/json" \ --data-binary "@$request_file" \ "https://api.vanellus.tech/simulations")" if [ "$(printf '%s\n' "$submission" | jq -r '(.warnings // []) | length')" -gt 0 ]; then echo "The API accepted the request with warnings:" >&2 printf '%s\n' "$submission" | jq '.warnings' >&2 fi simulation_id="$(printf '%s\n' "$submission" | jq -er '.id')" echo "Submitted simulation $simulation_id" # Poll at a restrained interval until the run reaches a terminal state. while true; do status_document="$(curl --fail-with-body --silent --show-error \ -H "X-API-Key: $VANELLUS_API_KEY" \ "https://api.vanellus.tech/simulations/$simulation_id/status")" run_status="$(printf '%s\n' "$status_document" | jq -r '.status')" iteration="$(printf '%s\n' "$status_document" | jq -r '.num_simple_iters // 0')" echo "$run_status after $iteration iterations" # Failed terminal states do not have a result bundle and must stop polling. case "$run_status" in diverged|error|killed) printf '%s\n' "$status_document" | jq . >&2 exit 1 ;; esac if [ "$(printf '%s\n' "$status_document" | jq -r '.completed')" = "true" ]; then break fi sleep 5 done # Accept normal completion states and fail before download on solver failure. case "$run_status" in dry_run|residual_converged|monitor_converged) ;; max_iterations_reached) echo "Warning: the iteration limit was reached before convergence." >&2 ;; canceled) echo "Warning: downloading the partial result from a canceled simulation." >&2 ;; *) printf '%s\n' "$status_document" | jq . >&2 exit 1 ;; esac # Download every available result item into one ZIP archive. curl --fail-with-body --silent --show-error \ -H "X-API-Key: $VANELLUS_API_KEY" \ "https://api.vanellus.tech/simulations/$simulation_id/download" \ --output "$result_file" echo "Downloaded $result_file"