def test_ScalarView_mpl_default(): mesh = Mesh() mesh.load(domain_mesh) mesh.refine_element(0) shapeset = H1Shapeset() pss = PrecalcShapeset(shapeset) # create an H1 space space = H1Space(mesh, shapeset) space.set_uniform_order(5) space.assign_dofs() # initialize the discrete problem wf = WeakForm(1) set_forms(wf) solver = DummySolver() sys = LinSystem(wf, solver) sys.set_spaces(space) sys.set_pss(pss) # assemble the stiffness matrix and solve the system sys.assemble() A = sys.get_matrix() b = sys.get_rhs() from scipy.sparse.linalg import cg x, res = cg(A, b) sln = Solution() sln.set_fe_solution(space, pss, x) view = ScalarView("Solution") view.show(sln, show=False, method="contour")
def test_ScalarView_mpl_default(): mesh = Mesh() mesh.load(domain_mesh) mesh.refine_element(0) shapeset = H1Shapeset() pss = PrecalcShapeset(shapeset) # create an H1 space space = H1Space(mesh, shapeset) space.set_uniform_order(5) space.assign_dofs() # initialize the discrete problem wf = WeakForm(1) set_forms(wf) solver = DummySolver() sys = LinSystem(wf, solver) sys.set_spaces(space) sys.set_pss(pss) # assemble the stiffness matrix and solve the system sys.assemble() A = sys.get_matrix() b = sys.get_rhs() from scipy.sparse.linalg import cg x, res = cg(A, b) sln = Solution() sln.set_fe_solution(space, pss, x) view = ScalarView("Solution") view.show(sln, show=False, method="contour")
ls = LinSystem(wf) ls.set_spaces(space) # Visualisation sview = ScalarView("Temperature", 0, 0, 450, 600) #title = "Time %s, exterior temperature %s" % (TIME, temp_ext(TIME)) #Tview.set_min_max_range(0,20); #Tview.set_title(title); #Tview.fix_scale_width(3); # Time stepping nsteps = int(FINAL_TIME / TAU + 0.5) rhsonly = False for n in range(1, nsteps + 1): print("\n---- Time %s, time step %s, ext_temp %s ----------" % (TIME, n, temp_ext(TIME))) # Assemble and solve ls.assemble() rhsonly = True ls.solve_system(tsln, lib="scipy") # Shifting the time variable TIME += TAU update_time(TIME) # Visualization of solution title = "Time %s, exterior temperature %s" % (TIME, temp_ext(TIME)) sview.show(tsln)
xdisp.set_uniform_order(8) ydisp.set_uniform_order(8) set_bc(xdisp, ydisp) ndofs = xdisp.assign_dofs(0) ndofs += ydisp.assign_dofs(ndofs) # initialize the discrete problem wf = WeakForm(2) set_forms(wf) solver = DummySolver() sys = LinSystem(wf, solver) sys.set_spaces(xdisp, ydisp) sys.set_pss(pss) xsln = Solution() ysln = Solution() sys.assemble() sys.solve_system(xsln, ysln) view = ScalarView("Von Mises stress [Pa]", 50, 50, 1200, 600) E = float(200e9) nu = 0.3 stress = VonMisesFilter(xsln, ysln, E / (2*(1 + nu)), (E * nu) / ((1 + nu) * (1 - 2*nu))) view.show(stress) view.wait()
def calc(threshold=0.3, strategy=0, h_only=False, error_tol=1, interactive_plotting=False, show_mesh=False, show_graph=True): mesh = Mesh() mesh.create([ [0, 0], [1, 0], [1, 1], [0, 1], ], [ [2, 3, 0, 1, 0], ], [ [0, 1, 1], [1, 2, 1], [2, 3, 1], [3, 0, 1], ], []) mesh.refine_all_elements() shapeset = H1Shapeset() pss = PrecalcShapeset(shapeset) space = H1Space(mesh, shapeset) set_bc(space) space.set_uniform_order(1) wf = WeakForm(1) set_forms(wf) sln = Solution() rsln = Solution() solver = DummySolver() selector = H1ProjBasedSelector(CandList.HP_ANISO, 1.0, -1, shapeset) view = ScalarView("Solution") iter = 0 graph = [] while 1: space.assign_dofs() sys = LinSystem(wf, solver) sys.set_spaces(space) sys.set_pss(pss) sys.assemble() sys.solve_system(sln) dofs = sys.get_matrix().shape[0] if interactive_plotting: view.show(sln, lib=lib, notebook=True, filename="a%02d.png" % iter) rsys = RefSystem(sys) rsys.assemble() rsys.solve_system(rsln) hp = H1Adapt([space]) hp.set_solutions([sln], [rsln]) err_est = hp.calc_error() * 100 err_est = hp.calc_error(sln, rsln) * 100 print "iter=%02d, err_est=%5.2f%%, DOFS=%d" % (iter, err_est, dofs) graph.append([dofs, err_est]) if err_est < error_tol: break hp.adapt(selector, threshold, strategy) iter += 1 if not interactive_plotting: view.show(sln, lib=lib, notebook=True) if show_mesh: mview = MeshView("Mesh") mview.show(mesh, lib="mpl", notebook=True, filename="b.png") if show_graph: from numpy import array graph = array(graph) import pylab pylab.clf() pylab.plot(graph[:, 0], graph[:, 1], "ko", label="error estimate") pylab.plot(graph[:, 0], graph[:, 1], "k-") pylab.title("Error Convergence for the Inner Layer Problem") pylab.legend() pylab.xlabel("Degrees of Freedom") pylab.ylabel("Error [%]") pylab.yscale("log") pylab.grid() pylab.savefig("graph.png")
sln = Solution() rsln = Solution() solver = DummySolver() view = ScalarView("Solution") iter = 0 while 1: space.assign_dofs() sys = LinSystem(wf, solver) sys.set_spaces(space) sys.set_pss(pss) sys.assemble() sys.solve_system(sln) if interactive_plotting: view.show(sln) rsys = RefSystem(sys) rsys.assemble() rsys.solve_system(rsln) hp = H1OrthoHP(space) error = hp.calc_error(sln, rsln)*100 print "iter=%02d, error=%5.2f%%" % (iter, error) if error < error_tol: break hp.adapt(threshold, strategy, h_only) iter += 1
# initialize the discrete problem wf = WeakForm(3) set_forms(wf, xprev, yprev) # visualize the solution vview = VectorView("velocity [m/s]", 0, 0, 1200, 350) pview = ScalarView("pressure [Pa]", 0, 500, 1200, 350) vview.set_min_max_range(0, 1.9) vview.show_scale(False) pview.show_scale(False) pview.show_mesh(False) solver = DummySolver() sys = LinSystem(wf, solver) sys.set_spaces(xvel, yvel, press) sys.set_pss(pss) #dp.set_external_fns(xprev, yprev) EPS_LOW = 0.0014 for i in range(1000): print "*** Iteration %d ***" % i psln = Solution() sys.assemble() sys.solve_system(xprev, yprev, psln) vview.show(xprev, yprev, 2*EPS_LOW) pview.show(psln) vview.wait()
# Create the x- and y- displacement space using the default H1 shapeset xdisp = H1Space(mesh, P_INIT) ydisp = H1Space(mesh, P_INIT) set_bc(xdisp, ydisp) # Initialize the weak formulation wf = WeakForm(2) set_forms(wf) # Initialize the linear system. ls = LinSystem(wf) ls.set_spaces(xdisp, ydisp) # Assemble and solve the matrix problem xsln = Solution() ysln = Solution() ls.assemble() ls.solve_system(xsln, ysln, lib="scipy") # Visualize the solution view = ScalarView("Von Mises stress [Pa]", 50, 50, 1200, 600) E = float(200e9) nu = 0.3 l = (E * nu) / ((1 + nu) * (1 - 2 * nu)) mu = E / (2 * (1 + nu)) stress = VonMisesFilter(xsln, ysln, mu, l) view.show(stress) # Visualize the mesh mesh.plot(space=xdisp)
mview = MeshView("Mesh") graph = [] iter = 0 print "Calculating..." while 1: space.assign_dofs() sys = LinSystem(wf, solver) sys.set_spaces(space) sys.set_pss(pss) sys.assemble() sys.solve_system(sln) dofs = sys.get_matrix().shape[0] if interactive_plotting: view.show(sln, lib="mayavi", filename="a%02d.png" % iter) if show_mesh: mview.show(mesh, lib="mpl", method="orders", filename="b%02d.png" % iter) rsys = RefSystem(sys) rsys.assemble() rsys.solve_system(rsln) hp = H1OrthoHP(space) error_est = hp.calc_error(sln, rsln) * 100 print "iter=%02d, error_est=%5.2f%%, DOFS=%d" % (iter, error_est, dofs) graph.append([dofs, error_est]) if error_est < error_tol: break hp.adapt(threshold, strategy, h_only)
ls = LinSystem(wf, solver) ls.set_spaces(space) ls.set_pss(pss) # Visualisation sview = ScalarView("Temperature", 0, 0, 450, 600) #title = "Time %s, exterior temperature %s" % (TIME, temp_ext(TIME)) #Tview.set_min_max_range(0,20); #Tview.set_title(title); #Tview.fix_scale_width(3); # Time stepping nsteps = int(FINAL_TIME/TAU + 0.5) rhsonly = False; for n in range(1,nsteps+1): print ("\n---- Time %s, time step %s, ext_temp %s ----------" % (TIME, n, temp_ext(TIME)) ) # Assemble and solve ls.assemble() rhsonly = True ls.solve_system(tsln, lib="scipy") # Shifting the time variable TIME += TAU update_time(TIME) # Visualization of solution title = "Time %s, exterior temperature %s" % (TIME, temp_ext(TIME)) sview.show(tsln, lib="mayavi")
def show_sol(s): view = ScalarView("Eigenvector", 0, 0, 400, 400) view.show(s)
# create an H1 space space = H1Space(mesh, shapeset) space.set_uniform_order(5) space.assign_dofs() # initialize the discrete problem wf = WeakForm(1) set_forms(wf) solver = DummySolver() sys = LinSystem(wf, solver) sys.set_spaces(space) sys.set_pss(pss) # assemble the stiffness matrix and solve the system sys.assemble() A = sys.get_matrix() b = sys.get_rhs() from scipy.sparse.linalg import cg x, res = cg(A, b) sln = Solution() sln.set_fe_solution(space, pss, x) view = ScalarView("Solution") view.show(sln, lib="mayavi") # view.wait() mview = MeshView("Hello world!", 100, 100, 500, 500) mview.show(mesh, lib="mpl", method="orders", notebook=False) mview.wait()
def plot(f): s = ScalarView("") s.show(f)
ndofs = xdisp.assign_dofs() ndofs += ydisp.assign_dofs(ndofs) print("xdof=%d, ydof=%d\n" % (xdisp.get_num_dofs(), ydisp.get_num_dofs()) ) # Solve the coarse mesh problem ls = LinSystem(wf, solver) ls.set_spaces(xdisp, ydisp) ls.set_pss(xpss, ypss) ls.assemble() ls.solve_system(x_sln_coarse, y_sln_coarse, lib="scipy") # View the solution -- this can be slow; for illustration only stress_coarse = VonMisesFilter(x_sln_coarse, y_sln_coarse, mu, lamda) #sview.set_min_max_range(0, 3e4) sview.show(stress_coarse, lib='mayavi') #xoview.show(xdisp, lib='mayavi') #yoview.show(ydisp, lib='mayavi') xomview.show(xmesh, space=xdisp, lib="mpl", method="orders", notebook=False) yomview.show(ymesh, space=ydisp, lib="mpl", method="orders", notebook=False) # Solve the fine mesh problem rs = RefSystem(ls) rs.assemble() rs.solve_system(x_sln_fine, y_sln_fine, lib="scipy") # Calculate element errors and total error estimate hp = H1OrthoHP(xdisp, ydisp) set_hp_forms(hp) err_est = hp.calc_error_2(x_sln_coarse, y_sln_coarse, x_sln_fine, y_sln_fine) * 100
ndofs += ydisp.assign_dofs(ndofs) # Initialize the weak formulation wf = WeakForm(2) set_forms(wf) # Initialize the linear system and solver solver = DummySolver() sys = LinSystem(wf, solver) sys.set_spaces(xdisp, ydisp) sys.set_pss(pss) # Assemble the stiffness matrix and solve the system xsln = Solution() ysln = Solution() sys.assemble() sys.solve_system(xsln, ysln, lib="scipy") # Visualize the solution view = ScalarView("Von Mises stress [Pa]", 50, 50, 1200, 600) E = float(200e9) nu = 0.3 l = (E * nu) / ((1 + nu) * (1 - 2*nu)) mu = E / (2*(1 + nu)) stress = VonMisesFilter(xsln, ysln, mu, l) view.show(stress, lib="mayavi") # Visualize the mesh mview = MeshView("Hello world!", 100, 100, 500, 500) mview.show(mesh, lib="mpl", method="orders", notebook=False)
def calc( threshold=0.3, strategy=0, h_only=False, error_tol=1, interactive_plotting=False, show_mesh=False, show_graph=True, ): mesh = Mesh() mesh.create( [[0, 0], [1, 0], [1, 1], [0, 1]], [[2, 3, 0, 1, 0]], [[0, 1, 1], [1, 2, 1], [2, 3, 1], [3, 0, 1]], [] ) mesh.refine_all_elements() shapeset = H1Shapeset() pss = PrecalcShapeset(shapeset) space = H1Space(mesh, shapeset) set_bc(space) space.set_uniform_order(1) wf = WeakForm(1) set_forms(wf) sln = Solution() rsln = Solution() solver = DummySolver() selector = H1ProjBasedSelector(CandList.HP_ANISO, 1.0, -1, shapeset) view = ScalarView("Solution") iter = 0 graph = [] while 1: space.assign_dofs() sys = LinSystem(wf, solver) sys.set_spaces(space) sys.set_pss(pss) sys.assemble() sys.solve_system(sln) dofs = sys.get_matrix().shape[0] if interactive_plotting: view.show(sln, lib=lib, notebook=True, filename="a%02d.png" % iter) rsys = RefSystem(sys) rsys.assemble() rsys.solve_system(rsln) hp = H1Adapt([space]) hp.set_solutions([sln], [rsln]) err_est = hp.calc_error() * 100 err_est = hp.calc_error(sln, rsln) * 100 print "iter=%02d, err_est=%5.2f%%, DOFS=%d" % (iter, err_est, dofs) graph.append([dofs, err_est]) if err_est < error_tol: break hp.adapt(selector, threshold, strategy) iter += 1 if not interactive_plotting: view.show(sln, lib=lib, notebook=True) if show_mesh: mview = MeshView("Mesh") mview.show(mesh, lib="mpl", notebook=True, filename="b.png") if show_graph: from numpy import array graph = array(graph) import pylab pylab.clf() pylab.plot(graph[:, 0], graph[:, 1], "ko", label="error estimate") pylab.plot(graph[:, 0], graph[:, 1], "k-") pylab.title("Error Convergence for the Inner Layer Problem") pylab.legend() pylab.xlabel("Degrees of Freedom") pylab.ylabel("Error [%]") pylab.yscale("log") pylab.grid() pylab.savefig("graph.png")
# Assemble and Solve the fine mesh problem rs = RefSystem(ls) rs.assemble() rs.solve_system(u_sln_fine, v_sln_fine, lib="scipy") # Either solve on coarse mesh or project the fine mesh solution # on the coarse mesh. if SOLVE_ON_COARSE_MESH: ls.assemble() ls.solve_system(u_sln_coarse, v_sln_coarse, lib="scipy") else: ls.project_global() # View the solution and meshes uview.show(u_sln_coarse) vview.show(v_sln_coarse) umesh.plot(space=uspace) vmesh.plot(space=vspace) # Calculate element errors and total error estimate hp = H1Adapt(ls) hp.set_solutions([u_sln_coarse, v_sln_coarse], [u_sln_fine, v_sln_fine]); set_hp_forms(hp) err_est = hp.calc_error() * 100 print("Error estimate: %s" % err_est) # If err_est too large, adapt the mesh if err_est < ERR_STOP: done = True
sln_fine = Solution() while(not done): print("\n---- Adaptivity step %d ---------------------------------------------\n" % it) it += 1 # Solve the coarse mesh problem ls = LinSystem(wf, solver) ls.set_spaces(space) ls.set_pss(pss) ls.assemble() ls.solve_system(sln_coarse) # View the solution sview.show(sln_coarse, lib='mayavi') # View the mesh mview = MeshView("Example 7", 100, 100, 500, 500) mview.show(mesh, lib="mpl", method="orders", notebook=False) # Solve the fine mesh problem rs = RefSystem(ls) rs.assemble() rs.solve_system(sln_fine) # Calculate element errors and total error estimate hp = H1OrthoHP(space); err_est = hp.calc_error(sln_coarse, sln_fine) * 100 print("Error estimate: %d" % err_est)
# Assemble and solve the fine mesh problem rs = RefSystem(ls) rs.assemble() rs.solve_system(sln_fine) # Either solve on coarse mesh or project the fine mesh solution # on the coarse mesh. if SOLVE_ON_COARSE_MESH: ls.assemble() ls.solve_system(sln_coarse) else: ls.project_global(sln_fine, sln_coarse) # View the solution and mesh sview.show(sln_coarse); mesh.plot(space=space) # Calculate error estimate wrt. fine mesh solution hp = H1Adapt(ls) hp.set_solutions([sln_coarse], [sln_fine]) err_est = hp.calc_error() * 100 print("Error estimate: %d" % err_est) # If err_est too large, adapt the mesh if (err_est < ERR_STOP): done = True else: done = hp.adapt(selector, THRESHOLD, STRATEGY, MESH_REGULARITY) if (ls.get_num_dofs() >= NDOF_STOP): done = True
# Assemble and solve the fine mesh problem rs = RefSystem(ls) rs.assemble() rs.solve_system(sln_fine) # Either solve on coarse mesh or project the fine mesh solution # on the coarse mesh. if SOLVE_ON_COARSE_MESH: ls.assemble() ls.solve_system(sln_coarse) else: ls.project_global(sln_fine, sln_coarse) # View the solution and mesh sview.show(sln_coarse); mesh.plot(space=space) # Calculate error estimate wrt. fine mesh solution hp = H1Adapt(ls) hp.set_solutions([sln_coarse], [sln_fine]) err_est = hp.calc_error() * 100 print("Error estimate: %d" % err_est) # If err_est too large, adapt the mesh if (err_est < ERR_STOP): done = True else: done = hp.adapt(selector, THRESHOLD, STRATEGY, MESH_REGULARITY) if (ls.get_num_dofs() >= NDOF_STOP): done = True
# Initialize the linear system. ls = LinSystem(wf) ls.set_spaces(space) # Visualisation sview = ScalarView("Temperature", 0, 0, 450, 600) #title = "Time %s, exterior temperature %s" % (TIME, temp_ext(TIME)) #Tview.set_min_max_range(0,20); #Tview.set_title(title); #Tview.fix_scale_width(3); # Time stepping nsteps = int(FINAL_TIME/TAU + 0.5) rhsonly = False; for n in range(1,nsteps+1): print ("\n---- Time %s, time step %s, ext_temp %s ----------" % (TIME, n, temp_ext(TIME)) ) # Assemble and solve ls.assemble() rhsonly = True ls.solve_system(tsln, lib="scipy") # Shifting the time variable TIME += TAU update_time(TIME) # Visualization of solution title = "Time %s, exterior temperature %s" % (TIME, temp_ext(TIME)) sview.show(tsln)