Skip to content

Commit

Permalink
Fix small things in notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
GijsVermarien committed Oct 12, 2024
1 parent 70fe115 commit 7b0b279
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 36 deletions.
18 changes: 8 additions & 10 deletions notebooks/1_first_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
},
"outputs": [],
"source": [
"import uclchem"
"import uclchem\n",
"import os"
]
},
{
Expand Down Expand Up @@ -53,6 +54,10 @@
" \"outputFile\": \"../examples/test-output/static-full.dat\", # full UCLCHEM output\n",
" \"abundSaveFile\": \"../examples/test-output/startstatic.dat\", # save final abundances to file\n",
"}\n",
"# Ensure the output directory is present:\n",
"if not os.path.exists(\"../examples/test-output/\"):\n",
" os.makedirs(\"../examples/test-output/\")\n",
"\n",
"result = uclchem.model.cloud(param_dict=param_dict, out_species=out_species)\n",
"print(result)"
]
Expand Down Expand Up @@ -87,7 +92,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also test whether the model run went well by checking for element conservation. We do this because integrator errors often show up as a failure to conserve elemental abundances. \n",
"We can also test whether the model run went well by checking for element conservation. We do this because integrator errors often show up as a failure to conserve elemental abundances.\n",
"\n",
"We can use `check_element_conservation()` to test whether we conserve elements in this run. This function returns a dictionary where each entry gives the change in the total abundance of an element as a percentage of the original abundance. In an ideal case, these values are 0\\% indicating the total abundance at the end of the model is exactly the same as the total at the start.\n",
"\n",
Expand Down Expand Up @@ -136,21 +141,14 @@
"source": [
"and that's it! You've run your first UCLCHEM model, checked that the element conservation is correct, and plotted the abundances."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"interpreter": {
"hash": "40d3a090f54c6569ab1632332b64b2c03c39dcf918b08424e98f38b5ae0af88f"
},
"jupytext": {
"formats": "ipynb,auto:light"
"formats": "ipynb,py:light"
},
"kernelspec": {
"display_name": "UCLCHEM 3.4.0 Release Candidate",
Expand Down
5 changes: 5 additions & 0 deletions notebooks/1_first_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# In this notebook, we demonstrate the basic use of UCLCHEM's python module by running a simple model and then using the analysis functions to examine the output. Otherwise, it is identical to notebook 3.

import uclchem
import os

# ## A Simple Cloud
#
Expand All @@ -41,6 +42,10 @@
"outputFile": "../examples/test-output/static-full.dat", # full UCLCHEM output
"abundSaveFile": "../examples/test-output/startstatic.dat", # save final abundances to file
}
# Ensure the output directory is present:
if not os.path.exists("../examples/test-output/"):
os.makedirs("../examples/test-output/")

result = uclchem.model.cloud(param_dict=param_dict, out_species=out_species)
print(result)
# -
Expand Down
13 changes: 8 additions & 5 deletions notebooks/2a_modelling_objects_on_disk.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"outputs": [],
"source": [
"import uclchem\n",
"import matplotlib.pyplot as plt"
"import matplotlib.pyplot as plt\n",
"import os"
]
},
{
Expand All @@ -30,7 +31,7 @@
"### Initial Conditions (Phase 1)\n",
"UCLCHEM typically starts with the gas in atomic/ionic form with no molecules. However, this clearly is not appropriate when modelling an object such as a hot core. In these objects, the gas is already evolved and there should be molecules in the gas phase as well as ice mantles on the dust. To allow for this, one must provide some initial abundances to the model. There are many ways to do this but we typically chose to run a preliminary model to produce our abundances. In many UCLCHEM papers, we refer to the preliminary model as *phase 1* and the science model as *phase 2*. Phase 1 simply models a collapsing cloud and phase 2 models the object in question.\n",
"\n",
"To do this, we will use `uclchem.model.cloud()` to run a model where a cloud of gas collapses from a density of $10^2 cm^{-3}$ to our hot core density of $10^6 cm^{-3}$, keeping all other parameters constant. During this collapse, chemistry will occur and we can assume the final abundances of this model will be reasonable starting abundances for the hot core. "
"To do this, we will use `uclchem.model.cloud()` to run a model where a cloud of gas collapses from a density of $10^2 cm^{-3}$ to our hot core density of $10^6 cm^{-3}$, keeping all other parameters constant. During this collapse, chemistry will occur and we can assume the final abundances of this model will be reasonable starting abundances for the hot core."
]
},
{
Expand All @@ -54,6 +55,8 @@
" \"abundSaveFile\": \"../examples/test-output/startcollapse.dat\", # save final abundances to file\n",
" \"outputFile\": \"../examples/test-output/phase1-full.dat\",\n",
"}\n",
"if not os.path.exists(\"../examples/test-output/\"):\n",
" os.makedirs(\"../examples/test-output/\")\n",
"result = uclchem.model.cloud(param_dict=param_dict)\n",
"print(result)"
]
Expand All @@ -66,7 +69,7 @@
"\n",
"### Running the Science Model (Phase 2)\n",
"\n",
"We need to change just a few things in `param_dict` to set up the hot core model. The key one is that UCLCHEM saves final abundances to `abundSaveFile` but loads them from `abundLoadFile` so we need to swap that key over to make the abundances we just produced our initial abundances. \n",
"We need to change just a few things in `param_dict` to set up the hot core model. The key one is that UCLCHEM saves final abundances to `abundSaveFile` but loads them from `abundLoadFile` so we need to swap that key over to make the abundances we just produced our initial abundances.\n",
"\n",
"We also want to turn off freefall and change how long the model runs for.\n"
]
Expand Down Expand Up @@ -377,7 +380,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"That's everything! We've run various science models using reasonable starting abundances that we produced by running a simple UCLCHEM model beforehand. One benefit of this method is that the abundances are consistent with the network. If we start with arbitrary, perhaps observationally motivated, abundances, it would be possible to initiate the model in a state our network could never produce. \n",
"That's everything! We've run various science models using reasonable starting abundances that we produced by running a simple UCLCHEM model beforehand. One benefit of this method is that the abundances are consistent with the network. If we start with arbitrary, perhaps observationally motivated, abundances, it would be possible to initiate the model in a state our network could never produce.\n",
"\n",
"However, one should be aware of the limitations of this method. A freefall collapse from low density to high is not really how a molecular cloud forms and so the abundances are only approximately similar to values they'd truly have in a real cloud. Testing whether your results are sensitive to things like the time you run the preliminary for or the exact density is a good way to make sure these approximations are not problematic.\n",
"\n",
Expand All @@ -390,7 +393,7 @@
"hash": "40d3a090f54c6569ab1632332b64b2c03c39dcf918b08424e98f38b5ae0af88f"
},
"jupytext": {
"formats": "ipynb,auto:light"
"formats": "ipynb,py:light"
},
"kernelspec": {
"display_name": "UCLCHEM 3.4.0 Release Candidate",
Expand Down
5 changes: 3 additions & 2 deletions notebooks/2a_modelling_objects_on_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import uclchem
import matplotlib.pyplot as plt
import os

# ## The Hot Core
#
Expand All @@ -27,7 +28,6 @@
#
# To do this, we will use `uclchem.model.cloud()` to run a model where a cloud of gas collapses from a density of $10^2 cm^{-3}$ to our hot core density of $10^6 cm^{-3}$, keeping all other parameters constant. During this collapse, chemistry will occur and we can assume the final abundances of this model will be reasonable starting abundances for the hot core.

# +
# set a parameter dictionary for cloud collapse model
param_dict = {
"endAtFinalDensity": False, # stop at finalTime
Expand All @@ -41,9 +41,10 @@
"abundSaveFile": "../examples/test-output/startcollapse.dat", # save final abundances to file
"outputFile": "../examples/test-output/phase1-full.dat",
}
if not os.path.exists("../examples/test-output/"):
os.makedirs("../examples/test-output/")
result = uclchem.model.cloud(param_dict=param_dict)
print(result)
# -

# With that done, we now have a file containing the final abundances of a cloud of gas after this collapse: `param_dict["abundSaveFile"]` we can pass this to our hot core model to use those abundances as our initial abundances.
#
Expand Down
21 changes: 6 additions & 15 deletions notebooks/2b_modelling_objects_in_memory.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
"source": [
"import uclchem\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"\n",
"# PLease run the notebook on the develop branch only :D jshock still has a bug with timepoints being 1 point too small as of 10-10-2024 (I might have fixed it, Gijs)."
"import pandas as pd"
]
},
{
Expand All @@ -33,7 +31,7 @@
"### Initial Conditions (Phase 1)\n",
"UCLCHEM typically starts with the gas in atomic/ionic form with no molecules. However, this clearly is not appropriate when modelling an object such as a hot core. In these objects, the gas is already evolved and there should be molecules in the gas phase as well as ice mantles on the dust. To allow for this, one must provide some initial abundances to the model. There are many ways to do this but we typically chose to run a preliminary model to produce our abundances. In many UCLCHEM papers, we refer to the preliminary model as *phase 1* and the science model as *phase 2*. Phase 1 simply models a collapsing cloud and phase 2 models the object in question.\n",
"\n",
"To do this, we will use `uclchem.model.cloud()` to run a model where a cloud of gas collapses from a density of $10^2 cm^{-3}$ to our hot core density of $10^6 cm^{-3}$, keeping all other parameters constant. During this collapse, chemistry will occur and we can assume the final abundances of this model will be reasonable starting abundances for the hot core. "
"To do this, we will use `uclchem.model.cloud()` to run a model where a cloud of gas collapses from a density of $10^2 cm^{-3}$ to our hot core density of $10^6 cm^{-3}$, keeping all other parameters constant. During this collapse, chemistry will occur and we can assume the final abundances of this model will be reasonable starting abundances for the hot core."
]
},
{
Expand Down Expand Up @@ -78,7 +76,7 @@
"\n",
"### Running the Science Model (Phase 2)\n",
"\n",
"We need to change just a few things in `param_dict` to set up the hot core model. The key one is that UCLCHEM saves final abundances to `abundSaveFile` but loads them from `abundLoadFile` so we need to swap that key over to make the abundances we just produced our initial abundances. \n",
"We need to change just a few things in `param_dict` to set up the hot core model. The key one is that UCLCHEM saves final abundances to `abundSaveFile` but loads them from `abundLoadFile` so we need to swap that key over to make the abundances we just produced our initial abundances.\n",
"\n",
"We also want to turn off freefall and change how long the model runs for.\n"
]
Expand Down Expand Up @@ -350,7 +348,7 @@
"metadata": {},
"source": [
"### J-shock\n",
"Running a j-shock is a simple case of changing function. We'll run a 10 km s $^{-1}$ shock through a gas of density $10^3$ cm $^{-3}$ gas this time. Note that nothing stops us using the intial abundances we produced for the c-shock. UCLCHEM will not check that the initial density matches the density of the `abundLoadFile`. It may not always be a good idea to do this but we should remember the intial abundances really are just a rough approximation. \n",
"Running a j-shock is a simple case of changing function. We'll run a 10 km s $^{-1}$ shock through a gas of density $10^3$ cm $^{-3}$ gas this time. Note that nothing stops us using the intial abundances we produced for the c-shock. UCLCHEM will not check that the initial density matches the density of the `abundLoadFile`. It may not always be a good idea to do this but we should remember the intial abundances really are just a rough approximation.\n",
"\n",
"By default UCLCHEM uses 500 timepoints for a model, but this turns out not be enough, which is why we increase the number of timepoints to 1500."
]
Expand Down Expand Up @@ -443,27 +441,20 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"That's everything! We've run various science models using reasonable starting abundances that we produced by running a simple UCLCHEM model beforehand. One benefit of this method is that the abundances are consistent with the network. If we start with arbitrary, perhaps observationally motivated, abundances, it would be possible to initiate the model in a state our network could never produce. \n",
"That's everything! We've run various science models using reasonable starting abundances that we produced by running a simple UCLCHEM model beforehand. One benefit of this method is that the abundances are consistent with the network. If we start with arbitrary, perhaps observationally motivated, abundances, it would be possible to initiate the model in a state our network could never produce.\n",
"\n",
"However, one should be aware of the limitations of this method. A freefall collapse from low density to high is not really how a molecular cloud forms and so the abundances are only approximately similar to values they'd truly have in a real cloud. Testing whether your results are sensitive to things like the time you run the preliminary for or the exact density is a good way to make sure these approximations are not problematic.\n",
"\n",
"Bear in mind that you can use `abundSaveFile` and `abundLoadFile` in the same model run. This lets you chain model runs together. For example, you could run a c-shock from a cloud model as we did here and then a j-shock with the c-shock's abundances as the initial abundances."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"interpreter": {
"hash": "40d3a090f54c6569ab1632332b64b2c03c39dcf918b08424e98f38b5ae0af88f"
},
"jupytext": {
"formats": "ipynb,auto:light"
"formats": "ipynb,py:light"
},
"kernelspec": {
"display_name": "UCLCHEM 3.4.0 Release Candidate",
Expand Down
4 changes: 0 additions & 4 deletions notebooks/2b_modelling_objects_in_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@
#
# In the previous tutorial, we simply modelled the chemistry of a static cloud for 1 Myr. This is unlikely to meet everybody's modelling needs and UCLCHEM is capable of modelling much more complex environments such as hot cores and shocks. In this tutorial, we model both a hot core and a shock to explore how these models work and to demonstrate the workflow that the UCLCHEM team normally follow.

# +
import uclchem
import matplotlib.pyplot as plt
import pandas as pd

# PLease run the notebook on the develop branch only :D jshock still has a bug with timepoints being 1 point too small as of 10-10-2024 (I might have fixed it, Gijs).
# -

# ## The Hot Core
#
# ### Initial Conditions (Phase 1)
Expand Down

0 comments on commit 7b0b279

Please sign in to comment.