def run(problem_number=1, params={}): """ Allows to run phaml examples with various parameters. problem_number ... which example to run params ... solver parameters (refinement strategy, error tolerance, ...) Examples: >>> from qsnake.examples.phaml_simple import run >>> import phaml >>> run(1, params = { "term_energy_err": 1e-6, "hp_strategy": phaml.HP_SMOOTH_PRED, }) >>> run(2, params = { "term_energy_err": 1e-4, "hp_strategy": phaml.HP_SMOOTH_PRED, }) >>> run(2, params = { "term_energy_err": 1e-4, "hp_strategy": phaml.HP_PRIOR2P_H1, }) >>> run(2, params = { "term_energy_err": 1e-4, "hp_strategy": phaml.HP_REFSOLN_ELEM, }) """ current_dir = os.path.dirname(os.path.abspath(__file__)) domain_file = os.path.join(current_dir, "data", "domain") p = Phaml(domain_file, problem_number) p.solve(params) x, y, elems, orders = p.get_mesh() nodes = zip(x, y) # Phaml counts nodes from 1, but we count from 0: elems = elems - 1 mesh = Mesh(nodes, elems, [], [], orders=orders) sol = Solution(mesh) # Refine mesh for plotting purposes. x_plot, y_plot, elems_plot = sol.create_plotting_mesh() # Get solution at vertex values of the plotting mesh from Phaml. values_plot = p.get_solution_values(x_plot, y_plot) # Set the values for plotting back into the Solution sol.set_values(x_plot, y_plot, elems_plot, values_plot) # Return it to the user, so that he/she can decide what to do with it. To # plot it, just do sol.plot(). return sol
def run(problem_number=1, params={}): """ Allows to run phaml examples with various parameters. problem_number ... which example to run params ... solver parameters (refinement strategy, error tolerance, ...) Examples: >>> from femhub.examples.phaml_simple import run >>> import phaml >>> run(1, params = { "term_energy_err": 1e-6, "hp_strategy": phaml.HP_SMOOTH_PRED, }) >>> run(2, params = { "term_energy_err": 1e-4, "hp_strategy": phaml.HP_SMOOTH_PRED, }) >>> run(2, params = { "term_energy_err": 1e-4, "hp_strategy": phaml.HP_PRIOR2P_H1, }) >>> run(2, params = { "term_energy_err": 1e-4, "hp_strategy": phaml.HP_REFSOLN_ELEM, }) """ current_dir = os.path.dirname(os.path.abspath(__file__)) domain_file = os.path.join(current_dir, "data", "domain") p = Phaml(domain_file, problem_number) p.solve(params) x, y, elems, orders = p.get_mesh() nodes = zip(x, y) # Phaml counts nodes from 1, but we count from 0: elems = elems - 1 mesh = Mesh(nodes, elems, [], [], orders=orders) sol = Solution(mesh) # get xy points x, y = sol.get_xy_points() # get values in those points from Phaml values = p.get_solution_values(x, y) sol.set_values(values) return sol
""" Python version of the example "simple". """ import os from numpy import array from phaml import Phaml from phaml.plot import plot_mesh_mpl, plot_sln_mayavi, convert_mesh problem_number = 1 current_dir = os.path.dirname(os.path.abspath(__file__)) domain_file = os.path.join(current_dir, "domain") p = Phaml(domain_file, problem_number) p.solve() mesh_data = p.get_mesh() print "Saving the mesh to 'mesh.png'..." polygons, orders = convert_mesh(*mesh_data) import matplotlib matplotlib.use("Agg") f = plot_mesh_mpl(polygons, orders) f.savefig("mesh.png") print "Saving the solution to 'sln.png'..." x, y, mesh, _ = mesh_data values = p.get_solution_values(x, y) mesh = [elem-1 for elem in mesh] f = plot_sln_mayavi(x, y, mesh, values) f.savefig("sln.png")