def plot_mesh(pb): # plot mesh for macro problem coors = pb.domain.mesh.coors graph = pb.domain.mesh.get_conn(pb.domain.mesh.descs[0]) fig2 = plt.figure(figsize=(5, 6)) ax = fig2.add_subplot(111, projection='3d') for e in range(graph.shape[0]): tupleList = coors[graph[e, :], :] vertices = [[0, 1, 2, 3], [4, 5, 6, 7], [0, 1, 5, 4], [1, 2, 6, 5], [2, 3, 7, 6], [3, 0, 4, 7]] verts = [[ tupleList[vertices[ix][iy]] for iy in range(len(vertices[0])) ] for ix in range(len(vertices))] pc3d = Poly3DCollection(verts=verts, facecolors='white', edgecolors='black', linewidths=1, alpha=0.5) ax.add_collection3d(pc3d) ax.set_xlim3d(-1.2, 1.2) ax.set_ylim3d(-1.2, 1.2) ax.set_zlim3d(-0.01, 3.2) ax.set_title('3D plot of macro system') plt.show(fig2) return None
def plot_gaps(fig_num, plot_rsc, gaps, kinds, freq_range, plot_range, show=False, clear=False, new_axes=False): """ Plot band gaps as rectangles. """ if plt is None: return fig = plt.figure(fig_num) if clear: fig.clf() if new_axes: ax = fig.add_subplot(111) else: ax = fig.gca() for ii in xrange(len(freq_range) - 1): f0, f1 = freq_range[[ii, ii + 1]] gap = gaps[ii] if isinstance(gap, list): for ig, (gmin, gmax) in enumerate(gap): kind, kind_desc = kinds[ii][ig] plot_gap(ax, ii, f0, f1, kind, kind_desc, gmin, gmax, plot_range, plot_rsc) else: gmin, gmax = gap kind, kind_desc = kinds[ii] plot_gap(ax, ii, f0, f1, kind, kind_desc, gmin, gmax, plot_range, plot_rsc) if new_axes: ax.set_xlim([freq_range[0], freq_range[-1]]) ax.set_ylim(plot_range) if show: plt.show() return fig
def main(): from sfepy.base.base import output from sfepy.base.conf import ProblemConf, get_standard_keywords from sfepy.discrete import Problem from sfepy.base.plotutils import plt parser = OptionParser(usage=usage, version='%prog') parser.add_option('-n', '--no-plot', action="store_true", dest='no_plot', default=False, help=helps['no_plot']) options, args = parser.parse_args() required, other = get_standard_keywords() # Use this file as the input file. conf = ProblemConf.from_file(__file__, required, other) # Create problem instance, but do not set equations. problem = Problem.from_conf(conf, init_equations=False) # Solve the problem. Output is ignored, results stored by using the # step_hook. u_t = solve_branch(problem, linear_tension) u_c = solve_branch(problem, linear_compression) # Get pressure load by calling linear_*() for each time step. ts = problem.get_timestepper() load_t = nm.array([linear_tension(ts, nm.array([[0.0]]), 'qp')['val'] for aux in ts.iter_from(0)], dtype=nm.float64).squeeze() load_c = nm.array([linear_compression(ts, nm.array([[0.0]]), 'qp')['val'] for aux in ts.iter_from(0)], dtype=nm.float64).squeeze() # Join the branches. displacements = {} for key in u_t.keys(): displacements[key] = nm.r_[u_c[key][::-1], u_t[key]] load = nm.r_[load_c[::-1], load_t] if plt is None: output('matplotlib cannot be imported, printing raw data!') output(displacements) output(load) else: legend = [] for key, val in six.iteritems(displacements): plt.plot(load, val) legend.append(key) plt.legend(legend, loc = 2) plt.xlabel('tension [kPa]') plt.ylabel('displacement [mm]') plt.grid(True) plt.gcf().savefig('pressure_displacement.png') if not options.no_plot: plt.show()
def main(): from sfepy.base.conf import ProblemConf, get_standard_keywords from sfepy.fem import ProblemDefinition from sfepy.base.plotutils import plt parser = OptionParser(usage=usage, version='%prog') parser.add_option('-n', '--no-plot', action="store_true", dest='no_plot', default=False, help=helps['no_plot']) options, args = parser.parse_args() required, other = get_standard_keywords() # Use this file as the input file. conf = ProblemConf.from_file( __file__, required, other ) # Create problem instance, but do not set equations. problem = ProblemDefinition.from_conf( conf, init_equations = False ) # Solve the problem. Output is ignored, results stored by using the # step_hook. u_t = solve_branch(problem, linear_tension) u_c = solve_branch(problem, linear_compression) # Get pressure load by calling linear_*() for each time step. ts = problem.get_timestepper() load_t = nm.array([linear_tension(ts, nm.array([[0.0]]), 'qp')['val'] for aux in ts.iter_from( 0 )], dtype=nm.float64).squeeze() load_c = nm.array([linear_compression(ts, nm.array([[0.0]]), 'qp')['val'] for aux in ts.iter_from( 0 )], dtype=nm.float64).squeeze() # Join the branches. displacements = {} for key in u_t.keys(): displacements[key] = nm.r_[u_c[key][::-1], u_t[key]] load = nm.r_[load_c[::-1], load_t] if plt is None: print 'matplotlib cannot be imported, printing raw data!' print displacements print load else: legend = [] for key, val in displacements.iteritems(): plt.plot( load, val ) legend.append( key ) plt.legend( legend, loc = 2 ) plt.xlabel( 'tension [kPa]' ) plt.ylabel( 'displacement [mm]' ) plt.grid( True ) plt.gcf().savefig( 'pressure_displacement.png' ) if not options.no_plot: plt.show()
def plot_logs(fig_num, plot_rsc, plot_labels, freqs, logs, valid, freq_range, plot_range, draw_eigs=True, show_legend=True, show=False, clear=False, new_axes=False): """ Plot logs of min/middle/max eigs of a mass matrix. """ if plt is None: return fig = plt.figure(fig_num) if clear: fig.clf() if new_axes: ax = fig.add_subplot(111) else: ax = fig.gca() if draw_eigs: plot_eigs(fig_num, plot_rsc, plot_labels, valid, freq_range, plot_range) for ii, log in enumerate(logs): l1 = ax.plot(freqs[ii], log[:, -1], **plot_rsc['eig_max']) if log.shape[1] >= 2: l2 = ax.plot(freqs[ii], log[:, 0], **plot_rsc['eig_min']) else: l2 = None if log.shape[1] == 3: l3 = ax.plot(freqs[ii], log[:, 1], **plot_rsc['eig_mid']) else: l3 = None l1[0].set_label(plot_labels['eig_max']) if l2: l2[0].set_label(plot_labels['eig_min']) if l3: l3[0].set_label(plot_labels['eig_mid']) fmin, fmax = freqs[0][0], freqs[-1][-1] ax.plot([fmin, fmax], [0, 0], **plot_rsc['x_axis']) ax.set_xlabel(plot_labels['x_axis']) ax.set_ylabel(plot_labels['y_axis']) if new_axes: ax.set_xlim([fmin, fmax]) ax.set_ylim(plot_range) if show_legend: ax.legend() if show: plt.show() return fig
def main(): from sfepy.base.conf import ProblemConf, get_standard_keywords from sfepy.fem import ProblemDefinition from sfepy.base.plotutils import plt required, other = get_standard_keywords() # Use this file as the input file. conf = ProblemConf.from_file( __file__, required, other ) # Create problem instance, but do not set equations. problem = ProblemDefinition.from_conf( conf, init_equations = False ) options = Struct( output_filename_trunk = None ) # Solve the problem. Output is ignored, results stored by using the # step_hook. u_t = solve_branch( problem, options, linear_tension ) u_c = solve_branch( problem, options, linear_compression ) # Get pressure load by calling linear_*() for each time step. ts = problem.get_timestepper() load_t = nm.array([linear_tension(ts, nm.array([[0.0]]), 'qp')['val'] for aux in ts.iter_from( 0 )], dtype=nm.float64).squeeze() load_c = nm.array([linear_compression(ts, nm.array([[0.0]]), 'qp')['val'] for aux in ts.iter_from( 0 )], dtype=nm.float64).squeeze() # Join the branches. displacements = {} for key in u_t.keys(): displacements[key] = nm.r_[u_c[key][::-1], u_t[key]] load = nm.r_[load_c[::-1], load_t] if plt is None: print 'matplotlib cannot be imported, printing raw data!' print displacements print load else: legend = [] for key, val in displacements.iteritems(): plt.plot( load, val ) legend.append( key ) plt.legend( legend, loc = 2 ) plt.xlabel( 'tension [kPa]' ) plt.ylabel( 'displacement [mm]' ) plt.grid( True ) plt.gcf().savefig( 'pressure_displacement.png' ) plt.show()
def plot_gaps(fig_num, plot_rsc, gaps, kinds, gap_ranges, freq_range, plot_range, show=False, clear=False, new_axes=False): """ Plot band gaps as rectangles. """ if plt is None: return fig = plt.figure(fig_num) if clear: fig.clf() if new_axes: ax = fig.add_subplot(111) else: ax = fig.gca() for ii in range(len(freq_range) - 1): f0, f1 = freq_range[[ii, ii + 1]] gap = gaps[ii] ranges = gap_ranges[ii] if isinstance(gap, list): for ig, (gmin, gmax) in enumerate(gap): kind, kind_desc = kinds[ii][ig] plot_gap(ax, ranges[ig], kind, kind_desc, plot_range, plot_rsc) output(ii, gmin[0], gmax[0], '%.8f' % f0, '%.8f' % f1) output(' -> %s\n %s' % (kind_desc, ranges[ig])) else: gmin, gmax = gap kind, kind_desc = kinds[ii] plot_gap(ax, ranges, kind, kind_desc, plot_range, plot_rsc) output(ii, gmin[0], gmax[0], '%.8f' % f0, '%.8f' % f1) output(' -> %s\n %s' % (kind_desc, ranges)) if new_axes: ax.set_xlim([freq_range[0], freq_range[-1]]) ax.set_ylim(plot_range) if show: plt.show() return fig
def plot_band_gaps(self, coefs): opts = self.app_options bg_keys = [ key for key in coefs.to_dict() if key.startswith('band_gaps') ] plot_opts = opts.plot_options plot_rsc = opts.plot_rsc plt.rcParams.update(plot_rsc['params']) for ii, key in enumerate(bg_keys): bg = coefs.get(key) plot_labels = opts.plot_labels.get(key, opts.plot_labels) plot_range, teigs = transform_plot_data(bg.logs.eigs, opts.plot_transform, self.conf) fig = plot_gaps(ii, plot_rsc, bg.gaps, bg.kinds, bg.gap_ranges, bg.freq_range_margins, plot_range, clear=True) fig = plot_logs(ii, plot_rsc, plot_labels, bg.logs.freqs, teigs, bg.valid[bg.eig_range], bg.freq_range_initial, plot_range, show_legend=plot_opts['legend'], new_axes=True) plt.tight_layout() if opts.fig_name is not None: fig_name = _get_fig_name(self.problem.output_dir, opts.fig_name, key, 'band_gaps', opts.fig_suffix) fig.savefig(fig_name) if plot_opts['show']: plt.show()
def plot_eigs(fig_num, plot_rsc, plot_labels, valid, freq_range, plot_range, show=False, clear=False, new_axes=False): """ Plot resonance/eigen-frequencies. `valid` must correspond to `freq_range` resonances : red masked resonances: dotted red """ if plt is None: return assert_(len(valid) == len(freq_range)) fig = plt.figure(fig_num) if clear: fig.clf() if new_axes: ax = fig.add_subplot(111) else: ax = fig.gca() l0 = l1 = None for ii, f in enumerate(freq_range): if valid[ii]: l0 = ax.plot([f, f], plot_range, **plot_rsc['resonance'])[0] else: l1 = ax.plot([f, f], plot_range, **plot_rsc['masked'])[0] if l0: l0.set_label(plot_labels['resonance']) if l1: l1.set_label(plot_labels['masked']) if new_axes: ax.set_xlim([freq_range[0], freq_range[-1]]) ax.set_ylim(plot_range) if show: plt.show() return fig
def plot_band_gaps(self, coefs): opts = self.app_options bg_keys = [key for key in coefs.to_dict() if key.startswith('band_gaps')] plot_opts = opts.plot_options plot_rsc = opts.plot_rsc plt.rcParams.update(plot_rsc['params']) for ii, key in enumerate(bg_keys): bg = coefs.get(key) plot_labels = opts.plot_labels.get(key, opts.plot_labels) plot_range, teigs = transform_plot_data(bg.logs.eigs, opts.plot_transform, self.conf) fig = plot_gaps(ii, plot_rsc, bg.gaps, bg.kinds, bg.gap_ranges, bg.freq_range_margins, plot_range, clear=True) fig = plot_logs(ii, plot_rsc, plot_labels, bg.logs.freqs, teigs, bg.valid[bg.eig_range], bg.freq_range_initial, plot_range, show_legend=plot_opts['legend'], new_axes=True) plt.tight_layout() if opts.fig_name is not None: fig_name = _get_fig_name(self.problem.output_dir, opts.fig_name, key, 'band_gaps', opts.fig_suffix) fig.savefig(fig_name) if plot_opts['show']: plt.show()
def plot_dispersion(self, coefs): opts = self.app_options bg_keys = [key for key in coefs.to_dict() if key.startswith('dispersion')] plot_rsc = opts.plot_rsc plot_opts = opts.plot_options plt.rcParams.update(plot_rsc['params']) plot_labels = opts.plot_labels_angle for ii, key in enumerate(bg_keys): pas_key = key.replace('dispersion', 'polarization_angles') pas = coefs.get(pas_key) aux = transform_plot_data(pas, opts.plot_transform_angle, self.conf) plot_range, pas = aux bg = coefs.get(key) fig = plot_gaps(1, plot_rsc, bg.gaps, bg.kinds, bg.freq_range_margins, plot_range, clear=True) fig = plot_logs(1, plot_rsc, plot_labels, bg.logs.freqs, pas, bg.valid[bg.eig_range], bg.freq_range_initial, plot_range, show_legend=plot_opts['legend'], new_axes=True) fig_name = opts.fig_name_angle if fig_name is not None: fig_name = _get_fig_name(self.problem.output_dir, fig_name, key, 'dispersion', opts.fig_suffix) fig.savefig(fig_name) aux = transform_plot_data(bg.logs.eigs, opts.plot_transform_wave, self.conf) plot_range, teigs = aux plot_labels = opts.plot_labels_wave fig = plot_gaps(2, plot_rsc, bg.gaps, bg.kinds, bg.freq_range_margins, plot_range, clear=True) fig = plot_logs(2, plot_rsc, plot_labels, bg.logs.freqs, teigs, bg.valid[bg.eig_range], bg.freq_range_initial, plot_range, show_legend=plot_opts['legend'], new_axes=True) fig_name = opts.fig_name_wave if fig_name is not None: fig_name = _get_fig_name(self.problem.output_dir, fig_name, key, 'dispersion', opts.fig_suffix) fig.savefig(fig_name) if plot_opts['show']: plt.show()
def call( self ): """In parametric runs, cached data (homogenized coefficients, Christoffel acoustic tensor and eigenvalue problem solution) are cleared according to 'clear_cache' aplication options. Example: clear_cache = {'cached_christoffel' : True, 'cached_evp' : True} """ options = self.options for key, val in self.app_options.clear_cache.iteritems(): if val and key.startswith('cached_'): setattr(self, key, None) if options.phase_velocity: # No band gaps in this case. return self.compute_phase_velocity() evp = self.solve_eigen_problem() self.fix_eig_range( evp.eigs.shape[0] ) if options.detect_band_gaps: bg = detect_band_gaps( self.problem, evp.kind, evp.eigs_rescaled, evp.eig_vectors, self.app_options, self.conf.funmod ) if options.plot: plot_range, teigs = transform_plot_data( bg.logs.eigs, bg.opts.plot_transform, self.conf.funmod ) plot_rsc = bg.opts.plot_rsc plot_opts = bg.opts.plot_options plot_labels = bg.opts.plot_labels plt.rcParams.update( plot_rsc['params'] ) fig = plot_gaps( 1, plot_rsc, bg.gaps, bg.kinds, bg.freq_range_margins, plot_range, clear = True ) fig = plot_logs( 1, plot_rsc, plot_labels, bg.logs.freqs, teigs, bg.valid[bg.eig_range], bg.freq_range_initial, plot_range, False, show_legend = plot_opts['legend'], new_axes = True ) fig_name = bg.opts.fig_name if fig_name is not None: fig.savefig( fig_name ) if plot_opts['show']: plt.show() elif options.analyze_dispersion: christoffel, iw_dir = self.compute_cat(ret_iw_dir=True) bg = detect_band_gaps( self.problem, evp.kind, evp.eigs_rescaled, evp.eig_vectors, self.app_options, self.conf.funmod, christoffel = christoffel ) output( 'computing polarization angles...' ) pas = compute_polarization_angles( iw_dir, bg.logs.eig_vectors ) output( '...done' ) bg.polarization_angles = pas output( 'computing phase velocity...' ) bg.phase_velocity = self.compute_phase_velocity() output( '...done' ) if options.plot: plot_rsc = bg.opts.plot_rsc plot_opts = bg.opts.plot_options plt.rcParams.update( plot_rsc['params'] ) aux = transform_plot_data( pas, bg.opts.plot_transform_angle, self.conf.funmod ) plot_range, pas = aux plot_labels = bg.opts.plot_labels_angle fig = plot_gaps( 1, plot_rsc, bg.gaps, bg.kinds, bg.freq_range_margins, plot_range, clear = True ) fig = plot_logs( 1, plot_rsc, plot_labels, bg.logs.freqs, pas, bg.valid[bg.eig_range], bg.freq_range_initial, plot_range, False, show_legend = plot_opts['legend'], new_axes = True ) fig_name = bg.opts.fig_name_angle if fig_name is not None: fig.savefig( fig_name ) aux = transform_plot_data( bg.logs.eigs, bg.opts.plot_transform_wave, self.conf.funmod ) plot_range, teigs = aux plot_labels = bg.opts.plot_labels_wave fig = plot_gaps( 2, plot_rsc, bg.gaps, bg.kinds, bg.freq_range_margins, plot_range, clear = True ) fig = plot_logs( 2, plot_rsc, plot_labels, bg.logs.freqs, teigs, bg.valid[bg.eig_range], bg.freq_range_initial, plot_range, False, show_legend = plot_opts['legend'], new_axes = True ) fig_name = bg.opts.fig_name_wave if fig_name is not None: fig.savefig( fig_name ) if plot_opts['show']: plt.show() else: bg = None return evp, bg