# How should the scope of variables be embedded in ModelingToolkit?

**URL:** https://discourse.julialang.org/t/how-should-the-scope-of-variables-be-embedded-in-modelingtoolkit/126373
**Category:** Modelling & Simulations
**Tags:** modelingtoolkit
**Created:** [February 27, 2025, 6:54am UTC](https://discourse.julialang.org/t/how-should-the-scope-of-variables-be-embedded-in-modelingtoolkit/126373 "2025-02-27T06:54:17Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ysyecust](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ysyecust/32/210991_2.png) [@ysyecust](https://discourse.julialang.org/u/ysyecust)
#### Post date: [February 27, 2025, 6:54am UTC](https://discourse.julialang.org/t/how-should-the-scope-of-variables-be-embedded-in-modelingtoolkit/126373/1 "2025-02-27T06:54:17Z")

</div>

When using ModelingToolkit to model and using NonlinearSolve to solve, I find that the variable range I have set seems to be ineffective.

```julia
@variables z[1:numcomp] [bounds = (0,1),guess=0.2] 
@variables x[1:numcomp] [bounds = (0,1),guess=0.2] y[1:numcomp] [bounds = (0,1),guess=0.2] 
@variables flow_in [bounds = (0,100),guess=50] liquid_out [bounds = (0,100),guess=50] vapor_out [bounds = (0,100),guess=50]
@variables enthalpy_in [bounds = (-1e18,1e18),guess=50] enthalpy_liquid [bounds = (-1e18,1e18),guess=50] enthalpy_vapor [bounds = (-1e18,1e18),guess=50]
@variables vaporFraction [bounds = (0,1),guess=0.5] T [bounds = (100,500),guess=298.15] P [bounds = (0,10132500),guess=101325] 
@variables Q
@parameters flow_scale

```

How should I set the variable range to be an effective constraint when solving a system of nonlinear equations?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [March 1, 2025, 2:46pm UTC](https://discourse.julialang.org/t/how-should-the-scope-of-variables-be-embedded-in-modelingtoolkit/126373/2 "2025-03-01T14:46:48Z")

</div>

ModelingToolkit is missing a check for the bounds, and NonlinearSolve.jl cannot force bounds on solves. It should throw an error here. Can you please open an issue on the ModelingToolkit.jl?

Using an unconstrained optimization with a trivial cost function (i.e. a constraint satisfaction problem) would be the right way to get solvers for this.

MTK could do a transform to make this work well, or NonlienarSolve could, but it at least doesn’t right now.

`bounds = (-1e18,1e18)` and that’s a bad idea 😅

---

<div class="post-metadata">

### Author: ![ysyecust](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ysyecust/32/210991_2.png) [@ysyecust](https://discourse.julialang.org/u/ysyecust)
#### Post date: [March 4, 2025, 5:27am UTC](https://discourse.julialang.org/t/how-should-the-scope-of-variables-be-embedded-in-modelingtoolkit/126373/3 "2025-03-04T05:27:23Z")

</div>

Thank you for your reply, I will go back and submit the relevant issue.
