#grid = grid.createP2() times = np.arange(0, 2., (h)*0.5) print(grid) dirichletBC = [[1, 0], # top [2, 0]] #bottom u = np.zeros((len(times), grid.nodeCount())) v = np.zeros((len(times), grid.nodeCount())) k = times[1]-times[0] u[0,0] = 0.0 v[0,0] = 0.0 e = np.zeros(len(times)) A = solver.createStiffnessMatrix(grid) M = solver.createMassMatrix(grid) F = solver.assembleForceVector(grid, 0) theta = 0.5 for n in range(1,len(times)): """ DRY. so wen can create temporary array for the force vector and the repeating part """ tmpRhs = ((1.0-theta) * (A*u[n-1] - F) - theta * F) rhs = M * u[n-1] + k * M * v[n-1] - k*k * theta * tmpRhs """ Create the system matrix """ S = M + A * k*k * theta*theta
def uAna(t, x): return np.exp(-np.pi**2. * t) * np.sin(np.pi * x) plt.plot(times, uAna(times, grid.node(probeID).pos()[0]), label='Analytical') # u = solvePoisson(grid, times=times, theta=0.0, # u0=lambda r: np.sin(np.pi * r[0]), # uBoundary=dirichletBC) dof = grid.nodeCount() u = np.zeros((len(times), dof)) u[0, :] = list(map(lambda r: np.sin(np.pi * r[0]), grid.positions())) dt = times[1] - times[0] A = solver.createStiffnessMatrix(grid, np.ones(grid.cellCount())) M = solver.createMassMatrix(grid, np.ones(grid.cellCount())) ut = pg.RVector(dof, 0.0) rhs = pg.RVector(dof, 0.0) b = pg.RVector(dof, 0.0) theta = 0 boundUdir = solver.parseArgToBoundaries(dirichletBC, grid) for n in range(1, len(times)): b = (M - A * dt) * u[n - 1] + rhs * dt S = M solver.assembleDirichletBC(S, boundUdir, rhs=b) # solver.assembleBoundaryConditions(grid, S,
# def uAna(t, x): return np.exp(-np.pi**2. * t) * np.sin(np.pi * x) plt.plot(times, uAna(times, grid.node(probeID).pos()[0]), label='Analytical') dof = grid.nodeCount() u = np.zeros((len(times), dof)) u[0, :] = list(map(lambda r: np.sin(np.pi * r[0]), grid.positions())) dt = times[1] - times[0] A = solver.createStiffnessMatrix(grid, np.ones(grid.cellCount())) M = solver.createMassMatrix(grid, np.ones(grid.cellCount())) ut = pg.RVector(dof, 0.0) rhs = pg.RVector(dof, 0.0) b = pg.RVector(dof, 0.0) theta = 0 boundUdir = solver.parseArgToBoundaries(dirichletBC, grid) for n in range(1, len(times)): b = (M - A * dt) * u[n - 1] + rhs * dt S = M solver.assembleDirichletBC(S, boundUdir, rhs=b) solve = pg.LinSolver(S)