Commit 9b77d485 authored by Frederik Hennig's avatar Frederik Hennig Committed by Markus Holzer
Browse files

A few minor fixes:

- Now applying pre-simplification on CQE, too
- Added zero and alias insertion
- Bugfix
parent d6bde7f7
......@@ -4,6 +4,7 @@ __pycache__
*.pyc
*.vti
/build
/html_doc
/dist
/*.egg-info
.cache
......@@ -13,5 +14,4 @@ _local_tmp
**/.vscode
/lbmpy_tests/db
doc/bibtex.json
/html_doc
RELEASE-VERSION
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
%% Cell type:code id: tags:
``` python
from lbmpy.session import *
from pystencils.timeloop import TimeLoop
```
%% Cell type:markdown id: tags:
# Tutorial 06: Coupling two LBM simulations for thermal simulations
In this notebook we demonstrate how to run a thermal lattice Boltzmann simulation.
We use a separate set of distribution functions to solve the advection-diffusion equation for the temperature.
The zeroth moment of these additional pdfs corresponds to temperature.
The thermal LB step is coupled to the normal hydrodynamic LB scheme using the Boussinesq approximation. The force on the liquid is proportional to the relative temperature. The hydrodynamic LB method computes the fluid velocity which in turn enters the thermal scheme, completing the two-way coupling.
To set this up in *lbmpy* we create first a `data handling` object and create an array to store the temperature.
%% Cell type:code id: tags:
``` python
domain_size = (100, 50)
gpu = False
dh = ps.create_data_handling(domain_size)
temperature_field = dh.add_array("T", gpu=gpu)
dh.fill('T', val=1.0)
```
%% Cell type:markdown id: tags:
Next, we define how to compute the local force from the temperature field:
%% Cell type:code id: tags:
``` python
gravity = sp.Matrix([0, -1e-2])
force = -gravity * (temperature_field(0) - 1.0)
```
%% Cell type:markdown id: tags:
Now, we can create both LB steps.
The coupling is created by passing the following parameters to the hydrodynamic step,
- `compute_velocity_in_every_step`: usually the velocity is not computed/stored in every time step, only for output and plotting reasons. In the coupled algorithm we have to make sure that after every time step the current velocity is stored in an array, since it enters the thermal LB scheme.
- `force`: we can simply pass our sympy expression for the force here, as long as the `LatticeBoltzmannStep` operates on a data handling that stores the fields that are referenced in the expression. This is why we have to create the data handling first and pass it to both Step objects
and to the thermal step
- `compute_density_in_every_step`: density corresponds to the temperature here, which we need to be computed in every time step, since it enters the force expression for the hydrodynamic scheme
- `equilibrium_order`: for the thermal LB method a first order accurate equilibrium is sufficient. This is slightly faster to compute than the normal equilibrium of order 2
- `velocity_input_array_name`: the velocity entering the thermal equilibrium equation is not computed as first moment of the thermal pdfs. Instead, the hydrodynamic velocity is used here.
%% Cell type:code id: tags:
``` python
optimization = {'target': 'cpu' if gpu else 'cpu', 'openmp': 2}
hydro_step = LatticeBoltzmannStep(data_handling=dh, name='hydro', optimization=optimization,
relaxation_rate=1.8,
compute_velocity_in_every_step=True,
force=force)
thermal_step = LatticeBoltzmannStep(data_handling=dh, name='thermal', optimization=optimization,
relaxation_rate=1.8, density_data_name="T",
compute_density_in_every_step=True,
equilibrium_order=1,
velocity_input_array_name=hydro_step.velocity_data_name)
```
%% Cell type:markdown id: tags:
We add `NoSlip` boundary conditions on all four walls for both schemes with the exception of the left wall of the thermal scheme. Here we set a heated wall, where the temperature is fixed to `1.01`. This kind of Dirichlet boundary condition can be set using a `FixedDensity` LB boundary.
%% Cell type:code id: tags:
``` python
add_box_boundary(hydro_step.boundary_handling)
add_box_boundary(thermal_step.boundary_handling)
thermal_step.boundary_handling.set_boundary(FixedDensity(1.01), slice_from_direction('W', dh.dim))
plt.figure(figsize=(20, 5))
plt.subplot(1, 2, 1)
plt.title("Hydrodynamic")
plt.boundary_handling(hydro_step.boundary_handling)
plt.subplot(1, 2, 2)
plt.title("Thermal")
plt.boundary_handling(thermal_step.boundary_handling)
```
%% Output
%% Cell type:code id: tags:
``` python
def run(time_steps):
hydro_step.pre_run()
thermal_step.pre_run()
for t in range(time_steps):
hydro_step.time_step()
thermal_step.time_step()
hydro_step.post_run()
thermal_step.post_run()
hydro_step.time_steps_run += time_steps
thermal_step.time_steps_run += time_steps
```
%% Cell type:code id: tags:
``` python
run(5000)
assert dh.max('T') < 2
plt.figure(figsize=(16, 5))
plt.subplot(1, 2, 1)
plt.scalar_field(thermal_step.density[:, :])
plt.subplot(1, 2, 2)
plt.vector_field(hydro_step.velocity[:, :]);
```
%% Output
%% Cell type:code id: tags:
``` python
assert np.isfinite(hydro_step.velocity[:, :].max())
assert np.isfinite(thermal_step.density[:, :].max())
```