diff --git a/water_sim.py b/water_sim.py index fba92a1..c8e636e 100644 --- a/water_sim.py +++ b/water_sim.py @@ -3,6 +3,11 @@ from game import * class WaterSim(Game): + alpha = 100000 + u = lambda Cm1,C,C1,dx: WaterSim.alpha * (Cm1+C1-2*C) * 0.5 * dx + v = lambda u,v,d,dt: ( v + (u - d * v) * dt ) + w = lambda w,v,dt: w + v * dt + def __init__(self,N): self.N = N self.init_data(N//2) @@ -26,19 +31,27 @@ class WaterSim(Game): def on_update(self, dtime): C2 = [0] * self.N - C2[0] = 100 * (self.C[1] - self.C[0]) * 100/self.N - C2[-1] = 100 * (self.C[-2] - self.C[-1]) * 100/self.N + C2[0] = WaterSim.u(self.C[1] , self.C[0] , self.C[1] , 1.0/self.N) + C2[-1] = WaterSim.u(self.C[-2] , self.C[-1] , self.C[-2] , 1.0/self.N) + #C2[0] = 100 * (self.C[1] - self.C[0]) * 100/self.N + #C2[-1] = 100 * (self.C[-2] - self.C[-1]) * 100/self.N for i in range(1,self.N-1): - C2[i] = 1000 * (self.C[i+1] + self.C[i-1] - 2*self.C[i]) * 0.5 * 100 /(self.N) + C2[i] = WaterSim.u(self.C[i-1],self.C[i], self.C[i+1], 1.0/self.N) + #C2[i] = 1000 * (self.C[i+1] + self.C[i-1] - 2*self.C[i]) * 0.5 * 100 /(self.N) for i in range(0,self.N): - self.C1[i] = (self.C1[i] + C2[i] * dtime) if i == 0 or i == self.N-1: - self.C1[i] *= 0.98 + d = 10 + #d = 0.95 else: - self.C1[i] *= 0.9999 - self.C[i] = ( self.C[i] + self.C1[i] * dtime ) + d = 0.1 + #d = 0.9999 + #self.C1[i] = d * (self.C1[i] + C2[i] * dtime) + self.C1[i] = WaterSim.v(C2[i], self.C1[i], d, dtime) + #self.C[i] = ( self.C[i] + self.C1[i] * dtime ) + self.C[i] = WaterSim.w(self.C[i], self.C1[i], dtime) + if self.C[i] < 0: self.C[i] = 0