Skip to content

Commit 15abe93

Browse files
chore: add change_tunables
1 parent 53a4867 commit 15abe93

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/parameters.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,27 @@ function split_parameters_by_type(ps)
147147
end
148148
end
149149
end
150+
151+
"""
152+
$(TYPEDSIGNATURES)
153+
154+
Change the tunable parameters of a system to a new set of tunables.
155+
156+
The new tunable parameters must be a subset of the current tunables as discovered by [`tunable_parameters`](@ref).
157+
The remaining parameters will be set as constants in the system.
158+
"""
159+
function change_tunables(sys, new_tunables)
160+
cur_tunables = tunable_parameters(sys, parameters(sys))
161+
diff_params = setdiff(cur_tunables, new_tunables)
162+
163+
if !isempty(setdiff(new_tunables, cur_tunables))
164+
throw(ArgumentError("New tunables must be a subset of the current tunables. Found tunable parameters not in the system: $(setdiff(new_tunables, cur_tunables))"))
165+
end
166+
cur_ps = get_ps(sys)
167+
const_ps = toconstant.(diff_params)
168+
169+
new_ps = replace(cur_ps, (diff_params .=> const_ps)...)
170+
new_ps = replace(new_ps, (Initial.(diff_params) .=> Initial.(const_ps))...)
171+
@set! sys.ps = new_ps
172+
complete(sys)
173+
end

0 commit comments

Comments
 (0)