# Field3D.compute\_tau\_r

[![badge](https://img.shields.io/badge/Source%20Code-06D40C?style=plastic)](https://github.com/LorenzoPiu/aPriori/blob/main/src/aPriori/DNS.py#L1193-L1394)

### **Field3D.**<mark style="color:red;">**compute\_tau\_r**</mark>(self, mode='Smag', save\_tensor\_components=True):

***

### Description

Computes the anisotropic part of the residual stress tensor, denoted as $$\tau\_r$$, for a given field in computational fluid dynamics simulations. The function can operate in two modes: 'Smag' and 'DNS'.

### Parameters

* `ValueError`: If the field is not a filtered field (i.e., `self.filter_size == 1`).
* `AttributeError`: If required attributes (`Cs`, `S_LES`, `DNS_folder_path`) are not defined.

### Raises

* `mode` (str, optional): Mode of operation, either 'Smag' for the Smagorinsky model or 'DNS' for Direct Numerical Simulation data. Default is 'Smag'.

### Returns

None

### Workflow

1. **Initial Setup and Validation**:
   * The function starts by updating the field and checking if the field is filtered.
   * If (self.filter\_size == 1), it raises a (ValueError) because residual quantities computation only makes sense for filtered fields.
2. **Mode: 'Smag' (Smagorinsky Model)**:
   * **Turbulent Viscosity**:
     * Checks if the Smagorinsky constant ((Cs)) is defined. If not, it initializes (Cs) to 0.1.
     * Computes the turbulent viscosity ((\mu\_t)) using: $$\mu\_t = (Cs \cdot \Delta \cdot l)^2 \cdot S\_{LES}$$ where $$\Delta$$ is the filter size, $$l$$ is the grid size, and $$S\_{LES}$$ is the strain rate at LES scale.
   * **Anisotropic Residual Stress Tensor**:
     * Initializes $$\tau\_r$$ as a zero matrix.
     * For each component ((i, j)) of the tensor:
       * Computes  $$\tau^r\_{ij} = -2\mu\_t S\_{ij}^{LES}$$.
       * Adjusts for compressibility by subtracting the isotropic part $$(S\_{iso}) when (i = j)$$.
       * Computes the squared components and accumulates them.
       * Saves the computed $$\tau^r\_{ij}$$ to a file.
3. **Mode: 'DNS' (Direct Numerical Simulation)**:
   * **DNS Data Setup**:
     * Checks if the path to DNS data is defined.
     * Initializes a (DNS\_field) object to read DNS data.
     * Determines the type of filter used (box or Gaussian).
   * **Residual Kinetic Energy**:
     * Computes residual kinetic energy ( K\_r^{DNS} ) as: $$K\_r^{DNS} = 0.5 \left( U\_x^2 + U\_y^2 + U\_z^2 \right)\_{DNS} - 0.5 \left( U\_x^2 + U\_y^2 + U\_z^2 \right)$$
     * Saves ( K\_r^{DNS} ) to a file.
   * **Anisotropic Residual Stress Tensor**:
     * Initializes (\tau\_r) as a zero matrix.
     * For each component $$(i, j)$$ of the tensor:
       * Computes the filtered product $$\widetilde{(U\_i U\_j)}\_{DNS})$$
       * Calculates $$\tau^r\_{ij}$$ as: $$\tau^r\_{ij} = \widetilde{(U\_i U\_j)}\_{DNS} - \widetilde{U}\_i \widetilde{U}*j - \delta*{ij} \frac{2}{3} K\_r^{DNS}$$
       * Computes the squared components and accumulates them.
       * Saves the computed $$\tau^r\_{ij}$$ to a file.
