I have a few swift codes to share that others may want I was trying to compile swift playgrounds programs on my iPhone I am interested in making an App for it possibly it has been a long time since I used a MAC First Code…Swift Compiler import subprocess
def compile_swift_code(code): try: # Write the Swift code to a temporary file with open('temp.swift', 'w') as file: file.write(code)
# Compile the Swift code using the subprocess module
subprocess.run(['swiftc', '-o', 'output', 'temp.swift'], check=True)
# Execute the compiled program and capture the output
completed_process = subprocess.run(['./output'], capture_output=True, text=True)
output = completed_process.stdout
return output.strip()
except subprocess.CalledProcessError as e:
print("Compilation error:", e)
return None
Example usage
swift_code = ''' print("Hello, world!") ''' output = compile_swift_code(swift_code) if output is not None: print(output)
Second Code example program that simulates the expansion of the universe from the Big Bang to the present day using N-body simulations and the Friedmann equations in swift playgrounds code
import UIKit import SceneKit import PlaygroundSupport import simd
// Constants let c: Double = 299792.458 // speed of light in km/s let H0: Double = 70 // Hubble constant in km/s/Mpc let G: Double = 6.6743e-11 // gravitational constant in m^3/kg/s^2 let rho_crit: Double = 1.878e-26 // critical density of the universe in kg/m^3
// Parameters let N: Int = 1000 // number of particles let t0: Double = 0 // initial time in Gyr let tf: Double = 13.8 // final time in Gyr let dt: Double = 0.01 // time step in Gyr let a0: Double = 1 / (1 + t0 * H0 / 2997.92) // initial scale factor
// Generate initial conditions var x = (0..<N).map { _ in return simd_double3(Double.random(in: -10..<10), Double.random(in: -10..<10), Double.random(in: -10..<10)) } var v = (0..<N).map { _ in return simd_double3(Double.random(in: -100..<100), Double.random(in: -100..<100), Double.random(in: -100..<100)) } var m = (0..<N).map { _ in return Double.random(in: 1e11..<1.1e11) }
// Define Friedmann equations func HubbleParameter(a: Double) -> Double { return H0 * sqrt((rho_crit / 3) * (1 / pow(a, 3))) }
func Acceleration(x: [simd_double3], v: [simd_double3]) -> [simd_double3] { let r = x.map { return length($0) } var a = [simd_double3](repeating: simd_double3(0), count: N) for i in 0..<N { a[i] = -G * (x[i] / pow(r[i], 3)) * m a[i] -= HubbleParameter(a: 1) * x[i] } return a }
// Initialize arrays for storing data var t = stride(from: t0, to: tf, by: dt).map { return $0 } var a = [Double](repeating: 0, count: t.count) var v = [simd_double3](repeating: simd_double3(0), count: N) var x = [simd_double3](repeating: simd_double3(0), count: N)
// Set initial conditions a[0] = a0 v[0] = v[0] x[0] = x[0]
// Integrate equations of motion using Verlet algorithm for i in 1..<t.count { x[i] = x[i-1] + v[i-1] * dt + 0.5 * Acceleration(x: x, v: v)[i-1] * dt * dt a[i] = a0 * (1 + HubbleParameter(a: a0) * (t[i] - t0)) // scale factor as a function of time v[i] = v[i-1] + 0.5 * (Acceleration(x: x, v: v)[i] + Acceleration(x: x, v: v)[i-1]) * dt }
// Plot results
I have a few more codes and some new engine/motor designs