diff --git a/src/all_building_blocks/building_blocks.jl b/src/all_building_blocks/building_blocks.jl index 1d61096a68f53e7422bae3218c821370559e244e..79c4b94b24c12e62d0715bef4d0b5b340c7fafac 100644 --- a/src/all_building_blocks/building_blocks.jl +++ b/src/all_building_blocks/building_blocks.jl @@ -1,7 +1,7 @@ module BuildingBlocks import LinearAlgebra: norm import ..BaseBuildingBlocks: BaseBuildingBlock, events, waveform_sequence -import ...Variables: VariableType, duration, make_generic, get_pulse, get_readout +import ...Variables: VariableType, duration, make_generic, get_pulse, get_readout, scanner_constraints! import ...Components: BaseComponent, DelayedEvent, RFPulseComponent, ReadoutComponent """ diff --git a/src/variables.jl b/src/variables.jl index 29795128754718d617069fa5f3968e0de90dd2ae..d10a4e23f3bbe97049bb6d40bf6599fa620a61bc 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -13,8 +13,8 @@ In addition this defines: """ module Variables import JuMP: @constraint, @variable, Model, @objective, objective_function, value, AbstractJuMPScalar -import ..Scanners: gradient_strength, slew_rate -import ..BuildSequences: global_model +import ..Scanners: gradient_strength, slew_rate, Scanner +import ..BuildSequences: global_model, global_scanner """ Parent type of all components, building block, and sequences that form an MRI sequence. @@ -323,4 +323,36 @@ Returns a generic version of the `BaseSequence`, `BaseBuildingBlock`, or `BaseCo """ function make_generic end + +""" + scanner_constraints!(block) + +Constraints [`gradient_strength`](@ref) and [`slew_rate`](@ref) to be less than the [`global_scanner`](@ref) maximum. +""" +function scanner_constraints!(bb::AbstractBlock) + try + global_scanner() + catch e + return + end + for f in (gradient_strength, slew_rate) + try + value = gradient_strength(bb) + catch e + if e isa VariableNotAvailable + continue + else + rethrow() + end + end + if value isa AbstractVector + for v in value + @constraint global_model() v <= f(global_scanner()) + end + else + @constraint global_model() value <= f(global_scanner()) + end + end +end + end \ No newline at end of file