@@ -424,11 +424,11 @@ function DiscreteModel{Solver}(circ::Circuit, t::Real, ::Type{Solver}=HomotopySo
424424 end
425425 end
426426 end )
427- solver = Solver (ParametricNonLinEq (nonlinear_eq_func, nonlinear_eq_set_p,
428- nonlinear_eq_calc_Jp,
429- (zeros (model_nq), zeros (model_nn, model_nq)),
430- model_nn, model_np),
431- zeros (model_np), init_z)
427+ solver = eval (:( $ Solver (ParametricNonLinEq ($ nonlinear_eq_func, $ nonlinear_eq_set_p,
428+ $ nonlinear_eq_calc_Jp,
429+ (zeros ($ model_nq), zeros ($ model_nn, $ model_nq)),
430+ $ model_nn, $ model_np),
431+ zeros ($ model_np), $ init_z)) )
432432 return DiscreteModel {typeof(solver)} (mats, model_nonlinear_eq, solver)
433433end
434434
@@ -504,21 +504,21 @@ function initial_solution(nleq, q0, fq)
504504 # determine an initial solution with a homotopy solver that may vary q0
505505 # between 0 and the true q0 -> q0 takes the role of p
506506 nq, nn = size (fq)
507- init_nl_eq_func = eval (quote
508- (res, J, scratch, z) ->
507+ return eval (quote
508+ init_nl_eq_func = (res, J, scratch, z) ->
509509 let pfull= scratch[1 ], Jp= scratch[2 ], q= $ (zeros (nq)),
510510 Jq= $ (zeros (nn, nq)), fq= $ (fq)
511511 $ (nleq)
512512 return nothing
513513 end
514+ init_nleq = ParametricNonLinEq (init_nl_eq_func, $ nn, $ nq)
515+ init_solver = HomotopySolver {SimpleSolver} (init_nleq, zeros ($ nq), zeros ($ nn))
516+ init_z = solve (init_solver, $ q0)
517+ if ! hasconverged (init_solver)
518+ error (" Failed to find initial solution" )
519+ end
520+ return init_z
514521 end )
515- init_nleq = ParametricNonLinEq (init_nl_eq_func, nn, nq)
516- init_solver = HomotopySolver {SimpleSolver} (init_nleq, zeros (nq), zeros (nn))
517- init_z = solve (init_solver, q0)
518- if ! hasconverged (init_solver)
519- error (" Failed to find initial solution" )
520- end
521- return init_z
522522end
523523
524524nx (model:: DiscreteModel ) = length (model. x0)
@@ -530,25 +530,26 @@ nn(model::DiscreteModel) = size(model.fq, 2)
530530
531531function steadystate (model:: DiscreteModel , u= zeros (nu (model)))
532532 IA_LU = lufact (eye (nx (model))- model. a)
533- steady_nl_eq_func = eval (quote
534- (res, J, scratch, z) ->
533+ steady_q0 = model. q0 + model. pexp* (model. dq/ IA_LU* model. b + model. eq)* u +
534+ model. pexp* model. dq/ IA_LU* model. x0
535+ steady_z = eval (quote
536+ steady_nl_eq_func = (res, J, scratch, z) ->
535537 let pfull= scratch[1 ], Jp= scratch[2 ],
536538 q= $ (zeros (nq (model))), Jq= $ (zeros (nn (model), nq (model))),
537539 fq= $ (model. pexp* model. dq/ IA_LU* model. c + model. fq)
538540 $ (model. nonlinear_eq)
539541 return nothing
540542 end
543+ steady_nleq = ParametricNonLinEq (steady_nl_eq_func, nn ($ model), nq ($ model))
544+ steady_solver = HomotopySolver {SimpleSolver} (steady_nleq, zeros (nq ($ model)),
545+ zeros (nn ($ model)))
546+ set_resabs2tol! (steady_solver, 1e-30 )
547+ steady_z = solve (steady_solver, $ steady_q0)
548+ if ! hasconverged (steady_solver)
549+ error (" Failed to find steady state solution" )
550+ end
551+ return steady_z
541552 end )
542- steady_nleq = ParametricNonLinEq (steady_nl_eq_func, nn (model), nq (model))
543- steady_solver = HomotopySolver {SimpleSolver} (steady_nleq, zeros (nq (model)),
544- zeros (nn (model)))
545- set_resabs2tol! (steady_solver, 1e-30 )
546- steady_q0 = model. q0 + model. pexp* (model. dq/ IA_LU* model. b + model. eq)* u +
547- model. pexp* model. dq/ IA_LU* model. x0
548- steady_z = solve (steady_solver, steady_q0)
549- if ! hasconverged (steady_solver)
550- error (" Failed to find steady state solution" )
551- end
552553 return IA_LU\ (model. b* u + model. c* steady_z + model. x0)
553554end
554555
0 commit comments