update phiflow examples
This commit is contained in:
parent
52d5eb202e
commit
d1bdc36622
1
_toc.yml
1
_toc.yml
@ -5,6 +5,7 @@
|
||||
- file: overview.md
|
||||
sections:
|
||||
- file: overview-burgers-forw.ipynb
|
||||
- file: overview-ns-forw.ipynb
|
||||
- file: supervised
|
||||
sections:
|
||||
- file: supervised-airfoils.ipynb
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
"nbformat_minor": 0,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"name": "Differentiable Fluid Simulations with Φ-Flow.ipynb",
|
||||
"name": "Differentiable Physics with Fluid Simulations.ipynb",
|
||||
"provenance": [],
|
||||
"collapsed_sections": []
|
||||
},
|
||||
@ -21,8 +21,9 @@
|
||||
"source": [
|
||||
"# Differentiable Fluid Simulations with Φ<sub>Flow</sub>\n",
|
||||
"\n",
|
||||
"This notebook steps you through setting up fluid simulations and using TensorFlow's differentiation to optimize them.\n",
|
||||
"... now a more complex example with fluid simulations (Navier-Stokes) ...\n",
|
||||
"\n",
|
||||
"Only for colab: \n",
|
||||
"Execute the cell below to install the [Φ<sub>Flow</sub> Python package from GitHub](https://github.com/tum-pbs/PhiFlow)."
|
||||
]
|
||||
},
|
||||
@ -55,11 +56,12 @@
|
||||
"id": "BVV1IKVqDfLl"
|
||||
},
|
||||
"source": [
|
||||
"# Setting up a simulation\n",
|
||||
"## Setting up the simulation\n",
|
||||
"\n",
|
||||
"Φ<sub>Flow</sub> is object-oriented, i.e. you assemble your simulation by constructing a number of objects and adding them to the world.\n",
|
||||
"Like before ...\n",
|
||||
"But now let's set up four fluid simulations that run in parallel, i.e. a mini batch similar to DL training. In phiflow we can directly pass a `batch_size=4` parameter to the `Fluid` object. Each fluid simulation is fully independent. In this case they differ by having circular Inflows at different locations.\n",
|
||||
"\n",
|
||||
"The following code sets up four fluid simulations that run in parallel (`batch_size=4`). Each fluid simulation has a circular Inflow at a different location."
|
||||
"Like before, let's plot the marker density after a few steps of simulation (each call to `step()` now updates all four simulations). Note that the boundaries between the four simulations are not visible in the image, but it shows four completely separate density states. The different inflow positions in conjunction with the solid wall boundaries (zero Dirichlet for velocity, and Neumann for pressure), result in four different end states of the simulation."
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -69,63 +71,13 @@
|
||||
},
|
||||
"source": [
|
||||
"world = World()\n",
|
||||
"fluid = world.add(Fluid(Domain([40, 32], boundaries=CLOSED), buoyancy_factor=0.1, batch_size=4), physics=IncompressibleFlow())\n",
|
||||
"world.add(Inflow(Sphere(center=[[5,4], [5,8], [5,12], [5,16]], radius=3), rate=0.2));"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "ExA0Pi2sFVka"
|
||||
},
|
||||
"source": [
|
||||
"The inflow affects the fluid's marker density. Because the `buoyancy_factor` is positive, the marker creates an upward force.\n",
|
||||
"fluid = world.add(Fluid(Domain([40, 32], boundaries=CLOSED), buoyancy_factor=0.05, batch_size=4), physics=IncompressibleFlow())\n",
|
||||
"centers = [[5,10], [5,12], [5,14], [5,16]]\n",
|
||||
"world.add(Inflow(Sphere(center=centers, radius=3), rate=0.2));\n",
|
||||
"\n",
|
||||
"Let's plot the marker density after one simulation frame."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "WmGZdOwswOva"
|
||||
},
|
||||
"source": [
|
||||
"world.step()\n",
|
||||
"pylab.imshow(np.concatenate(fluid.density.data[...,0], axis=1), origin='lower', cmap='magma')"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "PA-2tGuWGHv2"
|
||||
},
|
||||
"source": [
|
||||
"We can run more steps by repeatedly calling `world.step()`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "0hZk5HX3w4Or"
|
||||
},
|
||||
"source": [
|
||||
"for frame in range(20):\n",
|
||||
" print('Computing frame %d' % frame)\n",
|
||||
" world.step(dt=1.5)"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "Mfl80CjZxZcL"
|
||||
},
|
||||
"source": [
|
||||
" world.step(dt=1.5)\n",
|
||||
"\n",
|
||||
"pylab.imshow(np.concatenate(fluid.density.data[...,0], axis=1), origin='lower', cmap='magma')"
|
||||
],
|
||||
"execution_count": null,
|
||||
@ -137,7 +89,7 @@
|
||||
"id": "rdSTbMoaS0Uz"
|
||||
},
|
||||
"source": [
|
||||
"# Differentiation\n",
|
||||
"## Differentiation\n",
|
||||
"\n",
|
||||
"The simulation we just computed was using purely NumPy (non-differentiable) operations.\n",
|
||||
"To enable differentiability, we need to build a TensorFlow graph that computes this result."
|
||||
@ -176,8 +128,8 @@
|
||||
},
|
||||
"source": [
|
||||
"world = World()\n",
|
||||
"fluid = world.add(Fluid(Domain([40, 32], boundaries=CLOSED), buoyancy_factor=0.1, batch_size=4), physics=IncompressibleFlow())\n",
|
||||
"world.add(Inflow(Sphere(center=[[5,4], [5,8], [5,12], [5,16]], radius=3), rate=0.2));\n",
|
||||
"fluid = world.add(Fluid(Domain([40, 32], boundaries=CLOSED), buoyancy_factor=0.05, batch_size=4), physics=IncompressibleFlow())\n",
|
||||
"world.add(Inflow(Sphere(center=centers, radius=3), rate=0.2));\n",
|
||||
"fluid.velocity = variable(fluid.velocity) # create TensorFlow variable\n",
|
||||
"initial_state = fluid.state # Remember the state at t=0 for later visualization\n",
|
||||
"session.initialize_variables()"
|
||||
@ -299,9 +251,11 @@
|
||||
"source": [
|
||||
"target = session.run(fluid.density).data[0,...]\n",
|
||||
"loss = math.l2_loss(fluid.density.data[1:,...] - target)\n",
|
||||
"optim = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss)\n",
|
||||
"optim = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(loss)\n",
|
||||
"session.initialize_variables()\n",
|
||||
"print('Initial loss: %f' % session.run(loss))"
|
||||
"\n",
|
||||
"print('Initial loss: %f' % session.run(loss))\n",
|
||||
"pylab.imshow(np.concatenate(session.run(fluid.density).data[...,0], axis=1), origin='lower', cmap='magma')"
|
||||
],
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
@ -321,7 +275,7 @@
|
||||
"id": "pvvF6xqmaRLX"
|
||||
},
|
||||
"source": [
|
||||
"for optim_step in range(10):\n",
|
||||
"for optim_step in range(100):\n",
|
||||
" print('Running optimization step %d. %s' % (optim_step, '' if optim_step else 'The first step sets up the adjoint graph.'))\n",
|
||||
" _, loss_value = session.run([optim, loss])\n",
|
||||
" print('Loss: %f' % loss_value)"
|
||||
@ -397,4 +351,4 @@
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
347
overview-ns-forw.ipynb
Normal file
347
overview-ns-forw.ipynb
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user