Double criteria on constraint generation

Hi,
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(
        	nodes=thermal_nodes,
        	geometry=geo.channel,
        	outvar={"normal_gradient_theta_f": 0},
        	batch_size=cfg.batch_size.ChannelWalls,
        	criteria=And(Eq(pow(x, 2) + pow(y, 2), pow(geo.channel_radius, 2)), 
				wall_criteria),
        	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 thermal.py 
[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 "thermal.py", 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/operations.py", line 418, in __new__
    _args = frozenset(cls._new_args_filter(args))
  File "/opt/conda/lib/python3.8/site-packages/sympy/logic/boolalg.py", line 669, in _new_args_filter
    args = BooleanFunction.binary_check_and_simplify(*args)
  File "/opt/conda/lib/python3.8/site-packages/sympy/core/operations.py", line 411, in <genexpr>
    args = (_sympify(arg) for arg in args)
  File "/opt/conda/lib/python3.8/site-packages/sympy/core/sympify.py", line 418, in _sympify
    return sympify(a, strict=True)
  File "/opt/conda/lib/python3.8/site-packages/sympy/core/sympify.py", 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?
Thanks

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(
        nodes=nodes,
        geometry=rec,
        outvar={"u": 1.0, "v": 0},
        batch_size=cfg.batch_size.TopWall,
        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(
        nodes=thermal_nodes,
        geometry=geo.channel,
        outvar={"normal_gradient_theta_f": 0},
        batch_size=cfg.batch_size.ChannelWalls,
        # criteria=And(x > 0.5, wall_criteria), # Not this
        criteria=wall_criteria,
        lambda_weighting={"normal_gradient_theta_f": 1.0},
        parameterization=geo.pr,
    )
    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.