Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 179 additions & 0 deletions training/PDEClasses.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 6. Elaboration: the PDEmodel class <a class=\"anchor\" id=\"PDE_model_elaborate\"></a> ★"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets explore the model for PDE problems.\n",
"\n",
"#### Try it yourself (optional):\n",
"\n",
"* View: `model`, `model.pde`, `model.pde.PDE_form`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can, for example, create our own PDE model for simple Poisson equation with zero boundaries. We first create the forward difference operator using the cuqi operator `FirstOrderFiniteDifference`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n_poisson = 1000 #Number of nodes\n",
"L = 1 # Length of the domain\n",
"dx = L/(n_poisson-1) # grid spacing\n",
"diff_operator = FirstOrderFiniteDifference(n_poisson,bc_type='zero').get_matrix().todense()/dx"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We then construct the source term (point source):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"source_term = np.zeros(n_poisson)\n",
"source_term[int(n_poisson/2)] = 1/dx "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We create the PDE form which consists of the differential operator and the right hand side, and is a function of the Bayesian parameter x. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"poisson_form = lambda x: (diff_operator.T@diff_operator, x* source_term)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We create the CUQI PDEModel, in this case a `SteadyStateLinearPDE` model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"CUQI_pde = SteadyStateLinearPDE(poisson_form)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The model `CUQI_pde` has three main methods: \n",
"\n",
"1. assemble, which assembles the differential operator and the RHS given the Bayesian parameter x.\n",
"2. solve, which solves the PDE.\n",
"3. observe, for now observe returns the solution of the PDE, but it is to be generalized to apply observation operators on the PDE solution (e.g. extracting final temperature at specific or random points).\n",
"\n",
"In the following we assemble and solve this Poisson problem."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"CUQI_pde.assemble(5)\n",
"sol, info = CUQI_pde.solve()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And plot the solution:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.plot(np.linspace(dx,L,n_poisson,endpoint=False),sol)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Try it yourself (optional):\n",
"\n",
"* Double the magnitude of the source term by editing the line `CUQI_pde.assemble(5)` above. Look at the solution."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your code here\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"** Create PDEModel, explore geometry set up"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.12 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.9.12"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}