Double criteria on constraint generation

i’m working on a problem similar to “conjugate heat transfer example”. I’d like to add double criteria condition in a constraint. So i try to add something like this:

	def wall_criteria(invar, params):
		sdf = geo.piece.sdf(invar, params)
		return np.less(sdf["sdf"], -1e-5)

	# Channel walls
	print("adding channel_walls_constraint")
	channel_walls = PointwiseBoundaryConstraint(
        	outvar={"normal_gradient_theta_f": 0},
        	criteria=And(Eq(pow(x, 2) + pow(y, 2), pow(geo.channel_radius, 2)), 
        	lambda_weighting={"normal_gradient_theta_f": 1.0},
	thermal_domain.add_constraint(channel_walls, "channel_walls")

But this create the error below:

root@58c61e17b357:/examples/autoclave_flow/new_data_file# python 
[15:06:55] - JIT using the NVFuser TorchScript backend
[15:06:55] - JitManager: {'_enabled': True, '_arch_mode': <JitArchMode.ONLY_ACTIVATION: 1>, '_use_nvfuser': True, '_autograd_nodes': False}
[15:06:55] - GraphManager: {'_func_arch': False, '_debug': False, '_func_arch_allow_partial_hessian': True}
adding inlet_constraint
adding outlet_constraint
adding channel_walls_constraint
Error executing job with overrides: []
Traceback (most recent call last):
  File "", line 152, in run
    criteria=And(Eq(pow(x, 2) + pow(y, 2), pow(geo.channel_radius, 2)),
  File "/opt/conda/lib/python3.8/site-packages/sympy/core/", line 418, in __new__
    _args = frozenset(cls._new_args_filter(args))
  File "/opt/conda/lib/python3.8/site-packages/sympy/logic/", line 669, in _new_args_filter
    args = BooleanFunction.binary_check_and_simplify(*args)
  File "/opt/conda/lib/python3.8/site-packages/sympy/core/", line 411, in <genexpr>
    args = (_sympify(arg) for arg in args)
  File "/opt/conda/lib/python3.8/site-packages/sympy/core/", line 418, in _sympify
    return sympify(a, strict=True)
  File "/opt/conda/lib/python3.8/site-packages/sympy/core/", line 340, in sympify
    raise SympifyError(a)
sympy.core.sympify.SympifyError: SympifyError: <function run.<locals>.wall_criteria at 0x7fb445dd4d30>

Set the environment variable HYDRA_FULL_ERROR=1 for a complete stack trace.

Is it possible to add condition of this form in the same constraint?

Hi @tom_02

This should be achievable. At least if it can be compiled to a Sympy expression it should work. For example I can use the following in the LDC example with a double criteria:

top_wall = PointwiseBoundaryConstraint(
        outvar={"u": 1.0, "v": 0},
        lambda_weighting={"u": 1.0 - 20 * Abs(x), "v": 1.0},  # weight edges to be zero
        criteria=And(Eq(y, height / 2), GreaterThan(x, 0))
    ldc_domain.add_constraint(top_wall, "top_wall")

I can reproduce the error you’re seeing, it could be from the mixing of the custom function + sympy expression. But I am not 100% certain.

One alternative you could try is to do both criteria in the wall_criteria function. I believe x and y should be available in your invar dictionary, so you could do this all in numpy using np.logical_and. E.g.

def wall_criteria(invar, params):
        sdf = geo.three_fin.sdf(invar, params)
        return np.logical_and(np.greater(invar['x'], 0.5), np.less(sdf["sdf"], -1e-5))

    channel_walls = PointwiseBoundaryConstraint(
        outvar={"normal_gradient_theta_f": 0},
        # criteria=And(x > 0.5, wall_criteria), # Not this
        lambda_weighting={"normal_gradient_theta_f": 1.0},,
    thermal_domain.add_constraint(channel_walls, "channel_walls")

Just as warning, this can be tricky for sampling these points. Particularly if you have an equals condition. This criteria is used to assess if randomly sampled points are valid or not, so having a condition that requires a point to exactly be on a certain boundary/constraint is impossible (random generation). You’d be better off defining a new boundary geometry for it if that’s needed.

1 Like

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