Filter a 3D scalar field
Exercise 02
In this section you'll learn how to filter a 3D field using the Scalar3D class.
Make sure to download the data folder from the GitHub repo to be able to do this tutorial until the end
"""
Created on Fri May 24 12:00:20 2024
@author: lorenzo piu
"""
Import and define array
First of all, it's necessary to import the modules that we will use in this tutorial
>>> import aPrioriDNS.DNS as DNS
>>> from aPrioriDNS.DNS import Scalar3D
Again we are going to use the class Scalar3D object assigning a value, which is not the main purpose of the class, but it can be useful to have this option.
Let's define a 3d numpy vector:
>>> shape = [4, 2, 4]
>>> array = np.ones(shape)
>>> array[2:3, :, 2:3] = 2
>>> print(array)
[[[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 2. 1.]
[1. 1. 2. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]]
Filter 3D array
Filter the field with a Gaussian filter:
>>> filter_size = 2
>>> filtered_field = DNS.filter_3D(array, filter_size, favre=False, filter_type='Gauss')
>>> print(filtered_field)
[[[1.00001167 1.00052524 1.00235979 1.00105048]
[1.00001167 1.00052524 1.00235979 1.00105048]]
[[1.00052524 1.02364021 1.10621069 1.04728042]
[1.00052524 1.02364021 1.10621069 1.04728042]]
[[1.00235979 1.10621069 1.4771832 1.21242139]
[1.00235979 1.10621069 1.4771832 1.21242139]]
[[1.00105048 1.04728042 1.21242139 1.09456084]
[1.00105048 1.04728042 1.21242139 1.09456084]]]
At this point let's see how we can filter a scalar inside our DNS field. The procedure to load the files' information is the same as in the previous tutorial
Define Temperature field as a Scalar3D object
>>> import os
>>> # change this with your path to the data folder
>>> directory = os.path.join('..','data','Lifted_H2_subdomain')
>>> T_path = os.path.join(directory,'data', 'T_K_id000.dat')
>>> print(f"\nChecking the path '{T_path}' is correct...")
Checking the path '../data/Lifted_H2_subdomain/data/T_K_id000.dat' is correct...
>>> if not os.path.exists(T_path):
... raise ValueError("The path '{T_path}' does not exist in your system. Check to have the correct path to your data folder in the code")
>>> # Blastnet's data contain information about the shape of the field in the info.json file
>>> import json
>>> with open(os.path.join(directory,'info.json'), 'r') as file:
... info = json.load(file)
>>> DNS_shape = info['global']['Nxyz']
>>> # Now we have the shape and the path of the file, we can define the Scalar3D object:
>>> T = Scalar3D(shape=DNS_shape, path=T_path)
>>> # Try to access the value of the temperature field:
>>> print("Temperature values in the cells 5:8, 5:8, 5:8")
>>> print(T._3D[5:8, 5:8, 5:8])
Temperature values in the cells 5:8, 5:8, 5:8
[[[948.34 954.703 958.952]
[934.218 939.709 943.358]
[921.206 925.98 929.27 ]]
[[953.27 958.763 962.017]
[938.411 942.927 945.55 ]
[924.591 928.335 930.622]]
[[958.015 962.569 964.736]
[942.356 945.86 947.389]
[927.675 930.377 931.61 ]]]
Filter the scalar with a Gaussian filter
>>> filter_size = 20
>>> T_filt = DNS.filter_3D(T._3D, filter_size, favre=False, filter_type='Gauss')
>>> print("Filtered temperature values in the cells 5:8, 5:8, 5:8")
>>> print(T_filt[5:8, 5:8, 5:8])
Filtered temperature values in the cells 5:8, 5:8, 5:8
[[[944.01654 947.4043 951.1053 ]
[937.93115 941.49243 945.403 ]
[931.5709 935.3397 939.49695]]
[[945.629 948.70966 952.0795 ]
[939.3335 942.58923 946.16925]
[932.73956 936.2061 940.03516]]
[[947.3182 950.0389 953.0218 ]
[940.786 943.6836 946.8774 ]
[933.9282 937.0404 940.4859 ]]]
Filter the scalar with a Box filter
>>> T_filt_box = DNS.filter_3D(T._3D, filter_size, favre=False, filter_type='Box')
>>> print("Filtered temperature values in the cells 5:8, 5:8, 5:8")
>>> print(T_filt_box[5:8, 5:8, 5:8])
Filtered temperature values in the cells 5:8, 5:8, 5:8
[[[943.32434 946.17487 949.7231 ]
[939.5717 942.7536 946.63837]
[935.1457 938.67377 942.90924]]
[[944.25745 946.8551 950.1111 ]
[940.2976 943.2261 946.8169 ]
[935.63934 938.9155 942.85785]]
[[945.3546 947.65906 950.5727 ]
[941.159 943.79346 947.03986]
[936.23615 939.21985 942.8185 ]]]
I know it's not optimal to visualize the effect of the filtering operation in this way, so if you're curious to see how this affects the temperature field, I suggest reading the next two exercises.
Last updated