aPriori Documentation
  • 👋Welcome to aPriori
  • Getting started
    • What is aPriori?
    • Installation
    • Quickstart
  • Fundamentals and usage
    • aPriori Fundamentals
      • Data Formatting
      • Cut a 3D scalar
      • Filter a 3D scalar field
      • Initialize a DNS field
      • Data visualization
      • Cut a DNS field
      • Filter a DNS field
    • Machine Learning Tutorials
      • Data-Driven Closure for Turbulence-Chemistry interaction
      • Dynamic Data-Driven Smagorinky Closure for LES
  • API guide
    • Field3D
      • Field3D.build_attributes_list
      • Field3D.check_valid_attribute
      • Field3D.compute_chemical_timescale
      • Field3D.compute_kinetic_energy
      • Field3D.compute_mixing_timescale
      • Field3D.compute_residual_kinetic_energy
      • Field3D.compute_residual_dissipation_rate
      • Field3D.compute_reaction_rates
      • Field3D.compute_reaction_rates_batch
      • Field3D.compute_strain_rate
      • Field3D.compute_tau_r
      • Field3D.compute_velocity_module
      • Field3D.cut
      • Field3D.filter_favre
      • Field3D.filter
      • Field3D.find_path
      • Field3D.plot_x_midplane
      • Field3D.plot_y_midplane
      • Field3D.plot_z_midplane
      • Field3D.print_attributes
      • Field3D.update
    • Scalar3D
      • Scalar3D.is_light_mode
      • Scalar3D.reshape_3d
      • Scalar3D.reshape_column
      • Scalar3D.reshape_line
      • Scalar3D.cut
      • Scalar3D.filter_gauss
      • Scalar3D.plot_x_midplane
      • Scalar3D.plot_y_midplane
      • Scalar3D.plot_z_midplane
    • Mesh3D
  • BIBLIOGRAPHY
Powered by GitBook
On this page
  1. Fundamentals and usage
  2. aPriori Fundamentals

Cut a 3D scalar

PreviousData FormattingNextFilter a 3D scalar field

Last updated 1 year ago

Exercise 01

The goal of this exercise is to explain how the class Scalar3D works, in the two modes available. Once understood this, using this class should simplify operations with large 3d arrays.

Make sure to download the data folder from the to be able to do this tutorial until the end.

""" 
Created on Fri May 24 09:31:52 2024
@author: lorenzo piu 
"""

Import module and define array

First of all, we need to import the module. Make sure you installed the package with the command pip install aPrioriDNS

>>> from aPrioriDNS.DNS import Scalar3D

now let's define a generic array that we will use as a scalar field

>>> import numpy as np 
>>> shape = [30, 20, 15] 
>>> array = np.random.rand(*shape)

Define Scalar3D object

>>> scalar = Scalar3D(shape=shape, value=array) 
>>> print(f'Initial shape:\n{scalar.shape}') 
Initial shape:
(30, 20, 15)

Cut scalar

Cut the field using the mode 'equal'. This is useful when we want to cut the extrema after filtering operations, or in general after operations that have problems in the boundary treatment. Let's say we want to cut two cells from every boundary:

>>> scalar.cut(n_cut=2, mode='equal') 
>>> print(f'Scalar's shape after cutting with 'equal' mode:\n{scalar.shape}')
Scalar's shape after cutting with 'equal' mode:
(26, 16, 11)

Use Scalar3D in light_mode

This time we are going to use the DNS dataset, to use the Scalar3D class in a more useful way. We won't initialize the object with a value that correspond to a numpy array, but just with the path of the file that should be read. This will create a pointer to the file location, and the object will read the file every necessary time. This mode is called 'light_mode' and can be used if we initialize the Scalar3D object without specifying the value, but specifying the path.

In the following lines of code it's explained how to use the Scalar3D object in light mode. If you downloaded the entire folder from GitHub this path definition should work fine.

>>> 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']

With 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:
>>> T_values = T._3D #this will return a 3d array with the values of Temperature
>>> print("Temperature value in the cell 10,10,10")
>>> print(T_values[10,10,10])
Temperature value in the cell 10,10,10
905.779

The object is now defined and we can access its values. Nothing special until now. But let's check why using the Scalar3D object works so well with DNS data. Now we are going to define a numpy array of the same size as the temperature field, and we'll compare how much memory it takes in your system:

>>> T_numpy = np.random.rand(*DNS_shape)
>>> import sys
>>> T_size = sys.getsizeof(T)
>>> T_numpy_size = sys.getsizeof(T_numpy)

>>> print(f"\nSize of the numpy array:         {T_numpy_size} bytes"
>>>       f"\nSize of the Scalar3D object:     {T_size} bytes")
Size of the numpy array:         32000144 bytes
Size of the Scalar3D object:     56 bytes

Showed the advantage of the object, in the following section the goal is to learn how to use the method cut to cut this field. To cut a different amount of cells in every direction x, y and z, we can use the mode 'xyz'. This mode allows us to cut a different number of cells from the 3 directions, but the number of cells cut, for example, in the x direction, will be symmetric in the two directions of the x axis. If we cut 3 cells in the x direction, the function will remove 3 cells at the beginning and 3 at the end.

>>> T_cut = T.cut(n_cut=[30, 0, 10], mode='xyz') 
>>> print(f'Initial shape: {T.shape}') 
>>> print(f'Scalar's shape after cutting with 'xyz' mode:{T_cut.shape}')
Initial shape:                               (200, 200, 100)
Scalar's shape after cutting with 'xyz' mode:(140, 200, 80)
GitHub repo