phiflow 2 updates of overview sims
This commit is contained in:
parent
1f1c8703b7
commit
3cbaa5ef5d
@ -6,9 +6,9 @@
|
||||
"source": [
|
||||
"# Simple Forward Simulation of Burgers Equation with phiflow\n",
|
||||
"\n",
|
||||
"This chapter will give an introduction for how to run _forward_, i.e., regular simulations starting with a given initial state and approximating a later state numerically, with Φ<sub>Flow</sub>. The phiflow framework provides a set of differentiable building blocks that directly interface with deep learning frameworks, and before going for deeper and more complicated integrations, this notebook (and the next one), will show how regular simulations can be done with phiflow. Later on, we can use very similar code to couple them with neural networks.\n",
|
||||
"This chapter will give an introduction for how to run _forward_, i.e., regular simulations starting with a given initial state and approximating a later state numerically, with Φ<sub>Flow</sub>. The Φ<sub>Flow</sub> framework provides a set of differentiable building blocks that directly interface with deep learning frameworks, and before going for deeper and more complicated integrations, this notebook (and the next one), will show how regular simulations can be done with Φ<sub>Flow</sub>. Later on, we can use very similar code to couple them with neural networks.\n",
|
||||
"\n",
|
||||
"The main repository for phiflow is https://github.com/tum-pbs/PhiFlow, and additional API documenation and examples can be found at https://tum-pbs.github.io/PhiFlow/.\n",
|
||||
"The main repository for Φ<sub>Flow</sub> (in the following \"phiflow\") is https://github.com/tum-pbs/PhiFlow, and additional API documenation and examples can be found at https://tum-pbs.github.io/PhiFlow/.\n",
|
||||
"\n",
|
||||
"For this jupyter notebook (and all following ones), you can find a _\"[run in colab]\"_ link at the end of the first paragraph. This link will load the latest version from the PBDL github repo in a colab notebook that you can execute on the spot: \n",
|
||||
"[[run in colab]](https://colab.research.google.com/github/tum-pbs/pbdl-book/blob/main/overview-burgers-forw.ipynb)\n",
|
||||
@ -31,14 +31,14 @@
|
||||
"source": [
|
||||
"## Importing and loading phiflow\n",
|
||||
"\n",
|
||||
"Let's get some preliminaries out of the way: first we'll import the Φ<sub>Flow</sub> library, more specifically the `numpy` operators for fluid flow simulations: `phi.flow` (differentiable versions for a DL framework _X_ are loaded via `phi.X.flow` instead).\n",
|
||||
"Let's get some preliminaries out of the way: first we'll import the phiflow library, more specifically the `numpy` operators for fluid flow simulations: `phi.flow` (differentiable versions for a DL framework _X_ are loaded via `phi.X.flow` instead).\n",
|
||||
"\n",
|
||||
"**Note:** Below, the first command with a \"!\" prefix will install the [Φ<sub>Flow</sub> Python package from GitHub](https://github.com/tum-pbs/PhiFlow) via `pip` in your python environment once you uncomment it. We've assumed that phiflow isn't installed, but if you have already done so, just comment out the first line (the same will hold for all following notebooks)."
|
||||
"**Note:** Below, the first command with a \"!\" prefix will install the [phiflow python package from GitHub](https://github.com/tum-pbs/PhiFlow) via `pip` in your python environment once you uncomment it. We've assumed that phiflow isn't installed, but if you have already done so, just comment out the first line (the same will hold for all following notebooks)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@ -65,12 +65,16 @@
|
||||
"Next we can define and initialize the necessary constants (denoted by upper-case names):\n",
|
||||
"our simulation domain will have `N=128` cells as discretization points for the 1D velocity $u$ in a periodic domain $\\Omega$ for the interval $[-1,1]$. We'll use 32 time `STEPS` for a time interval of 1, giving us `DT=1/32`. Additionally, we'll use a viscosity `NU` of $\\nu=0.01/\\pi$.\n",
|
||||
"\n",
|
||||
"We'll also define an initial state given by $-\\text{sin}(\\pi x)$ in the numpy array `INITIAL`, which we'll use to initialize the velocity $u$ in the simulation in the next cell. This initialization will produce a nice shock in the center of our domain.\n"
|
||||
"We'll also define an initial state given by $-\\text{sin}(\\pi x)$ in the numpy array `INITIAL_NUMPY`, which we'll use to initialize the velocity $u$ in the simulation in the next cell. This initialization will produce a nice shock in the center of our domain. \n",
|
||||
"\n",
|
||||
"Phiflow is object-oriented and centered around field data in the form of grids (internally represented by a tensor object). I.e. you assemble your simulation by constructing a number of grids, and updating them over the course of time steps. \n",
|
||||
"\n",
|
||||
"Phiflow internally works with tensors that have named dimensions. This will be especially handy later on for 2D simulations with additional batch and channel dimensions, but for now we'll simply convert the 1D array into a phiflow tensor that has a single spatial dimension `'x'`.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -80,40 +84,40 @@
|
||||
"NU = 0.01/np.pi\n",
|
||||
"\n",
|
||||
"# initialization of velocities\n",
|
||||
"INITIAL = np.asarray( [-np.sin(np.pi * x) * 1. for x in np.linspace(-1,1,N)] ) # 1D array"
|
||||
"INITIAL_NUMPY = np.asarray( [-np.sin(np.pi * x) * 1. for x in np.linspace(-1,1,N)] ) # 1D numpy array\n",
|
||||
"\n",
|
||||
"INITIAL = math.tensor(INITIAL_NUMPY, spatial('x') ) # convert to phiflow tensor\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Φ<sub>Flow</sub> is object-oriented and centered around field data in the form of grids (internally represented by a tensor object). I.e. you assemble your simulation by constructing a number of grids, and updating them over the course of time steps.\n",
|
||||
"\n",
|
||||
"Next, we declare the `domain` object to contain $\\Omega$, as outlined above periodically for $[-1,1]$. We also \n",
|
||||
"initialize a 1D `velocity` grid from the `INITIAL` numpy array.\n",
|
||||
"Next, we initialize a 1D `velocity` grid from the `INITIAL` numpy array that was converted into a tensor.\n",
|
||||
"The extent of our domain $\\Omega$ is specifiied via the `bounds` parameter $[-1,1]$, and the grid uses periodic boundary conditions (`extrapolation.PERIODIC`). These two properties are the main difference between a tensor and a grid: the latter has boundary conditions and a physical extent.\n",
|
||||
"\n",
|
||||
"Just to illustrate, we'll also print some info about the velocity object: it's a `phiflow.math` tensor with a size of 128. Note that the actual grid content is contained in the `values` of the grid. Below we're printing five entries by using the `numpy()` function to convert the content of the phiflow tensor into a numpy array."
|
||||
"Just to illustrate, we'll also print some info about the velocity object: it's a `phi.math` tensor with a size of 128. Note that the actual grid content is contained in the `values` of the grid. Below we're printing five entries by using the `numpy()` function to convert the content of the phiflow tensor into a numpy array. For tensors with more dimensions, we'd need to specify the order here, e.g., `'y,x,vector'` for a 2D velocity field. (If we'd call `numpy('x,vector')` below, this would convert the 1D array into one with an additional dimension for each 1D velocity component.)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Velocity tensor shape: (x=128)\n",
|
||||
"Velocity tensor shape: (xˢ=128)\n",
|
||||
"Velocity tensor type: <class 'phi.math._tensors.CollapsedTensor'>\n",
|
||||
"Velocity tensor entries 10 to 14: [0.47480196 0.51774486 0.55942075 0.59972764 0.6385669 ]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"DOMAIN = Domain(x=N, boundaries=PERIODIC, bounds=Box[-1:1])\n",
|
||||
"velocity = DOMAIN.scalar_grid(INITIAL)\n",
|
||||
"#velocity = DOMAIN.grid(Noise(vector=1)) # random init instead of sine curve\n",
|
||||
"velocity = CenteredGrid(INITIAL, extrapolation.PERIODIC, x=N, bounds=Box[-1:1])\n",
|
||||
"#velocity = CenteredGrid(Noise(), extrapolation.PERIODIC, x=N, bounds=Box[-1:1]) # random init\n",
|
||||
"\n",
|
||||
"print(\"Velocity tensor shape: \" + format( velocity.shape )) # == velocity.values.shape\n",
|
||||
"print(\"Velocity tensor type: \" + format( type(velocity.values) ))\n",
|
||||
@ -131,7 +135,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@ -158,7 +162,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Here we're actually collecting all time steps in the list `vels`. This is not necessary in general (and could consume lots of memory for long-running sims), but useful here to plot the evolution of the velocity states later on.\n",
|
||||
"Here we're actually collecting all time steps in the list `velocities`. This is not necessary in general (and could consume lots of memory for long-running sims), but useful here to plot the evolution of the velocity states later on.\n",
|
||||
"\n",
|
||||
"The print statements print a few of the velocity entries, and already shows that something is happening in our simulation, but it's difficult to get an intuition for the behavior of the PDE just from these numbers. Hence, let's visualize the states over time to show what is happening.\n",
|
||||
"\n",
|
||||
@ -169,16 +173,16 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<matplotlib.legend.Legend at 0x7fe4d809d190>"
|
||||
"<matplotlib.legend.Legend at 0x7f9ca2d4ef70>"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
},
|
||||
@ -222,7 +226,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@ -277,7 +281,8 @@
|
||||
"\n",
|
||||
"Some things to try based on this simulation setup:\n",
|
||||
"\n",
|
||||
"- Feel free to experiment - the setup above is very simple, you can change the simulation parameters, or the initialization. E.g., you can use a noise field via `Noise(vector=1)` to get more chaotic results.\n",
|
||||
"- Feel free to experiment - the setup above is very simple, you can change the simulation parameters, or the initialization. E.g., you can use a noise field via `Noise()` to get more chaotic results (cf. the comment in the `velocity` cell above).\n",
|
||||
"\n",
|
||||
"- A bit more complicated: extend the simulation to 2D (or higher). This will require changes throughout, but all operators above support higher dimensions. Before trying this, you probably will want to check out the next example, which covers a 2D Navier-Stokes case."
|
||||
]
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user