Greetings!
I'm new to swift programming and new to the Mac world. I developed programs for scientific computing in the past with FORTRAN, C++ and python but I'd like to learn swift because I think it is very flexible language. So here is my first program which is the solution of the linear convection using finite difference methods one of the fundamental equations in computational science. The array u could be velocity or temperature or something else and the u.replaceSubrange should apply initial conditions within a range in the u vector or 1D array. But this is wage since the size of u changes if I change the constant nx for example.
So here are the questions: Is there a better way working with replaceSubrange ?
Is there a better way to write these lines in swift?
any comment or idea or suggestion is appreciated.
Thank you in advance!
import Foundation
let nx: Int = 41
let nt: Int = 25
let dt: Double = 0.025
let c: Double = 1.0
var dx: Double
dx = 2.0 / Double(nx - 1)
var u: ArrayFloat = Array(repeating: 1.0, count: nx)
var un = u
u.replaceSubrange(Int(0.5/dx)...Int(1/dx+1), with: repeatElement(2.0, count: 12))
for _ in 1...nt {
un = u
for i in 1..nx {
u[i] = un[i] - Float(c * dt / dx) * (un[i] - un[i-1])
}
}
print(u)
Selecting any option will automatically load the page