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 """ Apply boundary conditions. """ if n==1: solver.assembleDirichletBC(S, solver.parseArgToBoundaries([grid.findBoundaryByMarker(1), 1], grid), rhs) else: solver.assembleDirichletBC(S, solver.parseArgToBoundaries([grid.findBoundaryByMarker(1), 0], grid), rhs) solver.assembleDirichletBC(S, solver.parseArgToBoundaries([grid.findBoundaryByMarker(2), 0], grid), rhs) """ Solve for u """ u[n] = solver.linsolve(S, rhs)
# 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, # rhs=b, # boundArgs=dirichletBC, # assembler=solver.assembleDirichletBC) solve = pg.LinSolver(S) solve.solve(b, ut)
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) solve.solve(b, ut) u[n, :] = ut plt.plot(times, u[:, probeID], label='Explicit Euler') theta = 1