def check_gauges(self, save=False, gauge_id=1, indices=[0], rtol=1e-14, atol=1e-8, tolerance=None): r"""Basic test to assert gauge equality :Input: - *save* (bool) - If *True* will save the output from this test to the file *regresion_data.txt*. Default is *False*. - *indices* (tuple) - Contains indices to compare in the gague comparison. Defaults to *(0)*. - *rtol* (float) - Relative tolerance used in the comparison, default is *1e-14*. Note that the old *tolerance* input is now synonymous with this parameter. - *atol* (float) - Absolute tolerance used in the comparison, default is *1e-08*. """ if isinstance(tolerance, float): rtol = tolerance if not(isinstance(indices, tuple) or isinstance(indices, list)): indices = tuple(indices) # Get gauge data gauge = gauges.GaugeSolution(gauge_id, path=self.temp_path) # Get regression comparison data regression_data_path = os.path.join(self.test_path, "regression_data") if save: gauge_file_name = "gauge%s.txt" % str(gauge_id).zfill(5) shutil.copy(os.path.join(self.temp_path, gauge_file_name), regression_data_path) claw_git_status.make_git_status_file(outdir=regression_data_path) regression_gauge = gauges.GaugeSolution(gauge_id, path=regression_data_path) # Compare data try: for n in indices: numpy.testing.assert_allclose(gauge.q[n, :], regression_gauge.q[n, :], rtol=rtol, atol=atol, verbose=False) except AssertionError as e: err_msg = "\n".join((e.args[0], "Gauge Match Failed for gauge = %s" % gauge_id)) err_msg = "\n".join((err_msg, " failures in fields:")) failure_indices = [] for n in indices: if ~numpy.allclose(gauge.q[n, :], regression_gauge.q[n, :], rtol=rtol, atol=atol): failure_indices.append(str(n)) index_str = ", ".join(failure_indices) raise AssertionError(" ".join((err_msg, index_str)))
def read_gauges(gaugenos='all', outdir=None): if (type(gaugenos) is int) or (type(gaugenos) is str): gaugenos = [gaugenos] # single gaugeno passed in gd = GaugeData(outdir) gd.read() gauge_solutions = {} for gaugeno in gd.gauge_numbers: if (gaugeno in gaugenos) or (gaugenos[0] == 'all'): g = gauges.GaugeSolution(gaugeno, outdir) gauge_solutions[gaugeno] = g if 0: # for debugging: gaugenos_lagrangian = [k for k in gauge_solutions.keys() \ if gauge_solutions[k].gtype=='lagrangian'] gaugenos_stationary = [k for k in gauge_solutions.keys() \ if gauge_solutions[k].gtype=='stationary'] print('+++ Loaded %i stationary and %i Lagrangian gauges' \ % (len(gaugenos_stationary),len(gaugenos_lagrangian))) return gauge_solutions
def plot_regression_gauges(cd, plot_var=(3, 7)): import clawpack.pyclaw.gauges as gauges import numpy fig = plt.gcf() gauge = gauges.GaugeSolution(cd.gaugeno, path="./regression_data") for (i, n) in enumerate(plot_var): fig.axes[i].plot(gauge.t, gauge.q[n, :], 'k-')
def runTest(self, save=False, indices=(2, 3)): r"""Test particles example Note that this stub really only runs the code and performs no tests. """ # Write out data files self.load_rundata() self.write_rundata_objects() # Run code self.run_code() import clawpack.pyclaw.gauges as gauges gauge = gauges.GaugeSolution(1, path=self.temp_path) print('+++ Gauge 1:\n', gauge.q) gauge = gauges.GaugeSolution(2, path=self.temp_path) print('+++ Gauge 2:\n', gauge.q) # Perform tests self.check_gauges(save=save, gauge_id=1, indices=(1, 2)) self.check_gauges(save=save, gauge_id=2, indices=(1, 2)) self.success = True
def getgauge(self, gauge_id, outdir=None, verbose=True): r"""Read in the gauge labeled with `gaugeno` in path `outdir` :Note: The behavior of this function has changed to actually only read in the requested gauge id rather than all of the gauges. The dictionary `gaugesoln_dict` remains the same. :Input: - *gauge_id* - (int) The gauge id of the gauge to be read in. - *outdir* - (path) Path to output directory containing gauge files. Defaults to this data object's `self.outdir`. - *verbose* - (bool) Verbose console output, default is `False`. :Output: - (clawpack.amrclaw.GaugeSolution) The read in gauge solution either from the `gaugeson_dict` or from file. If something went wrong then the routine prints a warning and returns `None`. """ # Construct path to file if outdir is None: outdir = self.outdir outdir = os.path.abspath(outdir) # Reread gauge data file key = (gauge_id, outdir) if self.refresh_gauges or (key not in self.gaugesoln_dict): try: # Read gauge solution: import clawpack.pyclaw.gauges as gauges self.gaugesoln_dict[key] = gauges.GaugeSolution( gauge_id=gauge_id, path=outdir) if verbose: print("Read in gauge %s." % gauge_id) except Exception as e: import warnings warnings.warn(str(e)) return None return self.gaugesoln_dict[key]
def check_old_gauge_data(path, gauge_id): # Load old gauge data data = numpy.loadtxt(path) old_ids = numpy.asarray(data[:, 0], dtype=int) gauge_indices = numpy.nonzero(old_ids == gauge_id)[0] q = data[gauge_indices, 3:] # Load new data gauge = gauges.GaugeSolution(gauge_id, "./regression_data/") print(numpy.linalg.norm(q - gauge.q.transpose(), ord=2)) print(numpy.argmax(q - gauge.q.transpose())) fig = plt.figure() for i in range(gauge.q.shape[0]): axes = fig.add_subplot(1, gauge.q.shape[0], i + 1) axes.plot(q[:, i] - gauge.q[i, :]) axes.set_title("q[%s, :] comparison" % i) return fig
def plot_gauges(outdir_bouss='_output', outdir_swe=None, fname='GaugeComparison.png'): d = loadtxt('experimental_data/ts3b.txt', skiprows=6) tg = d[:, 0] g5 = d[:, 2] g7 = d[:, 4] g8 = d[:, 5] figure(400, figsize=(8, 8)) clf() subplot(311) gaugeno = 5 gauge = gauges.GaugeSolution(gaugeno, outdir_bouss) t = gauge.t eta = gauge.q[2, :] plot(tg - tg[0], g5, 'r', label='Experiment') plot(t, eta, 'b', label='Bouss') if outdir_swe: gauge = gauges.GaugeSolution(gaugeno, outdir_swe) t = gauge.t eta = gauge.q[2, :] plot(t, eta, 'k', label='SWE') xlim(0, 25) ylim(-0.01, 0.08) legend(loc='upper right') grid(True) xlabel('') ylabel('Surface (m)') title('Gauge %i' % gaugeno) subplot(312) gaugeno = 7 gauge = gauges.GaugeSolution(gaugeno, outdir_bouss) t = gauge.t eta = gauge.q[2, :] plot(tg - tg[0], g7, 'r', label='Experiment') plot(t, eta, 'b', label='Bouss') if outdir_swe: gauge = gauges.GaugeSolution(gaugeno, outdir_swe) t = gauge.t eta = gauge.q[2, :] plot(t, eta, 'k', label='SWE') xlim(0, 25) ylim(-0.01, 0.08) legend(loc='upper right') grid(True) xlabel('') ylabel('Surface (m)') title('Gauge %i' % gaugeno) subplot(313) gaugeno = 8 gauge = gauges.GaugeSolution(gaugeno, outdir_bouss) t = gauge.t eta = gauge.q[2, :] plot(tg - tg[0], g8, 'r', label='Experiment') plot(t, eta, 'b', label='Bouss') if outdir_swe: gauge = gauges.GaugeSolution(gaugeno, outdir_swe) t = gauge.t eta = gauge.q[2, :] plot(t, eta, 'k', label='SWE') xlim(0, 25) ylim(-0.01, 0.08) legend(loc='upper right') grid(True) xlabel('') ylabel('Surface (m)') title('Gauge %i' % gaugeno) tight_layout() if fname is not None: savefig(fname, bbox_inches='tight') print('Created %s' % fname)
(-40, 7,(40,90)), (-32, 9,(40,90)), (-31.2,11,(40,90)), (-30.8, 2,(40,90)), (-30, 4,(40,90)), (-28, 6,(40,90)), (-20, 8,(50,100)), (-10, 10,(60,110)), (-5, 12,(60,110))] for info in gauge_info: xp_g, k, xlimits = info subplot(6,2,k) gaugeno = int(-xp_g*10) gauge = gauges.GaugeSolution(gaugeno, outdir) t = gauge.t eta = gauge.q[2,:] plot(t, 100*eta, 'b', label='x = %.1f' % xp_g) ylim(-5, 15) xlim(xlimits) grid(True) xlabel('') #ylabel('Surface (m)') #title('Gauge %i' % gaugeno) legend(loc='upper right') tight_layout()