Skip to content

Commit

Permalink
Make pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
mdpiper committed Aug 9, 2024
1 parent 6c0ea66 commit 8bf7cdb
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions lessons/python/yet_another_oop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -540,17 +540,17 @@
" # Note: I didn't realize this when I originally wrote the class, but we won't actually need dz\n",
" # Want to know why? Check out the documentation for np.gradient -> https://numpy.org/doc/stable/reference/generated/numpy.gradient.html\n",
" # (What does the second argument to np.gradient do?)\n",
" \n",
"\n",
" # We'll need attributes for rho, c, and k\n",
" # You could also store info about boundary conditions at this point, if you want\n",
" self.rho = 917\n",
" self.c = 2093\n",
" self.k = 2.1\n",
"\n",
" # Let's store boundary conditions too\n",
" \n",
"\n",
" # Maybe we should go ahead and calculate diffusivity right away?\n",
" \n",
"\n",
" # Let's keep track of the elapsed time\n",
"\n",
" def calc_diffusivity(self) -> float:\n",
Expand All @@ -559,7 +559,7 @@
"\n",
" def calc_heat_flux(self) -> np.ndarray:\n",
" \"\"\"The heat flux is -kappa * dT / dz.\"\"\"\n",
" \n",
"\n",
" # How should we calculate the difference in temperature with depth? (hint: see dz, above)\n",
"\n",
" # Are dT and dz the same size? Are they the same size as z?\n",
Expand Down Expand Up @@ -634,16 +634,16 @@
"\n",
"dt = 60 * 60 * 24 * 20\n",
"\n",
"for i in range(30000): \n",
"for i in range(30000):\n",
" model.run_one_step(dt)\n",
"\n",
" if i % 1000 == 0:\n",
" print(f'{model.time_elapsed / 31556926} years.')\n",
" print(f\"{model.time_elapsed / 31556926} years.\")\n",
"\n",
"plt.plot(model.temperature, model.z)\n",
"plt.xlabel('Temperature ($^\\circ$ C)')\n",
"plt.ylabel('Depth (m)')\n",
"plt.gca().invert_yaxis() # This forces matplotlib to invert the y-axis\n",
"plt.xlabel(r\"Temperature ($^\\circ$ C)\")\n",
"plt.ylabel(\"Depth (m)\")\n",
"plt.gca().invert_yaxis() # This forces matplotlib to invert the y-axis\n",
"plt.show()"
]
},
Expand All @@ -662,8 +662,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Initialize your model with a new bottom boundary condition\n",
"\n"
"# Initialize your model with a new bottom boundary condition"
]
},
{
Expand All @@ -675,16 +674,16 @@
"source": [
"dt = 60 * 60 * 24 * 20\n",
"\n",
"for i in range(30000): \n",
"for i in range(30000):\n",
" model.run_one_step(dt)\n",
"\n",
" if i % 1000 == 0:\n",
" print(f'{model.time_elapsed / 31556926} years.')\n",
" print(f\"{model.time_elapsed / 31556926} years.\")\n",
"\n",
"plt.plot(model.temperature, model.z)\n",
"plt.xlabel('Temperature ($^\\circ$ C)')\n",
"plt.ylabel('Depth (m)')\n",
"plt.gca().invert_yaxis() # This forces matplotlib to invert the y-axis\n",
"plt.xlabel(r\"Temperature ($^\\circ$ C)\")\n",
"plt.ylabel(\"Depth (m)\")\n",
"plt.gca().invert_yaxis() # This forces matplotlib to invert the y-axis\n",
"plt.show()"
]
},
Expand Down Expand Up @@ -717,8 +716,8 @@
"source": [
"class SimpleGlacier:\n",
" \"\"\"Models the temperature profile with a glacier.\n",
" \n",
" This model is based off of: \n",
"\n",
" This model is based off of:\n",
" The Physics of Glaciers (Cuffey and Paterson, 2010).\n",
" Lecture notes from Andy Aschwanden (McCarthy school, summer 2012).\n",
"\n",
Expand All @@ -736,7 +735,7 @@
" self.dz = np.gradient(self.z)\n",
" # Note: I didn't realize this when I originally wrote the class, but we won't need dz\n",
" # Want to know why? Check out the documentation for np.gradient -> https://numpy.org/doc/stable/reference/generated/numpy.gradient.html\n",
" \n",
"\n",
" # We'll need attributes for rho, c, and k\n",
" # You could also store info about boundary conditions at this point, if you want\n",
" self.rho = 917\n",
Expand All @@ -746,10 +745,10 @@
" # Let's store boundary conditions too\n",
" self.surface_temperature = -10.0\n",
" self.basal_heat_flux = 0.0\n",
" \n",
"\n",
" # Maybe we should go ahead and calculate diffusivity right away?\n",
" self.kappa = self.calc_diffusivity()\n",
" \n",
"\n",
" # Let's keep track of the elapsed time\n",
" self.time_elapsed = 0.0\n",
"\n",
Expand All @@ -759,7 +758,7 @@
"\n",
" def calc_heat_flux(self) -> np.ndarray:\n",
" \"\"\"The heat flux is -kappa * dT / dz.\"\"\"\n",
" \n",
"\n",
" # How should we calculate the difference in temperature with depth? (hint: see dz, above)\n",
" temperature_gradient = np.gradient(self.temperature, self.z)\n",
"\n",
Expand All @@ -770,15 +769,15 @@
" temperature_gradient[-1] = self.basal_heat_flux\n",
"\n",
" return -self.kappa * temperature_gradient\n",
" \n",
"\n",
" def calc_divergence(self) -> np.ndarray:\n",
" \"\"\"In 1D, divergence is just the derivative. yay!\"\"\"\n",
" heat_flux = self.calc_heat_flux()\n",
" \n",
"\n",
" flux_divergence = np.gradient(heat_flux, self.z)\n",
" \n",
"\n",
" return flux_divergence\n",
" \n",
"\n",
" def run_one_step(self, dt: float):\n",
" \"\"\"Advance the model by one step of size dt.\"\"\"\n",
" flux_divergence = self.calc_divergence()\n",
Expand Down

0 comments on commit 8bf7cdb

Please sign in to comment.