def test_plot_solution_salt_dome(): from pygeoiga.nurb.cad import make_salt_dome from pygeoiga.analysis.MultiPatch import patch_topology, form_k_IGA_mp, boundary_condition_mp from pygeoiga.analysis.common import solve from pygeoiga.plot.solution_mpl import p_temperature, p_temperature_mp from pygeoiga.analysis.MultiPatch import map_MP_elements geometry = make_salt_dome(refine=True, knot_ins= [np.arange(0.2,1,0.2), np.arange(0.2,1,0.2)]) geometry, gDoF = patch_topology(geometry) K_glob = np.zeros((gDoF, gDoF)) K_glob = form_k_IGA_mp(geometry, K_glob) D = np.zeros(gDoF) b = np.zeros(gDoF) T_t = 10 # [°C] T_b = 90 # [°C] T_l = None#10 T_r = None#40 bc, D = boundary_condition_mp(geometry, D, T_t, T_b, T_l, T_r) bc["gDOF"] = gDoF D, b = solve(bc, K_glob, b, D) geometry = map_MP_elements(geometry, D) from pygeoiga.plot.nrbplotting_mpl import p_surface, p_cpoints, p_knots, create_figure fig, ax = create_figure("2d") #ax.view_init(azim=270, elev=90) fig_sol, ax_sol = create_figure("2d") geometrypatch = make_salt_dome(refine=False) figpatch, axpatch = create_figure("2d") for patch_id in geometry.keys(): ax = p_surface(geometry[patch_id].get("knots"), geometry[patch_id].get("B"), ax=ax, dim=2, color=geometry[patch_id].get("color"), alpha=0.5) #ax = p_cpoints(geometry[patch_id].get("B"), ax=ax, dim=2, color="black", marker=".", point=True, line=False) ax = p_knots(geometry[patch_id].get("knots"), geometry[patch_id].get("B"), ax=ax, dim=2, point=False, line=True) axpatch = p_surface(geometrypatch[patch_id].get("knots"), geometrypatch[patch_id].get("B"), ax=axpatch, dim=2, color=geometrypatch[patch_id].get("color"), alpha=0.5) #axpatch = p_cpoints(geometry[patch_id].get("B"), ax=axpatch, dim=2, color="black", marker=".", point=True, line=False) axpatch = p_knots(geometrypatch[patch_id].get("knots"), geometrypatch[patch_id].get("B"), ax=axpatch, dim=2, point=False, line=True) #ax_sol = p_cpoints(geometry[patch_id].get("B"), ax=ax_sol, dim=2, color=geometry[patch_id].get("color"), marker=".", point=True, line=False) ax_sol = p_surface(geometry[patch_id].get("knots"), geometry[patch_id].get("B"), ax=ax_sol, dim=2, color="k", fill=False) x = geometry[patch_id].get("x_sol") y = geometry[patch_id].get("y_sol") t = geometry[patch_id].get("t_sol") ax_sol = p_temperature(x,y,t, vmin = np.min(D), vmax = np.max(D), levels=50, show=False, colorbar=True, ax=ax_sol, point = False, fill=True)#, color = "k") fig.show() fig_sol.show() figpatch.show() fig_all, ax_all = create_figure("2d") p_temperature_mp(geometry=geometry, vmin=np.min(D), vmax=np.max(D), levels=50, show=False, colorbar=True, ax=ax_all, point=False, fill=True, contour=False) plt.show()
def show_plots(): from pygeoiga.plot.nrbplotting_mpl import p_surface, p_cpoints, p_knots import matplotlib import matplotlib.pyplot as mpl color = None mpl.close("all") figa, axa = mpl.subplots() org = False if org: image = mpl.imread( "/home/danielsk78/GitProjects/master_thesis_IGA-Geothermal-Modeling/IGA/pygeoiga/nurb/data/salt_diapir.png" ) extent = [0, 6000, 0, 3000] axa.imshow(np.flipud(image), extent=extent, origin="lower", aspect="auto") major_ticks_x = np.arange(0, extent[1] + 1, 1000) major_ticks_y = np.arange(0, extent[3] + 1, 1000) minor_ticks_x = np.arange(0, extent[1] + 1, 200) minor_ticks_y = np.arange(0, extent[3] + 1, 200) axa.set_xticks(major_ticks_x) axa.set_xticks(minor_ticks_x, minor=True) axa.set_yticks(major_ticks_y) axa.set_yticks(minor_ticks_y, minor=True) axa.grid(which='both') for i, surf in enumerate(cpoints): col = color[i] if color is not None else np.random.rand(3, ) p_surface(knots[i], surf, ax=axa, dim=2, color=col, alpha=0.5) p_cpoints(surf, ax=axa, line=False, point=True, color=col, dim=2) p_knots(knots[i], surf, ax=axa, dim=2, point=False, color=col) mpl.show() color = [ "red", "red", "red", "blue", "blue", "blue", "brown", "blue", "brown", "yellow", "blue", "yellow", "gray", "blue", "gray", "green", "green", "green" ] figs, axs = mpl.subplots() for i, surf in enumerate(cpoints): col = color[i] if color is not None else np.random.rand(3, ) p_surface(knots[i], surf, ax=axs, dim=2, color=col) #p_cpoints(surf, ax=axs, line=False, point=True, color=col, dim=2) #p_knots(knots[i], surf, ax=axs, dim=2, point=False, color=col) mpl.show()
def test_plot_solution_mp_fault(): geometry = make_fault_model(refine=True) geometry, gDoF = patch_topology(geometry) K_glob = np.zeros((gDoF, gDoF)) K_glob = form_k_IGA_mp(geometry, K_glob) D = np.zeros(gDoF) b = np.zeros(gDoF) T_t = 10 # [°C] T_b = 40 # [°C] T_l = None T_r = None bc, D = boundary_condition_mp(geometry, D, T_t, T_b, T_l, T_r) bc["gDOF"] = gDoF D, b = solve(bc, K_glob, b, D) geometry = map_MP_elements(geometry, D) from pygeoiga.plot.nrbplotting_mpl import p_surface, p_cpoints, p_knots, create_figure fig, ax = create_figure("2d") fig_sol, ax_sol = create_figure("2d")# fig_point, ax_point = create_figure("2d") # for patch_id in geometry.keys(): ax = p_surface(geometry[patch_id].get("knots"), geometry[patch_id].get("B"), ax=ax, dim=2, color=geometry[patch_id].get("color"), alpha=0.5) ax = p_cpoints(geometry[patch_id].get("B"), ax=ax, dim=2, color="black", marker=".", point=True, line=False) ax = p_knots(geometry[patch_id].get("knots"), geometry[patch_id].get("B"), ax=ax, dim=2, point=False, line=True) #ax_sol = p_cpoints(geometry[patch_id].get("B"), ax=ax_sol, dim=2, color=geometry[patch_id].get("color"), marker=".", point=True, line=False) #ax_sol = p_knots(geometry[patch_id].get("knots"), geometry[patch_id].get("B"), # color = geometry[patch_id].get("color"), ax=ax_sol, dim=2, point=False, line=True) x = geometry[patch_id].get("x_sol") y = geometry[patch_id].get("y_sol") t = geometry[patch_id].get("t_sol") ax_sol = p_temperature(x,y,t, vmin = np.min(D), vmax = np.max(D), levels=50, show=False, colorbar=True, ax=ax_sol, point = False, fill=True, contour=False) ax_point = p_temperature(x, y, t, vmin=np.min(D), vmax=np.max(D), levels=100, show=False, colorbar=True, ax=ax_point, point=True, fill=False) ax_sol = p_surface(geometry[patch_id].get("knots"), geometry[patch_id].get("B"), ax=ax_sol, dim=2, color="k", alpha=1, fill=False, border=True) fig.show() fig_sol.show() fig_point.show() fig_all, ax_all = create_figure("2d") p_temperature_mp(geometry=geometry, vmin=np.min(D), vmax=np.max(D), levels=200, show=False, colorbar=True, ax=ax_all, point=False, fill=True, contour=False) fig_all.show()
def plot_surface(self, ax=None, **kwargs_surface): if ax is None: fig, ax = create_figure("2d", figsize=(10, 20)) ax = p_surface(self.knots, self.B, weight=self.weight, ax=ax, **kwargs_surface) return ax
def test_make_NURB_quarter_disk(): from pygeoiga.nurb.cad import quarter_disk knots, B = quarter_disk() from pygeoiga.plot.nrbplotting_mpl import create_figure, p_cpoints, p_knots, p_curve, p_surface fig, [ax2, ax] = plt.subplots(1, 2, sharey=True) ax = p_knots(knots, B, ax=ax, dim=2, point=False, line=True, color="k") ax = p_surface(knots, B, ax=ax, dim=2, color="blue", border=False, alpha=0.5) ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ax.set_xlabel("x") ax.set_aspect("equal") ax2 = p_cpoints(B, ax=ax2, dim=2, color="blue", linestyle="-", point=False, line=True) ax2 = p_cpoints(B, ax=ax2, dim=2, color="red", marker="o", point=True, line=False) n, m = B.shape[0], B.shape[1] P = np.asarray([(B[x, y, 0], B[x, y, 1]) for y in range(m) for x in range(n)]) for count, point in enumerate(P): ax2.annotate(str(count), point, fontsize=8, xytext=(3, 7), textcoords="offset points") ax2.spines["right"].set_visible(False) ax2.spines["top"].set_visible(False) ax2.set_xlabel("x") ax2.set_ylabel("y") ax2.set_aspect("equal") fig.show() save = False if save or save_all: fig.savefig(fig_folder + "NURBS_surface.pdf", **kwargs_savefig)
def plot(geometry, file_name): from pygeoiga.plot.nrbplotting_mpl import p_surface, p_cpoints, p_knots fig, [ax2, ax] = plt.subplots(1,2,figsize=(10,4),constrained_layout=True) #fig2, ax2 = plt.subplots(constrained_layout=True) for patch_id in geometry.keys(): ax = p_knots(geometry[patch_id].get("knots"), geometry[patch_id].get("B"), ax=ax, color='k', dim=2, point=False, line=True) print(patch_id, geometry[patch_id].get("knots")) ax = p_surface(geometry[patch_id].get("knots"), geometry[patch_id].get("B"), color=geometry[patch_id].get("color"), dim=2, fill=True, border=False, ax=ax) ax2 = p_cpoints(geometry[patch_id].get("B"), dim=2, ax=ax2, point=True, marker="o", color=geometry[patch_id].get("color"), linestyle="-", line=True) ax.legend(labels=list(geometry.keys()), handles=ax.patches, loc='upper left', bbox_to_anchor=(1., .5), borderaxespad=0) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.set_aspect("equal") ax.set_ylabel(r"$y$") ax.set_xlabel(r"$x$") ax2.spines['right'].set_visible(False) ax2.spines['top'].set_visible(False) ax2.set_aspect("equal") ax2.set_ylabel(r"$y$") ax2.set_xlabel(r"$x$") fig.show() save = False if save or save_all: fig.savefig(fig_folder + file_name, **kwargs_savefig)
def plot_surfaces(self, ax=None, **kwargs_surface): if ax is None: fig, ax = create_figure("2d", figsize=(10, 20)) for patch_name in tqdm(self.geometry.keys(), desc="Plotting surfaces"): ax = p_surface(self.geometry[patch_name].get("knots"), self.geometry[patch_name].get("B"), weight=self.geometry[patch_name].get("weight"), ax=ax, color=self.geometry[patch_name].get("color"), **kwargs_surface) return ax
def plot(geometry, file_name, c, l): from pygeoiga.plot.nrbplotting_mpl import p_surface, p_cpoints, p_knots, create_figure #fig, ax = create_figure("2d") fig, [ax, ax2] = plt.subplots(1, 2, figsize=(10, 3)) for patch_id in geometry.keys(): ax2 = p_surface(geometry[patch_id].get("knots"), geometry[patch_id].get("B"), ax=ax2, dim=2, color=geometry[patch_id].get("color"), alpha=0.5) ax = p_cpoints(geometry[patch_id].get("B"), ax=ax, dim=2, color=geometry[patch_id].get("color"), marker="o", linestyle="-", point=True, line=True) ax2 = p_knots(geometry[patch_id].get("knots"), geometry[patch_id].get("B"), ax=ax2, dim=2, point=False, line=True, color="k") ax.set_title("Control net") ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ax.set_xlabel("$x$") ax.set_ylabel("$y$") ax.set_aspect("equal") ax2.set_title("Physical space ($x,y$)") ax2.spines["right"].set_visible(False) ax2.spines["top"].set_visible(False) ax2.set_xlabel("$x$") ax2.set_ylabel("$y$") ax2.set_aspect("equal") if c: ax2.annotate("$\Omega_1$", (70, 80), fontsize=20) ax2.annotate("$\Omega_2$", (300, 80), fontsize=20) ax2.annotate("$\Omega_3$", (100, 240), fontsize=20) c = False fig.show() save = False if save or save_all: fig.savefig(fig_folder + file_name, **kwargs_savefig)
def test_IEN(): from pygeoiga.nurb.cad import make_surface_biquadratic knots, B = make_surface_biquadratic() from pygeoiga.plot.nrbplotting_mpl import create_figure, p_cpoints, p_knots, p_curve, p_surface fig, ax = plt.subplots() ax = p_knots(knots, B, ax=ax, dim=2, point=False, line=True, color="k") ax = p_surface(knots, B, ax=ax, dim=2, color="blue", border=False, alpha=0.5) ax = p_cpoints(B, ax=ax, dim=2, color="red", marker="o", point=True, line=False) n, m = B.shape[0], B.shape[1] P = np.asarray([(B[x, y, 0], B[x, y, 1]) for x in range(n) for y in range(m)]) for count, point in enumerate(P): ax.annotate(str(count), point, xytext=(5, 5), textcoords="offset points") ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ax.set_xlabel("$x$") ax.set_ylabel("$y$") ax.set_aspect(0.8) fig.show()
def test_make_NURB_biquadratic(): from pygeoiga.nurb.cad import make_surface_biquadratic knots, B = make_surface_biquadratic() from pygeoiga.plot.nrbplotting_mpl import create_figure, p_cpoints, p_knots, p_curve, p_surface fig, [ax2, ax] = plt.subplots(1, 2, sharey=True, figsize=(7, 3)) ax = p_knots(knots, B, ax=ax, dim=2, point=False, line=True, color="k") ax = p_surface(knots, B, ax=ax, dim=2, color="blue", border=False, alpha=0.5) ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ax.set_xlabel("x") #ax.annotate("1", (-1, 1), fontsize=20) #ax.annotate("2", (1.6, 3), fontsize=20) ax.set_aspect(0.8) ax2 = p_cpoints(B, ax=ax2, dim=2, color="blue", linestyle="-", point=False, line=True) ax2 = p_cpoints(B, ax=ax2, dim=2, color="red", marker="s", point=True, line=False) n, m = B.shape[0], B.shape[1] P = np.asarray([(B[x, y, 0], B[x, y, 1]) for x in range(n) for y in range(m)]) for count, point in enumerate(P): ax2.annotate("$P_{%s}$" % str(count), point, fontsize=8, xytext=(3, 7), textcoords="offset points") ax2.spines["right"].set_visible(False) ax2.spines["top"].set_visible(False) ax2.set_xlabel("x") ax2.set_ylabel("y") ax2.set_aspect(0.8) fig.show() from pygeoiga.engine.NURB_engine import basis_function_array_nurbs fig3 = plt.figure(constrained_layout=True) gs = fig3.add_gridspec(2, 2, hspace=0, wspace=0, width_ratios=[0.2, 1], height_ratios=[1, 0.2]) (ax_v, ax3), (no, ax_u) = gs.subplots(sharex=True, sharey=True) no.remove() N_spline_u, _ = basis_function_array_nurbs(knot_vector=knots[0], degree=2, resolution=100) N_spline_v, _ = basis_function_array_nurbs(knot_vector=knots[1], degree=2, resolution=100) resol = np.linspace(0, 1, 100) ax_u.plot(resol, N_spline_u) ax_u.spines["top"].set_visible(False) ax_u.spines["right"].set_visible(False) ax_u.set_xlim(0, 1) ax_u.set_ylim(0, 1) ax_v.plot(N_spline_v, resol) ax_v.spines["top"].set_visible(False) ax_v.spines["right"].set_visible(False) ax_v.set_yticks(knots[2:-2]) ax_v.set_xlim(1, 0) ax_v.set_ylim(0, 1) for i in knots[0]: ax3.vlines(i, 0, 1, 'k') for j in knots[1]: ax3.hlines(j, 0, 1, 'k') ax3.set_xlim(0, 1) ax3.set_ylim(0, 1) ax3.set_axis_off() ax_u.set_xlabel("$u$") ax_v.set_ylabel("$v$") ax_v.set_yticks(knots[1][2:-2]) ax_u.set_xticks(knots[0][2:-2]) for ax in ax_u, ax_v, ax3, no: ax.label_outer() fig3.show() save = True if save or save_all: fig.savefig(fig_folder + "B-spline_biquadratic.pdf", **kwargs_savefig) fig3.savefig(fig_folder + "B-spline_biquadratic_parameter.pdf", **kwargs_savefig)
def plot(B, knots, file_name): from pygeoiga.plot.nrbplotting_mpl import create_figure, p_cpoints, p_knots, p_curve, p_surface fig, [ax2, ax] = plt.subplots(1, 2, figsize=(10, 5), constrained_layout=True) ax = p_knots(knots, B, ax=ax, dim=2, point=False, line=True, color="k") ax = p_surface(knots, B, ax=ax, dim=2, color="blue", border=False, alpha=0.5) ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ax.set_xlabel("$x$") ax.set_ylabel("$y$") ax.set_aspect(0.8) ax2 = p_cpoints(B, ax=ax2, dim=2, color="blue", linestyle="-", point=False, line=True) ax2 = p_cpoints(B, ax=ax2, dim=2, color="red", marker="o", point=True, line=False) n, m = B.shape[0], B.shape[1] ax2.spines["right"].set_visible(False) ax2.spines["top"].set_visible(False) ax2.set_xlabel("$x$") ax2.set_ylabel("$y$", rotation=90) ax2.set_aspect(0.8) ax.set_title("Physical space ($x,y$)", fontsize=20) ax2.set_title("Control net", fontsize=20) fig.tight_layout(pad=0, h_pad=0, w_pad=0) fig.show() from pygeoiga.engine.NURB_engine import basis_function_array_nurbs fig3 = plt.figure(constrained_layout=True) gs = fig3.add_gridspec(2, 2, hspace=0, wspace=0, width_ratios=[0.2, 1], height_ratios=[1, 0.2]) (ax_v, ax3), (no, ax_u) = gs.subplots(sharex=True, sharey=True) no.remove() N_spline_u, _ = basis_function_array_nurbs(knot_vector=knots[0], degree=2, resolution=100) N_spline_v, _ = basis_function_array_nurbs(knot_vector=knots[1], degree=2, resolution=100) resol = np.linspace(0, 1, 100) ax_u.plot(resol, N_spline_u) ax_u.spines["top"].set_visible(False) ax_u.spines["right"].set_visible(False) ax_u.set_xlim(0, 1) ax_u.set_ylim(0, 1) ax_v.plot(N_spline_v, resol) ax_v.spines["top"].set_visible(False) ax_v.spines["right"].set_visible(False) ax_v.set_yticks(knots[2:-2]) ax_v.set_xlim(1, 0) ax_v.set_ylim(0, 1) for i in knots[0]: ax3.vlines(i, 0, 1, 'k') for j in knots[1]: ax3.hlines(j, 0, 1, 'k') ax3.set_xlim(0, 1) ax3.set_ylim(0, 1) ax3.set_axis_off() ax3.set_title("Parametric space ($u,v$)", fontsize=20) ax_u.set_xlabel("$u$") ax_v.set_ylabel("$v$") ax_v.set_yticks(knots[1][2:-2]) ax_u.set_xticks(knots[0][2:-2]) for ax in ax_u, ax_v, ax3, no: ax.label_outer() fig3.show() save = False if save or save_all: fig.savefig(fig_folder + file_name, **kwargs_savefig) fig3.savefig( fig_folder + file_name.split(".")[0] + "_parameter.pdf", **kwargs_savefig)
def test_image_example2(): from pygeoiga.nurb.cad import make_surface_biquadratic knots, B = make_surface_biquadratic() from pygeoiga.nurb.refinement import knot_insertion knot_ins_1 = [0.3, 0.6] knot_ins_0 = [0.25, 0.75] B, knots = knot_insertion(B, (2, 2), knots, knot_ins_0, direction=0) B, knots = knot_insertion(B, (2, 2), knots, knot_ins_1, direction=1) before = B from pygeoiga.plot.nrbplotting_mpl import create_figure, p_cpoints, p_knots, p_curve, p_surface from pygeoiga.engine.NURB_engine import basis_function_array_nurbs fig = plt.figure(constrained_layout=True) gs = fig.add_gridspec(3, 3, hspace=0, wspace=0, width_ratios=[0.2, 0.2, 1], height_ratios=[1, 0.2, 0.2]) (ax_bv, ax_v, ax2), (no1, no2, ax_u), (no3, no4, ax_bu) = gs.subplots(sharex=True, sharey=True) for no in no1, no2, no3, no4: no.remove() N_spline_u, _ = basis_function_array_nurbs(knot_vector=knots[0], degree=2, resolution=100) N_spline_v, _ = basis_function_array_nurbs(knot_vector=knots[1], degree=2, resolution=100) resol = np.linspace(0, 1, 100) ax_u.plot(resol, N_spline_u) ax_u.spines["top"].set_visible(False) ax_u.spines["right"].set_visible(False) ax_u.set_xlim(0, 1) ax_u.set_ylim(0, 1) ax_v.plot(N_spline_v, resol) ax_v.spines["top"].set_visible(False) ax_v.spines["right"].set_visible(False) ax_v.set_yticks(knots[2:-2]) ax_v.set_xlim(0, 1) ax_v.set_ylim(0, 1) ax_u.set_xlabel("$u$") ax_v.set_ylabel("$v$") ax_v.set_xlabel("$N(v)$") ax_u.set_ylabel("$N(u)$") ax_v.set_yticks(knots[1][2:-2]) ax_u.set_xticks(knots[0][2:-2]) for i in knots[0]: ax2.vlines(i, 0, 1, 'k') for j in knots[1]: ax2.hlines(j, 0, 1, 'k') ax2.set_xlim(0, 1) ax2.set_ylim(0, 1) ax2.set_axis_off() fig2, ax = plt.subplots(constrained_layout=True) ax = p_cpoints(B, ax=ax, dim=2, point=False, line=True, color="black", linestyle="-") ax = p_cpoints(B, ax=ax, dim=2, point=True, line=False, color="red") ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ax.set_xlabel("$x$") ax.set_ylabel("$y$") ax.set_aspect(0.8) fig2.tight_layout(pad=0, h_pad=0, w_pad=0) fig2.show() from pygeoiga.nurb.refinement import knot_insertion knot_ins_1 = [0.3, 0.6] knot_ins_0 = [0.25, 0.5, 0.75] B, knots = knot_insertion(B, (2, 2), knots, knot_ins_0, direction=0) B, knots = knot_insertion(B, (2, 2), knots, knot_ins_1, direction=1) new = B N_spline_u, _ = basis_function_array_nurbs(knot_vector=knots[0], degree=2, resolution=100) N_spline_v, _ = basis_function_array_nurbs(knot_vector=knots[1], degree=2, resolution=100) resol = np.linspace(0, 1, 100) ax_bu.plot(resol, N_spline_u) ax_bu.spines["top"].set_visible(False) ax_bu.spines["right"].set_visible(False) ax_bu.set_xlim(0, 1) ax_bu.set_ylim(0, 1) ax_bv.plot(N_spline_v, resol) ax_bv.spines["top"].set_visible(False) ax_bv.spines["right"].set_visible(False) ax_bv.set_yticks(knots[2:-2]) ax_bv.set_xlim(0, 1) ax_bv.set_ylim(0, 1) ax_bu.set_xlabel("$u$") ax_bv.set_ylabel("$v$") ax_bv.set_xlabel("$B(v)$") ax_bu.set_ylabel("$B(u)$") ax_bv.set_yticks(knots[1][2:-2]) ax_bu.set_xticks(knots[0][2:-2]) #for ax in ax_u, ax_v, ax2, no, ax_bu, ax_bv: # ax.label_outer() fig2.show() degree_u = len(np.where(knots[0] == 0.)[0]) - 1 degree_v = len(np.where(knots[1] == 0.)[0]) - 1 n_xi = len(knots[0]) - degree_u - 3 n_eta = len(knots[1]) - degree_v - 3 from pygeoiga.analysis.common import IEN_element_topology, transform_matrix_B IEN = IEN_element_topology(n_xi, n_eta, degree_u) P, W = transform_matrix_B(B) fig3, ax = plt.subplots(constrained_layout=True) for i in range(0, n_xi, 2): pos = IEN[i::n_xi] for e in pos[::2]: # cont = np.hstack([IEN[0][:degree_u+1], IEN[0][degree_u+3], np.flip(IEN[0][-degree_u-1:]), IEN[0][degree_u+1], IEN[0][0]]) cont = np.hstack([ e[:degree_u + 1], e[degree_u + 3], np.flip(e[-degree_u - 1:]), e[degree_u + 1], e[0] ]) ax.plot(P[cont][:, 0], P[cont][:, 1], linestyle="-", color="black", marker=None) ax = p_cpoints(B, ax=ax, dim=2, point=True, line=False, color="red") ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ax.set_xlabel("$x$") ax.set_ylabel("$y$") ax.set_aspect(0.8) fig3.tight_layout(pad=0, h_pad=0, w_pad=0) fig.show() fig2.show() fig3.show() fig_0, ax = plt.subplots(constrained_layout=True) ax = p_knots(knots, B, ax=ax, dim=2, point=False, line=True, color="k") ax = p_surface(knots, B, ax=ax, dim=2, fill=True, border=False, color="gray", alpha=0.5) ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) ax.set_xlabel("$x$") ax.set_ylabel("$y$") ax.set_aspect(0.8) fig_0.tight_layout(pad=0, h_pad=0, w_pad=0) fig_0.show() save = False if save or save_all: fig.savefig(fig_folder + "bezier_extraction.pdf", **kwargs_savefig) fig_0.savefig(fig_folder + "curve_original.pdf", **kwargs_savefig) fig2.savefig(fig_folder + "control_original.pdf", **kwargs_savefig) fig3.savefig(fig_folder + "control_bezier.pdf", **kwargs_savefig)
def test_explicit_model(): import matplotlib from pygeoiga.plot.nrbplotting_mpl import create_figure, p_cpoints, p_knots, p_curve, p_surface img = matplotlib.image.imread('dike.jpg') fig, ax = plt.subplots() ax.imshow(img, alpha=.8, extent=(0, 300, 0, 178)) ax.set_aspect("equal") ax.set_xlabel("x (m)") ax.set_ylabel("y (m)") ### Big dike cp_1 = np.array([[[223, 0], [145, 60], [108, 90], [71, 149]], [[300, 0], [300, 4.5], [141, 111], [104, 159]]]) kn1_1 = [0, 0, 1, 1] kn1_2 = [0, 0, 0.3, 0.6, 1, 1] knots_1 = [kn1_1, kn1_2] ax = p_cpoints(cp_1, ax=ax, dim=2, color="black", marker="s", point=True, line=False) ax = p_knots(knots_1, cp_1, ax=ax, dim=2, point=True, line=True) ### middle dike cp_2 = np.array([[[0, 40], [92, 86], [197, 154], [300, 178]], [[0, 94], [80, 120], [91, 144], [204, 178]]]) kn2_1 = [0, 0, 1, 1] kn2_2 = [0, 0, 0.3, 0.6, 1, 1] knots_2 = [kn2_1, kn2_2] ax = p_cpoints(cp_2, ax=ax, dim=2, color="black", marker="s", point=True, line=False) ax = p_knots(knots_2, cp_2, ax=ax, dim=2, point=True, line=True) ### Weathered dike cp_3 = np.array([[[0, 94], [90, 100], [91, 144], [204, 178]], [[0, 100], [80, 150], [91, 160], [204, 178]], [[0, 178], [80, 178], [142, 178], [204, 178]]]) kn3_1 = [0, 0, 0, 1, 1, 1] kn3_2 = [0, 0, 0, 0.5, 1, 1, 1] knots_3 = [kn3_1, kn3_2] ax = p_cpoints(cp_3, ax=ax, dim=2, color="black", marker="s", point=True, line=False) ax = p_knots(knots_3, cp_3, ax=ax, dim=2, point=True, line=True) ### Bottom cp_4 = np.array([[[0, 40], [92, 86], [197, 154], [300, 178]], [[0, 0], [92, 0], [197, 0], [300, 0]]]) kn4_1 = [0, 0, 1, 1] kn4_2 = [0, 0, 0.3, 0.6, 1, 1] knots_4 = [kn4_1, kn4_2] ax = p_cpoints(cp_4, ax=ax, dim=2, color="black", marker="s", point=True, line=False) ax = p_knots(knots_4, cp_4, ax=ax, dim=2, point=True, line=True) ax.set_xlim(0, 300) ax.set_ylim(0, 178) fig.show() fig2, ax2 = create_figure("2d") p_surface(knots_4, cp_4, ax=ax2, dim=2, color="red", border=False, label="Dike1") p_surface(knots_2, cp_2, ax=ax2, dim=2, color="blue", border=False, label="Dike2") p_surface(knots_1, cp_1, ax=ax2, dim=2, color="yellow", border=False, label="Dike3") p_surface(knots_3, cp_3, ax=ax2, dim=2, color="gray", border=False, label="Unknown") ax2.set_xlim(0, 300) ax2.set_ylim(0, 178) ax2.set_aspect("equal") ax2.set_xlabel("x (m)") ax2.set_ylabel("y (m)") ax2.legend(loc="center right", facecolor='white', framealpha=0.8) fig2.show() fig3, ax3 = create_figure("2d") ax3.imshow(img, alpha=.8, extent=(0, 300, 0, 178)) #p_surface(knots_4, cp_4, ax=ax3, dim=2, color="red", fill=False, label="Dike1") #p_surface(knots_2, cp_2, ax=ax3, dim=2, color="blue", fill=False, label="Dike2") #p_surface(knots_1, cp_1, ax=ax3, dim=2, color="yellow", fill=False, label="Dike3") #p_surface(knots_3, cp_3, ax=ax3, dim=2, color="gray", fill=False, label="Unknown") ax3.set_xlim(0, 300) ax3.set_ylim(0, 178) ax3.set_aspect("equal") ax3.set_xlabel("x (m)") ax3.set_ylabel("y (m)") #ax3.legend(loc="center right") fig3.show() save = False if save or save_all: fig2.savefig(fig_folder + "model_explicit.pdf", **kwargs_savefig) fig3.savefig(fig_folder + "original_explicit.pdf", **kwargs_savefig)
def test_listings(): import numpy as np from collections import OrderedDict def create_three_layer_model(): # Lower layer control points bottom_c = np.array([[[0., 0., 1.], [0., 50., 1.], [0., 100., 1.]], [[250., 0., 1.], [250., 180., 1.], [250., 250., 1.]], [[500., 0., 1.], [500., 50., 1.], [500., 100., 1.]]]) knot_b = ([0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1]) # knot vector (U, V) # Middle layer control points middle_c = np.array([[[0., 100., 1.], [0., 200., 1.], [0., 300., 1.]], [[250., 250., 1.], [250., 380., 1.], [250., 400., 1.]], [[500., 100., 1.], [500., 200., 1.], [500., 300., 1.]]]) knot_m = ([0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1]) # knot vector (U, V) # Upper layer control points upper_c = np.array([[[0., 300., 1.], [0., 400., 1.], [0., 500., 1.]], [[250., 400., 1.], [250., 450., 1.], [250., 500., 1.]], [[500., 300., 1.], [500., 400., 1.], [500., 500., 1.]]]) knot_u = ([0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1]) # knot vector (U, V) cpoints = [bottom_c, middle_c, upper_c] knots = [knot_b, knot_m, knot_u] geometry = OrderedDict({}) name = ["Granite", "Mudstone", "Sandstone"] # type of litholgy for i, lith in enumerate(name): geometry[lith] = {"B": cpoints[i], "knots": knots[i]} return geometry def assign_properties(geometry: dict): color = ["red", "blue", "green"] # Assign a fixed color to the NURBS. Useful for visualization kappa = [3.1, 0.9, 3] # Thermal conductivity of the layer [W/mK] position = [(1, 1), (2, 1), (3, 1)] # Position of the patch in a global grid (Row and colum) for i, patch_name in enumerate(geometry.keys()): geometry[patch_name]["kappa"] = kappa[i] geometry[patch_name]["color"] = color[i] geometry[patch_name]["position"] = position[i] # Topology of patches -BOUNDARIES - faces of the patch in contact # 0: down; 1: right; 2: up; 3: left # Granite is in contact to mudstone in the "up" face geometry["Granite"]["patch_faces"] = {2: "Mudstone"} # Mudstone is in contact to granite in the "down" face and to sandstone in the "up" face geometry["Mudstone"]["patch_faces"] = {0: "Granite", 2: "Sandstone"} # Sandstone is in contact to mudstone in the "down" face geometry["Sandstone"]["patch_faces"] = {0: "Mudstone"} # Specify the face that have a boundary condition #geometry["granite"]["BC"] = {0: "bot_bc"} #geometry["sandstone"]["BC"] = {2: "top_bc"} return geometry def refinement(geometry: dict): # Knot insertion following the computational procedure of Piegl and Tille (1997) from pygeoiga.nurb.refinement import knot_insertion knot_ins_0 = np.arange(0.1, 1, 0.1) knot_ins_1 = np.arange(0.1, 1, 0.1) for count, patch_name in enumerate(geometry.keys()): B = geometry[patch_name].get("B") knots = geometry[patch_name].get("knots") B, knots = knot_insertion(B, degree=(2, 2), knots=knots, knots_ins=knot_ins_0, direction=0) # U refinement B, knots = knot_insertion(B, degree=(2, 2), knots=knots, knots_ins=knot_ins_1, direction=1) # V refinement geometry[patch_name]["B"] = B geometry[patch_name]["knots"] = knots return geometry def form_k_IGA(K_glb: np.ndarray, IEN: np.ndarray, P: list, kappa: float, nx: int, ny: int, degree: int, knots: float): """ Function to form the stiffness matrix Args: K_glb: empty stiffness matrix IEN: element topology = numbering of control points P: coordinates of NURBS control points x, y, z in single matrix (:,3) kappa: Thermal conductivity nx: number of elements in u direction ny: number of elments in v direction degree: polynomial order knots: knot vector (U, V) Returns: K -> Stiffness matrix """ from pygeoiga.analysis.common import gauss_points from pygeoiga.analysis.iga import nurb_basis_IGA, jacobian_IGA # Gauss quadrature for performing numerical integration. Gauss points and weights G, W = gauss_points(degree) # Assign a value of thermal conductivity to each control point and calculate the element thermal conductivity #kappa_element = kappa_domain(kappa, IEN) # Knot vector from each parametric direction U = knots[0][degree:-degree] V = knots[1][degree:-degree] e = 0 for i in range(ny): for j in range(nx): IEN_e = IEN[e] # Element topology of current element eDOF = IEN_e # Element degrees of freedom #kappa_e = kappa_element[e] # k = 0 for g in range(len(G)): # Obtain gauss points from reference element xi = G[g, 0] # Gauss point in xi direction eta = G[g, 1] # Gauss point in eta direction w = W[g] # weight of Gauss point # Map Gauss points to parameter space u = U[j] + (xi + 1) * (U[j + 1] - U[j]) / 2 v = V[i] + (eta + 1) * (V[i + 1] - V[i]) / 2 # Evaluate basis functions and derivatives N_u, dN_u, N_v, dN_v = nurb_basis_IGA(u, v, knots=knots, degree=degree) # Map of derivatives from parameter space to reference element dN_xi = dN_u / 2 dN_eta = dN_v / 2 # Collect the basis functions and derivatives which support the element N_u = N_u[j:j + degree + 1] dN_xi = dN_xi[j:j + degree + 1] N_v = N_v[i:i + degree + 1] dN_eta = dN_eta[i:i + degree + 1] # Calculate the Jacobian to map from the reference element to physical space J, dxy = jacobian_IGA(N_u, dN_xi, N_v, dN_eta, P, IEN_e, degree) # add contributions to local stiffness matrix k = k + (dxy.T @ dxy) * np.linalg.det(J) * w * kappa#kappa_e # Add local stiffness matrix to global stiffness matrix K_glb[np.ix_(eDOF, eDOF)] = K_glb[np.ix_(eDOF, eDOF)] + k e += 1 return K_glb geometry = create_three_layer_model() geometry = assign_properties(geometry) geometry = refinement(geometry) from pygeoiga.analysis.MultiPatch import patch_topology # Extract the information from each patch and create a global numbering of the multi-patch geometry. # The total amount of non-repeated control points will be global degrees of freedom geometry, gDoF = patch_topology(geometry) print(gDoF) #from pygeoiga.analysis.iga import form_k_IGA def assemble_stiffness_matrix(geometry: dict, gDoF: int): """ Args: geometry: NURBS multipatch gDoF: Global degrees of freedom Return: K_glob: Global stiffnes matrix """ # Set empty the stiffness matrix according to the global degrees of freedom K_glob = np.zeros((gDoF, gDoF)) # iterate over the patches for patch_id in geometry.keys(): pDof = geometry[patch_id].get("patch_DOF") # Degrees of freedom per patch K = np.zeros((pDof, pDof)) # Initialize empty patch stiffness matrix nx, ny = geometry[patch_id].get("n_element") # number of elements in u and v parametric coordinates U, V = geometry[patch_id].get("knots") # knot vectors degree = geometry[patch_id].get("degree") # degree of NURBS patch # Currently support only the same degree for both parametric directions assert degree[0] == degree[1] degree = degree[0] P = geometry[patch_id].get("list_cp") # Get list with location of control points IEN = geometry[patch_id].get("IEN") # connectivity array (element topology) kappa = geometry[patch_id].get("kappa") # Patch thermal conductivity # create patch stiffness matrix K = form_k_IGA(K, IEN, P, kappa, nx, ny, degree, knots=(U, V)) # Assemble global stiffness matrix according to global indexing patch_glob_num = geometry[patch_id].get("glob_num") K_glob[np.ix_(patch_glob_num, patch_glob_num)] = K_glob[np.ix_(patch_glob_num, patch_glob_num)] + K return K_glob K_glob = assemble_stiffness_matrix(geometry, gDoF) fig, ax = plt.subplots(constrained_layout=True) ax.spy(K_glob) ax.set_xlabel("Global $K$ of Anticline multi-patch geometry") fig.show() save = False if save or save_all: fig.savefig(fig_folder+"stiffnes_matrix.pdf", **kwargs_savefig) from pygeoiga.analysis.MultiPatch import boundary_condition_mp a = np.zeros(gDoF) T_top = 10; T_bottom = 25; T_left = None; T_right = None # Also possible to pass a function as #T_bottom = lambda x, m: 10 * np.sin(np.pi * x / m) # with x the position of the node and m the total number of nodes bc, a = boundary_condition_mp(geometry, a, T_top, T_bottom, T_left, T_right) bc["gDOF"] = gDoF from scipy.sparse.linalg import cg # Empty Force vector F = np.zeros(gDoF) def solve(bc: dict, K: np.ndarray, F: np.ndarray, a: np.ndarray): prDOF = bc.get("prDOF") # list of indexes for control points with boundary condition gDOF = bc.get("gDOF") # Degrees of freedom for the system # Find the active control points acDOF = np.setxor1d(np.arange(0, gDOF), prDOF).astype('int') # Reduced stiffness matrix using only active control points Kfs = K[np.ix_(acDOF, prDOF)] f = F[acDOF] bf_n = f - Kfs @ a[prDOF] # Solve for the system of equations for displacement vector a[acDOF], _ = cg(A=K[np.ix_(acDOF, acDOF)], b=bf_n) # Calculate Force vector F = K @ a return a, F a, F = solve(bc, K_glob, F, a) from pygeoiga.analysis.iga import map_solution_elements def map_MP_elements(geometry, a): for patch_id in geometry.keys(): degree, _ = geometry[patch_id].get("degree") # Degree of NURBS patch P = geometry[patch_id].get("list_cp") # List of control points associated to the patch nx, ny = geometry[patch_id].get("n_element") # number of elements in u and v diection n, m = geometry[patch_id].get("n_basis") # number of basis functions in u and v direction ncp = n * m # Total amount of control points W = geometry[patch_id].get("list_weight") # List of weights knots = geometry[patch_id].get("knots") # knot vectors in u and v glob_num = geometry[patch_id].get("glob_num") # global index numbering a_patch = a[glob_num] # Extract from the displacement vector the values corresponding to the patch control points IEN = geometry[patch_id].get("IEN") # Connectivity array (element topology) # Procedure to obtain the coordinate x and y of the temperature value t x_temp, y_temp, t_temp = map_solution_elements(a_patch, degree, P, nx, ny, n, m, ncp, IEN, W, knots) geometry[patch_id]["x_sol"] = x_temp geometry[patch_id]["y_sol"] = y_temp geometry[patch_id]["t_sol"] = t_temp return geometry geometry = map_MP_elements(geometry, a) fig_sol, [ax1, ax2, ax3] = plt.subplots(1, 3, figsize=(15,5), sharey=True) from pygeoiga.plot.nrbplotting_mpl import p_surface, p_cpoints, p_knots, np from pygeoiga.plot.solution_mpl import p_temperature for patch_id in geometry.keys(): ax1 = p_surface(geometry[patch_id].get("knots"), geometry[patch_id].get("B"), color=geometry[patch_id].get("color"), dim=2, fill=True, border=False, ax=ax1) x = geometry[patch_id].get("x_sol") y = geometry[patch_id].get("y_sol") t = geometry[patch_id].get("t_sol") ax2 = p_temperature(x, y, t, vmin=np.min(a), vmax=np.max(a), levels=100, show=False, colorbar=False, ax=ax2, point=True, fill=False, markersize=50) ax2 = p_knots(geometry[patch_id].get("knots"), geometry[patch_id].get("B"), ax=ax2, color='k', dim=2, point=False, line=True, linestyle="--", linewidth=0.2) #ax2 = p_cpoints(geometry[patch_id].get("B"), dim=2, ax=ax2, point=True, line=False) ax3 = p_temperature(x, y, t, vmin=np.min(a), vmax=np.max(a), levels=200, show=False, colorbar=True, ax=ax3, point=False, fill=True, contour=False) ax1.legend(labels=list(geometry.keys()), handles=ax1.patches, loc='upper left', bbox_to_anchor=(0.05, .9), borderaxespad=0) ax1.set_ylabel(r"$y$") for ax in ax1, ax2, ax3: ax.set_aspect("equal") ax.set_xlabel(r"$x$") ax.set_xlim(0,500) ax.set_ylim(0,500) for c in ax3.collections: c.set_edgecolor("face") plt.tight_layout() fig_sol.show() save = False if save or save_all: fig_sol.savefig(fig_folder + "solution_anticline.pdf", **kwargs_savefig)