bounds (Photo Robert Katzki on Unsplash) Setting the boundaries is setting the game, even for large eddy simulations.

The solutBound file manages the boundaries of the domain for AVBP. PyAVBP provides many ways to create or edit a solutbound.

Example of solutbound

Create any Solutbound from scratch

There is a crude helper function in pyavbp called gensolutbound(). It generates AVBP solutBound.h5 files without any check using a blueprint. Better start with an example:

from pyavbp.io.mesh_utils import load_mesh_bnd
from pyavbp.io.gensolutbound import gensolutbound 
bnd_data  = load_mesh_bnd('./trappedvtx_perio.mesh.h5')
bnd_targets =  [
    { 
        "fields": {
            "RhoUn": {
                "type": "constant",
                "value": 2.,
            },
            "Urms": {
                "type": "constant",
                "value": 100.,
            },
            "mixture": {
                "type": "constant",
                "value": [ 0.75, 0.25],
            },
        },
        "name" : "Inlet",  
    },
    { 
        "name" : "Outlet",
        "fields": {
            "Pressure": {
                "type": "constant",
                "value": 101325.,
            },
        },
    },
]
mixture_species = ["O2", "N2"]
gensolutbound(bnd_data, bnd_targets, mixture_species, "raw_solutbound.h5")

This small script creates a solutBound for the mesh trappedvtx_perio. Showing the data inside, we get:

>h5cross stats raw_solutbound.h5 
+----------------------------+----------+----------+----------+--------+----------+
| Dataset                    |   min    |   mean   |   max    | st dev |  median  |
+----------------------------+----------+----------+----------+--------+----------+
| /Patch_001-Inlet/N2        |   0.25   |   0.25   |   0.25   |  0.0   |   0.25   |
| /Patch_001-Inlet/O2        |   0.75   |   0.75   |   0.75   |  0.0   |   0.75   |
| /Patch_001-Inlet/RhoUn     |   2.0    |   2.0    |   2.0    |  0.0   |   2.0    |
| /Patch_001-Inlet/Urms      |  100.0   |  100.0   |  100.0   |  0.0   |  100.0   |
| /Patch_002-Outlet/Pressure | 101325.0 | 101325.0 | 101325.0 |  0.0   | 101325.0 |
+----------------------------+----------+----------+----------+--------+----------+
Custom fields

However you can generate any kind of custom field, as long a you provide a numpy array of the correct shape. In this case, the inlet patch is defined by 417 vertices. Here we replace replace the value 2. of RhoUn by 2.*np.random.rand(417) -1.:

import numpy as np
(...)
bnd_targets =  [
    { 
        "fields": {
            "RhoUn": {
                "type": "constant",
                "value": 2.*np.random.rand(417) -1.,
            },
(...)

We get the same solutBound with a random field:

+----------------------------+-------------+-------------+------------+------------+-------------+
| Dataset                    |     min     |     mean    |    max     |   st dev   |    median   |
+----------------------------+-------------+-------------+------------+------------+-------------+
| /Patch_001-Inlet/N2        |     0.25    |     0.25    |    0.25    |    0.0     |     0.25    |
| /Patch_001-Inlet/O2        |     0.75    |     0.75    |    0.75    |    0.0     |     0.75    |
| /Patch_001-Inlet/RhoUn     | -0.99816073 | -0.01738461 | 0.98975535 | 0.57305945 | -0.02638937 |
| /Patch_001-Inlet/Urms      |    100.0    |    100.0    |   100.0    |    0.0     |    100.0    |
| /Patch_002-Outlet/Pressure |   101325.0  |   101325.0  |  101325.0  |    0.0     |   101325.0  |
+----------------------------+-------------+-------------+------------+------------+-------------+

So fields can be either a single scalar or a numpy array of the correct shape.

What if I mess up ?

If you mess up the shape, you will get the following error:

Traceback (most recent call last):
  File "/Users/dauptain/PYAVBP/GENPROFILE/trial_genprofile.py", line 94, in <module>
    gensolutbound(bnd_data, bnd_targets, mixture_species, "raw_solutbound.h5")
  File "/Users/dauptain/GITLAB/pyavbp/src/pyavbp/io/gensolutbound.py", line 131, in gensolutbound
    boundary_target[bnd_p["name"]] = _create_boundary_data(
  File "/Users/dauptain/GITLAB/pyavbp/src/pyavbp/io/gensolutbound.py", line 91, in _create_boundary_data
    out[field] = scalar_array * bnd_fields[field]["value"]
ValueError: operands could not be broadcast together with shapes (417,) (418,) 

If you provide a pathname not present in the mesh, you will get the following error:

Traceback (most recent call last):
  File "/Users/dauptain/PYAVBP/GENPROFILE/trial_genprofile.py", line 94, in <module>
    gensolutbound(bnd_data, bnd_targets, mixture_species, "raw_solutbound.h5")
  File "/Users/dauptain/GITLAB/pyavbp/src/pyavbp/io/gensolutbound.py", line 133, in gensolutbound
    patch_data[bnd_p["name"]],
KeyError: 'InletXXX'
Takeaway

In a nutshell this gensolutbound() is only writing down to the disc what is in the blueprint bnd_target. All complex treatments (makeinject, genprofile, patchmapper) must be done before, in a non-AVBP, pure Numpy context.

Edit an existing solutBound

Add a hot spot

Here we want to add a hot spot at the inlet of the domain. We will directly edit the h5 file. There are plenty of Python packages to edit HDF files, some of them are described in this post. We choose to use h5py and hdfdict.

import h5py
import hdfdict

We use with instructions to make sure the files are closed at the end of the script. h5py is used to read the original file while hdfdict is used to transform the file into a dict and to render it easier to edit. We also use PyAVBP’s io.write_xmf function.

with h5py.File('./oms_combu.solutbound.h5','r') as fin:
    solutbound_dict=hdfdict.load(fin)
    field=solutbound_dict['Patch_001-Inlet']['Temperature']
    solutbound_dict['Patch_001-Inlet']['Temperature'][100]=1000
    solutbound_dict['Patch_001-Inlet']['Temperature'][250]=1000
    with h5py.File('new_solutbound.h5','w') as fout:
        hdfdict.dump(solutbound_dict,fout)
io.write_xmf(meshfile, 'new_solutbound.h5', 'new_solutbound.xmf')

Two hot spots

Apply a profile based on coordinates

In this part, we will change a patch property according to the coordinates. For example, we want to apply a constant sinus-shaped temperature at the inlet.

# Some imports

We start importing the same packages as before plus a package to get mesh information and numpy:

import h5py
import hdfdict
import pyavbp.io as io
import numpy as np
# Mesh manipulations

We use mesh_utils to get the coordinates of the points at the inlet patch:

solutbound_file = "./oms_combu.solutbound.h5"
meshfile = "../../COMMON/oms_combu.mesh.h5"
mesh = io.get_mesh_bulk(meshfile)
coord = io.load_mesh_bnd(meshfile)['Inlet']['xyz']
# Dict instantiation

Then, we use h5py to read the current solutbound and put it in a dict with hdfdict.

with h5py.File(solutbound_file,'r') as fin:
    datadict = hdfdict.load(fin)

We create a new array with the temperatures we want:

    new_temp = 300 + 20 * np.sin(coord[:,2]*50*2*np.pi)
    datadict['Patch_001-Inlet']['Temperature'] = new_temp
# Dumping the solution

We conclude by writing the new values in the dict and writing a new solutbound file:

    with h5py.File('new_solutbound.h5','w') as fout:
        hdfdict.dump(datadict,fout)
    io.write_xmf(meshfile, 'new_solutbound.h5', 'new_solutbound.xmf')

New solutbound

Apply a profile created with patchmapper

Disclaimer: This option is available only for patches with x constant profiles

Patchmapper is a tool that applies a profile defined as a numpy.array to an axisymmetric patch. You have to give it a dict with all the profiles you want to change and a reference array with key name ['rR'] along the height to make the interpolation. Make sure the lengths of the arrays in the dict are all the same, but not necessarily the same as the number of nodes in the patch.

# Some importations

We import the same packages as before for handling the HDF files, plus the package containing patchmapper and a package for mesh manipulation:

import h5py
import hdfdict
from pyavbp.tools import patchmapper as pm
from pyavbp.io import mesh_utils as mu
import numpy as np
# Mesh and solutbound handling

Now, we load the mesh and the solutbound files. We store the coordinates of the nodes for later:

solutbound_file = "./oms_combu.solutbound.h5"
meshfile = "../../COMMON/oms_combu.mesh.h5"
mesh = io.get_mesh_bulk(meshfile)
coord = io.load_mesh_bnd(meshfile)['Inlet']['xyz']

The mesh

# Dict creation

We create the dict that we will use as an input of patchmapper. First, we calculate the radial coordinates of all the points of the mesh in order to find the extrema and to create the ‘rR’ entry of the dict for the interpolation made by patchmapper.

with h5py.File(solutbound_file,'r') as fin:
    datadict = hdfdict.load(fin)
    radial_points=2000
    radial_coord = np.hypot(coord[:,1],coord[:,2])
    profile_dict=dict()
    profile_dict['rR'] = np.linspace(np.min(radial_coord),
                                     np.max(radial_coord),
                                     radial_points)

Now, we can create all the entries we want in the dict. They have to be defined as vectors that will be interpolated on the mesh given the ‘rR’ vector. In this example, we create a temperature vector with a 1000K band and 600K everywhere else. The length of the hot band is one third of the total height located in the middle of the inlet patch:

    temp = 600 * np.ones(radial_points)
    temp[int(len(temp) / 3):int(len(temp) * 2 / 3)] = 1000
    profile_dict['Temperature'] = temp
# Patchmapper and dumping the solution

We call the patchmapper function with the right inputs. We change the initial dict with the new values. Then, we dump the new solutBound and the xmf file:

    new_fields_inlet = pm.apply_patchmapper_1d(profile_dict, coord)
    datadict['Patch_001-Inlet']['Temperature'] = new_fields_inlet['Temperature']
    with h5py.File('new_solutbound.h5','w') as fout:
        hdfdict.dump(datadict,fout)
    io.write_xmf(meshfile, 'new_solutbound.h5', 'new_solutbound.xmf')

Patchmapper defined profile

# The full code
import h5py
import hdfdict
from pyavbp.tools import patchmapper as pm
import pyavbp.io as io
import numpy as np

#Mesh and solutboud handling
solutbound_file = "./oms_combu.solutbound.h5"
meshfile = "../../COMMON/oms_combu.mesh.h5"
mesh = io.get_mesh_bulk(meshfile)
coord = io.load_mesh_bnd(meshfile)['Inlet']['xyz']

#Reading the initial solutbound and initializing the output dict
with h5py.File(solutbound_file,'r') as fin:
    datadict = hdfdict.load(fin)
    radial_points=2000
    radial_coord = np.hypot(coord[:,1],coord[:,2])
    profile_dict=dict()
    profile_dict['rR'] = np.linspace(np.min(radial_coord),
                                     np.max(radial_coord),
                                     radial_points)

    #Creating temperature vector and applying patchmapper
    temp = 600 * np.ones(radial_points)
    temp[int(len(temp) / 3):int(len(temp) * 2 / 3)] = 1000
    profile_dict['Temperature'] = temp
    new_fields_inlet = pm.apply_patchmapper_1d(profile_dict, coord)
    datadict['Patch_001-Inlet']['Temperature'] = new_fields_inlet['Temperature']

    #Writing the new values in a new file
    with h5py.File('new_solutbound.h5','w') as fout:
        hdfdict.dump(datadict,fout)
    io.write_xmf(meshfile, 'new_solutbound.h5', 'new_solutbound.xmf')   

Like this post? Share on: TwitterFacebookEmail


Matthieu Rossi is an engineer focused on making COOP tools available to industry.
Antoine Dauptain is a research scientist focused on computer science and engineering topics for HPC.

Keep Reading


Published

Category

Tutorials

Tags

Stay in Touch