/
coupled1D.py
46 lines (25 loc) · 964 Bytes
/
coupled1D.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from fipy import Grid1D, CellVariable, TransientTerm, DiffusionTerm, Viewer
from simulation import Simulation
class CoupledSimulation(Simulation):
def setup(self, ncell):
m = Grid1D(nx=ncell, Lx=1.)
v = CellVariable(mesh=m, hasOld=True, value=[[0.5], [0.5]], elementshape=(2,))
v.constrain([[0], [1]], m.facesLeft)
v.constrain([[1], [0]], m.facesRight)
eqn = TransientTerm([[1,0], [0,1]]) == DiffusionTerm([[[0.01, -1], [1, 0.01]]])
self.v = v
self.eqn = eqn
for step in range(2):
self.time_step()
def time_step(self):
self.v.updateOld()
self.eqn.solve(var=self.v, dt=1.e-3)
def run(self, ncell=100):
self.setup(ncell)
self.continue_steps()
def func(ncell):
coupled = CoupledSimulation()
coupled.run(ncell)
if __name__ == '__main__':
coupled = CoupledSimulation()
coupled.run()