@@ -63,6 +63,10 @@ Keyword Arguments:
63
63
programming language
64
64
- `MATLABTarget`: Generates an anonymous function for use in MATLAB and Octave
65
65
environments
66
+ - `fname`: Used by some targets for the name of the function in the target space.
67
+
68
+ Note that not all build targets support the full compilation interface. Check the
69
+ individual target documentation for details.
66
70
"""
67
71
function build_function (args... ;target = JuliaTarget (),kwargs... )
68
72
_build_function (target,args... ;kwargs... )
@@ -455,11 +459,73 @@ function numbered_expr(de::ModelingToolkit.Equation,args...;varordering = args[1
455
459
end
456
460
numbered_expr (c:: ModelingToolkit.Constant ,args... ;kwargs... ) = c. value
457
461
458
- function _build_function (target:: StanTarget , eqs, vs, ps, iv,
459
- conv = simplified_expr, expression = Val{true };
462
+ """
463
+ Build function target: CTarget
464
+
465
+ ```julia
466
+ function _build_function(target::CTarget, eqs::Array{<:Equation}, args...;
467
+ conv = simplified_expr, expression = Val{true},
468
+ fname = :diffeqf,
469
+ lhsname=:du,rhsnames=[Symbol("RHS$i ") for i in 1:length(args)],
470
+ libpath=tempname(),compiler=:gcc)
471
+ ```
472
+
473
+ This builds an in-place C function. Only works on arrays of equations. If
474
+ `expression == Val{false}`, then this builds a function in C, compiles it,
475
+ and returns a lambda to that compiled function. These special keyword arguments
476
+ control the compilation:
477
+
478
+ - libpath: the path to store the binary. Defaults to a temporary path.
479
+ - compiler: which C compiler to use. Defaults to :gcc, which is currently the
480
+ only available option.
481
+ """
482
+ function _build_function (target:: CTarget , eqs:: Array{<:Equation} , args... ;
483
+ conv = simplified_expr, expression = Val{true },
484
+ fname = :diffeqf ,
485
+ lhsname= :du ,rhsnames= [Symbol (" RHS$i " ) for i in 1 : length (args)],
486
+ libpath= tempname (),compiler= :gcc )
487
+ differential_equation = string (join ([numbered_expr (eq,args... ,lhsname= lhsname,
488
+ rhsnames= rhsnames,offset= - 1 ) for
489
+ (i, eq) ∈ enumerate (eqs)]," ;\n " )," ;" )
490
+
491
+ argstrs = join (vcat (" double* $(lhsname) " ,[typeof (args[i])<: Array ? " double* $(rhsnames[i]) " : " double $(rhsnames[i]) " for i in 1 : length (args)])," , " )
492
+ ex = """
493
+ void $fname ($(argstrs... ) ) {
494
+ $differential_equation
495
+ }
496
+ """
497
+
498
+ if expression == Val{true }
499
+ return ex
500
+ else
501
+ @assert compiler == :gcc
502
+ ex = build_function (eqs,args... ;target= ModelingToolkit. CTarget ())
503
+ open (` gcc -fPIC -O3 -msse3 -xc -shared -o $(libpath * " ." * Libdl. dlext) -` , " w" ) do f
504
+ print (f, ex)
505
+ end
506
+ eval (:((du:: Array{Float64} ,u:: Array{Float64} ,p:: Array{Float64} ,t:: Float64 ) -> ccall ((" diffeqf" , $ libpath), Cvoid, (Ptr{Float64}, Ptr{Float64}, Ptr{Float64}, Float64), du, u, p, t)))
507
+ end
508
+ end
509
+
510
+ """
511
+ Build function target: StanTarget
512
+
513
+ ```julia
514
+ function _build_function(target::StanTarget, eqs::Array{<:Equation}, vs, ps, iv;
515
+ conv = simplified_expr, expression = Val{true},
460
516
fname = :diffeqf, lhsname=:internal_var___du,
461
- varname= :internal_var___u ,paramname= :internal_var___p )
462
- rhsnames= [varname,paramname]
517
+ rhsnames=[:internal_var___u,:internal_var___p,:internal_var___t])
518
+ ```
519
+
520
+ This builds an in-place Stan function compatible with the Stan differential equation solvers.
521
+ Unlike other build targets, this one requestions (vs, ps, iv) as the function arguments.
522
+ Only allowed on arrays of equations.
523
+ """
524
+ function _build_function (target:: StanTarget , eqs:: Array{<:Equation} , vs, ps, iv;
525
+ conv = simplified_expr, expression = Val{true },
526
+ fname = :diffeqf , lhsname= :internal_var___du ,
527
+ rhsnames= [:internal_var___u ,:internal_var___p ,:internal_var___t ])
528
+ @assert expression == Val{true }
463
529
differential_equation = string (join ([numbered_expr (eq,vs,ps,lhsname= lhsname,
464
530
rhsnames= rhsnames) for
465
531
(i, eq) ∈ enumerate (eqs)]," ;\n " )," ;" )
@@ -472,26 +538,26 @@ function _build_function(target::StanTarget, eqs, vs, ps, iv,
472
538
"""
473
539
end
474
540
475
- function _build_function (target:: CTarget , eqs, vs, ps, iv;
541
+ """
542
+ Build function target: MATLABTarget
543
+
544
+ ```julia
545
+ function _build_function(target::MATLABTarget, eqs::Array{<:Equation}, args...;
476
546
conv = simplified_expr, expression = Val{true},
477
- fname = :diffeqf , derivname= :internal_var___du ,
478
- varname= :internal_var___u ,paramname= :internal_var___p )
479
- differential_equation = string (join ([numbered_expr (eq,vs,ps,lhsname= derivname,
480
- rhsnames= [varname,paramname],offset= - 1 ) for
481
- (i, eq) ∈ enumerate (eqs)]," ;\n " )," ;" )
482
- """
483
- void $fname (double* $derivname , double* $varname , double* $paramname , double $iv ) {
484
- $differential_equation
485
- }
486
- """
487
- end
547
+ lhsname=:internal_var___du,
548
+ rhsnames=[:internal_var___u,:internal_var___p,:internal_var___t])
549
+ ```
488
550
489
- function _build_function (target:: MATLABTarget , eqs, vs, ps, iv;
551
+ This builds an out of place anonymous function @(t,rhsnames[1]) to be used in MATLAB.
552
+ Compatible with the MATLAB differential equation solvers. Only allowed on arrays
553
+ of equations.
554
+ """
555
+ function _build_function (target:: MATLABTarget , eqs:: Array{<:Equation} , args... ;
490
556
conv = simplified_expr, expression = Val{true },
491
- fname = :diffeqf , derivname = :internal_var___du ,
492
- varname = :internal_var___u ,paramname = :internal_var___p )
493
- rhsnames = [varname,paramname]
494
- matstr = join ([numbered_expr (eq. rhs,vs,ps, lhsname= derivname ,
557
+ fname = :diffeqf , lhsname = :internal_var___du ,
558
+ rhsnames = [ :internal_var___u ,:internal_var___p , :internal_var___t ] )
559
+ @assert expression == Val{ true }
560
+ matstr = join ([numbered_expr (eq. rhs,args ... , lhsname= lhsname ,
495
561
rhsnames= rhsnames) for
496
562
(i, eq) ∈ enumerate (eqs)]," ; " )
497
563
@@ -500,22 +566,3 @@ function _build_function(target::MATLABTarget, eqs, vs, ps, iv;
500
566
matstr = " $fname = @(t,$(rhsnames[1 ]) ) [" * matstr* " ];"
501
567
matstr
502
568
end
503
-
504
- """
505
- compile_cfunction(eqs,args...;libpath=tempname(),compiler=:gcc)
506
-
507
- Builds a function in C, compiles it, and returns a lambda to that compiled function.
508
- Arguments match those of `build_function`. Keyword arguments:
509
-
510
- - libpath: the path to store the binary. Defaults to a temporary path.
511
- - compiler: which C compiler to use. Defaults to :gcc, which is currently the
512
- only available option.
513
- """
514
- function compile_cfunction (eqs,args... ;libpath= tempname (),compiler= :gcc )
515
- @assert compiler == :gcc
516
- ex = build_function (eqs,args... ;target= ModelingToolkit. CTarget ())
517
- open (` gcc -fPIC -O3 -msse3 -xc -shared -o $(libpath * " ." * Libdl. dlext) -` , " w" ) do f
518
- print (f, ex)
519
- end
520
- eval (:((du:: Array{Float64} ,u:: Array{Float64} ,p:: Array{Float64} ,t:: Float64 ) -> ccall ((" diffeqf" , $ libpath), Cvoid, (Ptr{Float64}, Ptr{Float64}, Ptr{Float64}, Float64), du, u, p, t)))
521
- end
0 commit comments