Skip to content

Commit

Permalink
feat: support Differential(t) syntax in parse_variable
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Nov 20, 2024
1 parent afd55cb commit 2186298
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3292,7 +3292,7 @@ Return the variable in `sys` referred to by its string representation `str`.
Roughly supports the following CFG:
```
varname = "D(" varname ")" | arrvar | maybe_dummy_var
varname = "D(" varname ")" | "Differential(" iv ")(" varname ")" | arrvar | maybe_dummy_var
arrvar = maybe_dummy_var "[idxs...]"
idxs = int | int "," idxs
maybe_dummy_var = namespacedvar | namespacedvar "(" iv ")" |
Expand All @@ -3310,9 +3310,18 @@ function parse_variable(sys::AbstractSystem, str::AbstractString)
# I'd write a regex to validate `str`, but https://xkcd.com/1171/
str = strip(str)
derivative_level = 0
while startswith(str, "D(") && endswith(str, ")")
while ((cond1 = startswith(str, "D(")) || startswith(str, "Differential(")) && endswith(str, ")")
if cond1
derivative_level += 1
str = _string_view_inner(str, 2, 1)
continue
end
_tmpstr = _string_view_inner(str, 13, 1)
if !startswith(_tmpstr, "$iv)(")
throw(ArgumentError("Expected differential with respect to independent variable $iv in $str"))
end
derivative_level += 1
str = _string_view_inner(str, 2, 1)
str = _string_view_inner(_tmpstr, length(iv) + 2, 0)
end

arr_idxs = nothing
Expand Down
10 changes: 10 additions & 0 deletions test/variable_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ end
("D(😄.x($iv))", D(sys.😄.x)),
("D(😄₊x)", D(sys.😄.x)),
("D(😄₊x($iv))", D(sys.😄.x)),
("Differential($iv)(😄.x)", D(sys.😄.x)),
("Differential($iv)(😄.x($iv))", D(sys.😄.x)),
("Differential($iv)(😄₊x)", D(sys.😄.x)),
("Differential($iv)(😄₊x($iv))", D(sys.😄.x)),
# other derivative
("😄.xˍ$iv", D(sys.😄.x)),
("😄.x($iv$iv", D(sys.😄.x)),
Expand All @@ -113,6 +117,12 @@ end
("D(arr₊x[1])", D(sys.arr.x[1])),
("D(arr.x($iv)[1])", D(sys.arr.x[1])),
("D(arr₊x($iv)[1])", D(sys.arr.x[1])),
("Differential($iv)(arr.x($iv))", D(sys.arr.x)),
("Differential($iv)(arr₊x($iv))", D(sys.arr.x)),
("Differential($iv)(arr.x[1])", D(sys.arr.x[1])),
("Differential($iv)(arr₊x[1])", D(sys.arr.x[1])),
("Differential($iv)(arr.x($iv)[1])", D(sys.arr.x[1])),
("Differential($iv)(arr₊x($iv)[1])", D(sys.arr.x[1])),
# other derivative
("arr.xˍ$iv", D(sys.arr.x)),
("arr₊xˍ$iv", D(sys.arr.x)),
Expand Down

0 comments on commit 2186298

Please sign in to comment.