Skip to content

Commit 419208b

Browse files
Merge pull request #3852 from SciML/document-evalat
Document EvalAt operator
2 parents 94b5775 + 64fdd28 commit 419208b

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

docs/src/API/variables.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ ModelingToolkit also defines a plethora of custom operators.
337337
Pre
338338
Initial
339339
Shift
340+
EvalAt
340341
```
341342

342343
While not an operator, `ShiftIndex` is commonly used to use `Shift` operators in a more

src/variables.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,59 @@ getshift(x::Symbolic) = Symbolics.getmetadata(x, VariableShift, 0)
578578
###################
579579
### Evaluate at ###
580580
###################
581+
"""
582+
EvalAt(t)
583+
584+
An operator that evaluates time-dependent variables at a specific absolute time point `t`.
585+
586+
# Fields
587+
- `t::Union{Symbolic, Number}`: The absolute time at which to evaluate the variable.
588+
589+
# Description
590+
`EvalAt` is used to evaluate time-dependent variables at a specific time point. This is particularly
591+
useful in optimization problems where you need to specify constraints or costs at particular moments
592+
in time, or delay differential equations for setting a delay time.
593+
594+
The operator works by replacing the time argument of time-dependent variables with the specified
595+
time `t`. For variables that don't depend on time, `EvalAt` returns them unchanged.
596+
597+
# Behavior
598+
- For time-dependent variables like `x(t)`, `EvalAt(τ)(x)` returns `x(τ)`
599+
- For time-independent parameters, `EvalAt` returns them unchanged
600+
- For derivatives, `EvalAt` evaluates the derivative at the specified time
601+
- For arrays of variables, `EvalAt` is applied element-wise
602+
603+
604+
# Examples
605+
```julia
606+
using ModelingToolkit
607+
608+
@variables t x(t) y(t)
609+
@parameters p
610+
611+
# Evaluate x at time t=1.0
612+
EvalAt(1.0)(x) # Returns x(1.0)
613+
614+
# Works with parameters (returns unchanged)
615+
EvalAt(1.0)(p) # Returns p
616+
617+
# Works with derivatives
618+
D = Differential(t)
619+
EvalAt(1.0)(D(x)) # Returns D(x) evaluated at t=1.0
620+
621+
# Use in optimization constraints
622+
@optimization_model model begin
623+
@constraints begin
624+
EvalAt(0.5)(x) ~ 2.0 # x must equal 2.0 at t=0.5
625+
end
626+
end
627+
```
628+
629+
# Errors
630+
- Throws an error when applied to variables with more than one argument (e.g., `z(u, t)`)
631+
632+
See also: [`Differential`](@ref)
633+
"""
581634
struct EvalAt <: Symbolics.Operator
582635
t::Union{Symbolic, Number}
583636
end

0 commit comments

Comments
 (0)