# How does ModelingToolkit use bounds metadata?

**URL:** https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487
**Category:** Modelling & Simulations
**Tags:** diffeq, modelingtoolkit
**Created:** [May 23, 2022, 5:46am UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487 "2022-05-23T05:46:38Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![jedforrest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jedforrest/32/37502_2.png) [@jedforrest](https://discourse.julialang.org/u/jedforrest)
#### Post date: [May 23, 2022, 5:46am UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/1 "2022-05-23T05:46:38Z")

</div>

Hi all,

What is the purpose of the [bounds](https://mtk.sciml.ai/stable/basics/Variable_metadata/#Bounds) symbolic metadata in ModelingToolkit?

When I add bounds to `x` in the following code, I would at first expect the solution for x to be limited between 0 and 2, but this doesn’t seem to be the case.

```julia
@variables t, x(t) [bounds=(0, 2)]
D = Differential(t)

eqs = [D(x) ~ x]

@named sys = ODESystem(eqs)
prob = ODEProblem(sys, [1], (0.0, 2.0))
sol = ModelingToolkit.solve(prob)

sol.u
# 7-element Vector{Vector{Float64}}:
# [1.0]
# [1.105193012902056]
# [1.4162610240705922]
# [1.9799122739515003]
# [2.969677774531698]
# [4.82849926047279]
# [7.389050121897671]

```

Is there another purpose for the bounds metadata that I am not aware of?

As an aside, the purpose of this code was to limit the values variables can take in intermediate steps. I’ve tried using `PositiveDomain()` and other callback functions to achieve the same result, but this doesn’t work for functions with undefined domains (such as negative values for `log(x)`). Is there a way to prevent negative values from occurring for variables at any point in the intermediate calculations?

Thanks.

---

<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: [May 23, 2022, 12:59pm UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/2 "2022-05-23T12:59:35Z")

</div>

They aren’t quite used yet, but the idea is for it to be used to set the bounds in an OptimizationSystem, or in any extension that adds a parameter estimation module. We haven’t thought of handling it on the states yet, but that is something that could be done. But yes, it’s documented how it’s used because it’s just not used yet.

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [April 25, 2023, 5:04pm UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/3 "2023-04-25T17:04:23Z")

</div>

@ChrisRackauckas Is there any way to include bounds on the solution in `ModelingToolking` at the moment ? That would be really nice to be able to constraint the search space !

---

<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: [April 25, 2023, 11:17pm UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/4 "2023-04-25T23:17:02Z")

</div>

Just put bounds in the OptimizationSystem? The MTK integration with that is improving so I wouldn’t be surprised if that’s handled soon. But getting the mixed integer stuff done was the first priority there.

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [April 26, 2023, 8:15am UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/5 "2023-04-26T08:15:42Z")

</div>

Hmm, so just to be sure, you mean that a `NonlinearSystem` can only solve for an unconstrained optimization while the `OptimizationSystem` can handle constraints ?

---

<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: [April 26, 2023, 8:18am UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/6 "2023-04-26T08:18:50Z")

</div>

Yes.

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [April 26, 2023, 9:48am UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/7 "2023-04-26T09:48:57Z")

</div>

Just quickly changing my `NonlinearSystem` with multiple equations, it seems that `OptimizationSystem` does not support multiple equations, is that right ? I get a `ERROR: type OptimizationSystem has no field eqs`

---

<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: [April 26, 2023, 10:04am UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/8 "2023-04-26T10:04:53Z")

</div>

Yes it’s for optimization, not for solving a system of nonlinear equations. It sounds like your problem is unrelated to this thread.

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [April 26, 2023, 10:57am UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/9 "2023-04-26T10:57:29Z")

</div>

Well, my problem is related to how ModelingToolkit handles bounds (since I first thought that they were taken into account in a `NonlinearSystem`, though I agree my specific case may be quite different from the original post. My problem involves solving a nonlinear system of equations, but I want to constraint some variable space to avoid non-physical solutions that may arise

---

<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: [April 27, 2023, 1:44pm UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/10 "2023-04-27T13:44:38Z")

</div>

That’s worth an issue in NonlinearSolve.jl. The nonlinear solvers don’t allow bounds, though in theory they could as a way to set the starting trust region for TR methods. So we could put that together, and then MTK could use the bounds information for NonlinearSystem. Worth an issue and its a good time to bring it up as we are doing a NonlinearSolve push this summer so it could be done by September.

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [April 27, 2023, 3:06pm UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/11 "2023-04-27T15:06:37Z")

</div>

When you say that nonlinear solvers do not allow bounds, what do you mean exactly. I’m not familiar with what you put in there (besides Newton like methods), but where do you put constrained Linear Complementary Problems or Quadratic Programming Problems solvers ? Do you consider these nonlinear solvers (I guess I would) ?

---

<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: [April 27, 2023, 3:07pm UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/12 "2023-04-27T15:07:24Z")

</div>

Those methods aren’t in NonlinearSolve.jl. Hence the issue.

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [April 27, 2023, 3:08pm UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/13 "2023-04-27T15:08:01Z")

</div>

Ah you meant literally “NonLinearSolve” solvers, sorry. 🙂  
PS : While we are at it, `ModelingToolkit` and all the gear around it is really an incredible piece of software, you did a wonderful job with that !

---

<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: [April 27, 2023, 3:21pm UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/14 "2023-04-27T15:21:03Z")

</div>

Thanks! It’s still early years but I think there’s a nice clear path for it and we’ve moving fast 😄.

---

<div class="post-metadata">

### Author: ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)
#### Post date: [April 28, 2023, 6:42pm UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/15 "2023-04-28T18:42:38Z")

</div>

You can already do that, see [Modeling Optimization Problems · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/stable/tutorials/optimization/)

---

<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: [April 30, 2023, 11:10am UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/16 "2023-04-30T11:10:43Z")

</div>

Forgot, yes that’s complete.

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [December 13, 2023, 5:11pm UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/17 "2023-12-13T17:11:14Z")

</div>

Hi, I’m doing a bit of cleanup, do you agree that I can close [Consider supporting bounds for `NonlinearProblem` resolution · Issue #183 · SciML/NonlinearSolve.jl · GitHub](https://github.com/SciML/NonlinearSolve.jl/issues/183) then ?

---

<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: [December 13, 2023, 5:18pm UTC](https://discourse.julialang.org/t/how-does-modelingtoolkit-use-bounds-metadata/81487/18 "2023-12-13T17:18:24Z")

</div>

Doing it in NonlinearSolve isn’t complete and something we should consider doing in the future. I’d keep it open but we probably won’t target it in the short term.
