diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 36bd5ac800..37a7a74f2d 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -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 ")" | @@ -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 diff --git a/test/variable_utils.jl b/test/variable_utils.jl index ebbc3c0d3b..ecc2421955 100644 --- a/test/variable_utils.jl +++ b/test/variable_utils.jl @@ -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)), @@ -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)),