Skip to content

Instantly share code, notes, and snippets.

@Kadeluxe
Last active September 7, 2021 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kadeluxe/b10eb5f32fabaa77c64d45b4e1a8e29f to your computer and use it in GitHub Desktop.
Save Kadeluxe/b10eb5f32fabaa77c64d45b4e1a8e29f to your computer and use it in GitHub Desktop.
alias TargetTank d0
alias TargetPipe d1
alias BufferPipe d2
alias HotPump d3
alias HotPipe d4
define FINAL_TEMPERATURE 295.15 # 20C, adjust as needed
define HEAT_EXCHANGER_VOLUME 500 # const
define R 8.3144 # const
define HEAT_CO2 28.2 # const
define HEAT_N 20.6 # const
define HEAT_N2O 23 # const
define HEAT_O2 21.1 # const
define HEAT_X 24.8 # const
define HEAT_H2 20.4 # const
alias TargetTotalMoles r15
alias TargetCurrentEnergy r14
alias TargetFinalEnergy r13
alias BufferTotalMoles r12
alias BufferCurrentEnergy r11
alias BufferFinalEnergy r10
alias CurrentTemperature r9
alias HotBufferTemperature r8
alias CurrentDevice r7
alias CurrentDeviceRegister dr7
Loop:
# calculate amount of moles on the cold side
l r0 TargetTank TotalMoles
l r1 TargetPipe TotalMoles
add TargetTotalMoles r0 r1 # tank + pipe
# heat exchanger has its own internal volume too
l r0 TargetPipe Pressure
l CurrentTemperature TargetPipe Temperature
mul r2 r0 HEAT_EXCHANGER_VOLUME
mul r3 CurrentTemperature R
div r4 r2 r3 # moles inside heat exchanger
add TargetTotalMoles TargetTotalMoles r4 # moles in the whole cold network
# cold side energy
move CurrentDevice 0 # d0
jal CalculateHeatCapacity
mul r0 r0 TargetTotalMoles
mul TargetCurrentEnergy r0 CurrentTemperature # cold network current energy
mul TargetFinalEnergy r0 FINAL_TEMPERATURE # cold network expected energy
# calculate amount of moles on the hot side
l BufferTotalMoles BufferPipe TotalMoles # moles inside buffer pipe
# calculate hot moles inside heat exchanger
l r1 BufferPipe Pressure
l HotBufferTemperature BufferPipe Temperature
mul r1 r1 HEAT_EXCHANGER_VOLUME
mul r2 HotBufferTemperature R
div r4 r1 r2
add BufferTotalMoles BufferTotalMoles r4 # moles in the whole hot network
# hot buffer energy
move CurrentDevice 2 # d2
jal CalculateHeatCapacity
mul r0 r0 BufferTotalMoles
mul BufferCurrentEnergy r0 HotBufferTemperature # hot buffer current energy
mul BufferFinalEnergy r0 FINAL_TEMPERATURE # hot buffer expected energy
# calculate energy delta of the whole system
add r0 TargetCurrentEnergy BufferCurrentEnergy # current energy of the whole system
add r1 TargetFinalEnergy BufferFinalEnergy # expected energy of the whole system
sub r2 r1 r0 # current energy delta
max r2 r2 0
# calculate hot gas stats
move CurrentDevice 4 # d4
push r2 # preserve r2 (delta)
jal CalculateHeatCapacity
pop r2
l r1 HotPipe Temperature
l r3 HotPipe Pressure
mul r0 r0 r1 # energy in 1 mole of the hot gas
div r0 r2 r0 # needed amount of hot moles to compensate current delta
# add energy (move hot moles using Volume Pump)
mul r0 r0 R
mul r0 r0 r1
div r0 r0 r3 # volume pump setting
div r0 r0 10 # throttle volume pump to avoid overshooting
s HotPump Setting r0
s db Setting r0 # debug
sgt r0 r0 0.0001
s HotPump On r0
yield
j Loop
CalculateHeatCapacity:
move r0 0 # result
l r2 CurrentDeviceRegister RatioCarbonDioxide
mul r2 r2 HEAT_CO2
add r0 r0 r2
l r2 CurrentDeviceRegister RatioNitrogen
mul r2 r2 HEAT_N
add r0 r0 r2
l r2 CurrentDeviceRegister RatioNitrousOxide
mul r2 r2 HEAT_N2O
add r0 r0 r2
l r2 CurrentDeviceRegister RatioOxygen
mul r2 r2 HEAT_O2
add r0 r0 r2
l r2 CurrentDeviceRegister RatioPollutant
mul r2 r2 HEAT_X
add r0 r0 r2
l r2 CurrentDeviceRegister RatioVolatiles
mul r2 r2 HEAT_H2
add r0 r0 r2 # r0 = heat capacity of 1 mole
j ra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment