Skip to content

Commit

Permalink
Add plotting functions for orbits and impacts (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
akoumjian authored Feb 20, 2025
1 parent 9dc5060 commit 20aa441
Show file tree
Hide file tree
Showing 13 changed files with 3,196 additions and 77 deletions.
143 changes: 70 additions & 73 deletions examples/2024_yr4_impact_risk.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,141 +17,138 @@
"Before we can start, you will need to install a couple packages.\n",
"\n",
"```bash\n",
"pip install adam-core adam-assist\n",
"pip install adam-core[plots] adam-assist\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" orbit_id object_id coordinates.x coordinates.y coordinates.z \\\n",
"0 00000 (2024 YR4) -0.554305 1.039765 -0.031525 \n",
"\n",
" coordinates.vx coordinates.vy coordinates.vz coordinates.time.days \\\n",
"0 -0.019574 -0.000133 -0.001166 60693 \n",
"\n",
" coordinates.time.nanos coordinates.covariance.values \\\n",
"0 0 [2.6790681662875186e-12, -4.746648923989143e-1... \n",
"\n",
" coordinates.origin.code \n",
"0 SUN \n"
]
}
],
"outputs": [],
"source": [
"# Fetch the state vector from Small Bodies Database (SBDB)\n",
"from adam_core.orbits.query import query_sbdb\n",
"\n",
"yr4 = query_sbdb([\"2024 YR4\"])\n",
"orbit = query_sbdb([\"2024 YR4\"])\n",
"\n",
"print(yr4.to_dataframe())"
"print(orbit.to_dataframe())"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\n",
" 2925\n",
"]\n"
]
}
],
"outputs": [],
"source": [
"# We need to know how many days out to run the propagation,\n",
"# relative to when the orbit state vector is defined.\n",
"# Let's go 30 days past the possible 2032 impact of 2024 YR4\n",
"from adam_core.time import Timestamp\n",
"approx_impact_date = Timestamp.from_iso8601([\"2032-12-22\"], scale=\"tdb\")\n",
"thirty_days_after_impact = approx_impact_date.add_days(30)\n",
"days_until_thirty_days_after_impact, _ = thirty_days_after_impact.difference(yr4.coordinates.time)\n",
"days_until_thirty_days_after_impact, _ = thirty_days_after_impact.difference(orbit.coordinates.time)\n",
"print(days_until_thirty_days_after_impact)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"EarthImpacts(size=19)\n"
]
}
],
"outputs": [],
"source": [
"# Now we initialize our propagator, and pass it to our calculate_impacts function\n",
"from adam_core.dynamics.impacts import calculate_impacts\n",
"from adam_assist import ASSISTPropagator\n",
"\n",
"propagator = ASSISTPropagator()\n",
"\n",
"final_states, earth_impacts = calculate_impacts(\n",
" yr4,\n",
"variants, impacts = calculate_impacts(\n",
" orbit,\n",
" days_until_thirty_days_after_impact[0].as_py(),\n",
" propagator,\n",
" num_samples=1000,\n",
" num_samples=10000,\n",
" processes=10, # Multiprocessing speeds things up if you have the CPUs\n",
")\n",
"\n",
"print(earth_impacts)"
"print(variants, impacts)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" orbit_id impacts variants cumulative_probability mean_impact_time.days \\\n",
"0 00000 19 1000 0.019 63588 \n",
"\n",
" mean_impact_time.nanos stddev_impact_time minimum_impact_time.days \\\n",
"0 50517116965167 0.00501 63588 \n",
"\n",
" minimum_impact_time.nanos maximum_impact_time.days \\\n",
"0 49983530294895 63588 \n",
"\n",
" maximum_impact_time.nanos \n",
"0 51346236683428 \n"
]
}
],
"outputs": [],
"source": [
"# Now we can summarize the returns, a simple ratio in our case\n",
"# with only 1 orbit considered\n",
"from adam_core.dynamics.impacts import calculate_impact_probabilities\n",
"ip = calculate_impact_probabilities(final_states, earth_impacts)\n",
"ip = calculate_impact_probabilities(variants, impacts)\n",
"print(ip.to_dataframe())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And there you have it. "
"We've recently added some visualization functionality to visualize both the risk corridor and the impact events. Let's first look at the impact corridor."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from adam_core.dynamics.plots import plot_risk_corridor\n",
"\n",
"fig = plot_risk_corridor(impacts, title=\"Risk Corridor for 2024 YR4\")\n",
"fig.show()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from adam_core.dynamics.plots import generate_impact_visualization_data\n",
"\n",
"propagated_best_fit_orbit, propagated_variants = generate_impact_visualization_data(\n",
" orbit,\n",
" variants,\n",
" impacts,\n",
" propagator,\n",
" time_step=5,\n",
" time_range=120,\n",
" max_processes=None\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from adam_core.dynamics.plots import plot_impact_simulation\n",
"\n",
"fig = plot_impact_simulation(\n",
" propagated_best_fit_orbit, \n",
" propagated_variants, \n",
" impacts, \n",
" grid=True, \n",
" title=\"2024 YR4 Impact Simulation\",\n",
")\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
"source": [
"And there you have it. "
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 20aa441

Please sign in to comment.