我写了这段代码来计算一个叫做B因子的东西。一切都很好。您输入压力和温度,B因子出来。但是,当我输入压力和温度的新值时 - 应用程序似乎冻结了。
我的代码如下:
import Foundation
class bFactorFormula {
var NK_VALUES:[Double] = [0.924803575275, -0.492448489428, 0.661883336938, -1.92902649201, -0.0622469309629, 0.349943957581, 0.564857472498, -1.61720005987, -0.481395031883, 0.421150636384, -0.0161962230825, 0.172100994165, 0.00735448924933, 0.0168077305479, -0.00107626664179, -0.0137318088513, 0.000635466899859, 0.00304432279419, -0.0435762336045, -0.0723174889316, 0.0389644315272, -0.021220136391, 0.00408822981509, -0.0000551990017984, -0.0462016716479, -0.00300311716011, 0.0368825891208, -0.0025585684622, 0.00896915264558, -0.0044151337035, 0.00133722924858, 0.000264832491957, 19.6688194015, -20.915560073, 0.0167788306989, 2627.67566274]
var IK_VALUES:[Double] = [1, 1, 2, 2, 3, 3, 1, 1, 1, 3, 3, 4, 6, 6, 7, 7, 8, 8, 1, 2, 3, 4, 5, 8, 4, 5, 5, 8, 3, 5, 6, 9, 1, 1, 3, 2]
var JK_VALUES:[Double] = [0.25, 0.875, 0.5, 0.875, 0.375, 0.75, 0.5, 0.75, 2, 1.25, 3.5, 1, 0.5, 3, 0, 2.75, 0.75, 2.5, 4, 6, 6, 3, 3, 6, 16, 11, 15, 12, 12, 7, 4, 16, 0, 1, 2, 3]
var LK_VALUES:[Double] = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 0, 0, 0, 0]
var PK_VALUES:[Double] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 20, 15, 25]
var BK_VALUES:[Double] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0325, 325, 300, 275]
var UK_VALUES:[Double] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.16, 1.16, 1.13, 1.25]
var R_CONST = 8.31451
var T_C:Double = 126.192
var P_C:Double = 3.3958
var MOLECULAR_WEIGHT:Double = 28.01348
var DENSITY_STP:Double = 1.1851
var RHO_C:Double = 11.1839
// DECLARE INPUT VARIABLES
var n2TemperatureInput:Double
var n2PressureInput: Double
var tempK:Double {
get {
return n2TemperatureInput + 273.15
}
}
var tempR:Double {
get {
return T_C / tempK
}
}
var pTarget:Double {
get {
return (n2PressureInput * 1000000) + 101325
}
}
init (setBFactorTemperature:Double, setBFactorPressure:Double) {
self.n2TemperatureInput = setBFactorTemperature
self.n2PressureInput = setBFactorPressure
}
var rhoCurrent:Double = 0
var rhoHigh:Double = 0
var rhoLow:Double = 0
var diffAggregate:Double = 0
var diffSegment:Double = 0
var iFactor:Double = 0
var bFactor:Double = 0
//
// kIndex defined in the while-loop
// var kIndex:Double = 0
//
var pCalc:Double = 0
var zDiff:Double = 0
var rhoR:Double = 0
func retRhoCurrent() -> Double {
while abs(pTarget - pCalc) > 1 {
//
// SET rhoCurrent
//
if (pTarget > pCalc) {
rhoLow = rhoCurrent
if(rhoCurrent >= rhoHigh) {
rhoCurrent += RHO_C
rhoHigh = rhoCurrent
} else {
rhoCurrent += ((rhoHigh - rhoCurrent)/2)
}
} else {
rhoHigh = rhoCurrent
rhoCurrent += ((rhoLow - rhoCurrent)/2)
}
//
// SET rhoR
//
rhoR = rhoCurrent / RHO_C
//
// SET DIFFERENTIAL
//
diffAggregate = 0
for var kIndex = 0; kIndex < NK_VALUES.count; ++kIndex {
diffSegment = NK_VALUES[kIndex]
diffSegment *= pow(rhoR, IK_VALUES[kIndex])
diffSegment *= pow(tempR, JK_VALUES[kIndex])
iFactor = 0
if LK_VALUES[kIndex] > 0 {
diffSegment *= exp(-1 * pow(rhoR, LK_VALUES[kIndex]))
iFactor = LK_VALUES[kIndex] * pow(rhoR, LK_VALUES[kIndex])
}
if PK_VALUES[kIndex] > 0 {
diffSegment *= exp(-1 * PK_VALUES[kIndex] * pow(rhoR, 2) - BK_VALUES[kIndex] * pow(tempR - UK_VALUES[kIndex], 2))
iFactor = 2 * rhoR * PK_VALUES[kIndex] * (rhoR - 1)
}
diffAggregate += (diffSegment * (IK_VALUES[kIndex] - iFactor))
}
//
// SET pCalc
//
zDiff = 1 + diffAggregate
pCalc = zDiff * R_CONST * 1000 * tempK * rhoCurrent
}
return rhoCurrent
}
}
我在想我可能需要某种“刷新”命令,但如果这是正确的话,我不知道如何走这条路。
另外 - 我想向你们展示我的ViewController,看看我是如何做到这一点来执行它的动作。
import UIKit
extension Double {
func format(f: String) -> String {
return NSString(format: "%\(f)f", self) as String
}
}
class NitrogenViewController: UIViewController {
@IBOutlet weak var bFactorPressureInput: UITextField!
@IBOutlet weak var bFactorTemperatureInput: UITextField!
@IBOutlet weak var bFactorResults: UILabel!
var calcBFactor = bFactorFormula(setBFactorTemperature: 10, setBFactorPressure: 10)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func bFactorButton(sender: AnyObject) {
calcBFactor.n2PressureInput = Double((bFactorPressureInput.text as NSString).doubleValue)
calcBFactor.n2TemperatureInput = Double((bFactorTemperatureInput.text as NSString).doubleValue)
var rhoCurrentReturned = calcBFactor.retRhoCurrent()
var bFactorResult = rhoCurrentReturned * (28.01348/1.1851)
var bFactorResultFormat = "0.2"
bFactorResults.text = "The B-Factor is \(bFactorResult.format(bFactorResultFormat)) scm/m3"
}
我在想它与我的视图控制器代码有关。有什么想法吗?
答案 0 :(得分:1)
初始化bFactorFormula
类时,看起来正在将pCalc初始化为0。没关系。但是在第一次运行之后pCalc是10101321.0261427并且再次运行计算时,你永远不会超出while
循环。
因此,您可以为IBAction代码中的每次运行创建一个新的bFactorForumula对象,或者您可以在retRhoCurrent
的顶部设置pCalc = 0。