# How to make nodes for geometry's sdf

I am referring to the Fully Developed Turbulent Channel in the tutorial. In ver 22.03, the distance from the wall could be referenced by making a node of the geometry’s sdf.

``````nodes = (
~
+ [Node.from_sympy(geo_sdf.sdf, "normal_distance")]
~
)
``````

In ver 22.09, however, the geometry’s sdf is not made and the distance from the wall is passed as a constant to argument "parameter” of PointwiseBoundaryConstraint.

``````wf_pt = PointwiseBoundaryConstraint(
~
parameterization={"normal_distance": resolved_y_start},
)
domain.add_constraint(wf_pt, "WF")
``````

In ver 22.09, when I tried to make a node of the geometry’s sdf as before, the following error occurred.

raceback (most recent call last):
File “mycode.py”, line 114, in run
+ [Node.from_sympy(geo.sdf, ‘normal_distance’)]
File “/modulus/modulus/node.py”, line 93, in from_sympy
evaluate = SympyToTorch(sub_eq, out_name, freeze_terms, detach_names)
File “/modulus/modulus/utils/sympy/torch_printer.py”, line 252, in init
self.keys = sorted([k.name for k in sympy_expr.free_symbols])
AttributeError: ‘function’ object has no attribute ‘free_symbols’

Can you tell me how to generate a node of the geometry’s sdf?

The SDF of the geometry can be accessed using a SymPy symbol (seen here used for lambda weighting). Alternatively it can be accessed directing using the SDF attribute which will output a dictionary containing a SDF variable (example here).

I would also suggest looking at the zero equation turbulence model which has the SDF in its equation for guidance.

In the turbulent channel problem you reference, the 22.09 implementation is essentially training on a slightly smaller channel where the domain is between the edges of the wall-function. Hence the boundary is the value of the wall function at a fixed point (not no-slip).

Hi, @ngeneva

Thank you for your response.
The use of sdf in the tutorial on zero equation turbulence model is what I was looking for.

I have two additional problems.

1. In ver. 22.09, I get the following error if I do not specify requires_grad=True in PoinrwiseInferencer etc. In ver. 22.03, there was no problem even if requires_grad=False.

RuntimeError: The following operation failed in the TorchScript interpreter.
Traceback of TorchScript (most recent call last):

File “/modulus/modulus/eq/derivatives.py”, line 24, in gradient
“”"
grad_outputs: List[Optional[torch.Tensor]] = [torch.ones_like(y, device=y.device)]
grad = torch.autograd.grad(
~~~~~~~~~~~~~ <— HERE
[
y,
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

In what case is requires_grad=True needed?

2. I am using PointVYKInferencer for prediction. What should I do if there are sdf itself or quantities which has sdf in its equations in the `output_names`?
In the case of PointwiseInferencer, sdf is supposed to be calculated by passing geometory.sample_interior to `invar`. Likewise, should I pass the VTK lattice points containing the sdf to `invar` in some way?

In what case is requires_grad=True needed?

This is required when you want to save results that are gradients of the input. `requires_grad` turns on gradients for the model inputs. E.g. if I have a model that computes `u = f(x)`, where `f(.)` is my NN, and I want to have an output value `du/dx` then `requires_grad=True` because I need gradients to be calculated on the input tensors. Without this grad variables will not be initialized and PyTorch will complain.

Likewise, should I pass the VTK lattice points containing the sdf to `invar` in some way?

Yes, basically the VTK inferencer is designed more for conducting inference on a predefined geometry as opposed to randomly sampling geometry with the regular `PointwiseInferencer`. This means you need to provide the SDF through the VTK object or as an additional input array of the appropriate size.

Side note, the regular pointwise inferencer will still save the results in a Poly VTK file. The downside is that you wont have any connectivity between these points for a mesh (although can could do this in post). Some more information on VTK utils is in our user-guide.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.