|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +from scipy.optimize import brentq |
| 7 | +import sys |
| 8 | +import os |
| 9 | +import matplotlib.pyplot as plt |
| 10 | +from util import msg, runparams, io |
| 11 | + |
| 12 | +usage = """ |
| 13 | + compare the output for a dam problem with the exact solution contained |
| 14 | + in dam-exact.out. |
| 15 | +
|
| 16 | + usage: ./dam_compare.py file |
| 17 | +""" |
| 18 | + |
| 19 | + |
| 20 | +def abort(string): |
| 21 | + print(string) |
| 22 | + sys.exit(2) |
| 23 | + |
| 24 | + |
| 25 | +if not len(sys.argv) == 2: |
| 26 | + print(usage) |
| 27 | + sys.exit(2) |
| 28 | + |
| 29 | +try: |
| 30 | + file1 = sys.argv[1] |
| 31 | +except IndexError: |
| 32 | + print(usage) |
| 33 | + sys.exit(2) |
| 34 | + |
| 35 | +sim = io.read(file1) |
| 36 | +myd = sim.cc_data |
| 37 | +myg = myd.grid |
| 38 | + |
| 39 | +# time of file |
| 40 | +t = myd.t |
| 41 | +if myg.nx > myg.ny: |
| 42 | + # x-problem |
| 43 | + xmin = myg.xmin |
| 44 | + xmax = myg.xmax |
| 45 | + param_file = "inputs.dam.x" |
| 46 | +else: |
| 47 | + # y-problem |
| 48 | + xmin = myg.ymin |
| 49 | + xmax = myg.ymax |
| 50 | + param_file = "inputs.dam.y" |
| 51 | + |
| 52 | + |
| 53 | +height = myd.get_var("height") |
| 54 | +xmom = myd.get_var("x-momentum") |
| 55 | +ymom = myd.get_var("y-momentum") |
| 56 | + |
| 57 | +# get the 1-d profile from the simulation data -- assume that whichever |
| 58 | +# coordinate is the longer one is the direction of the problem |
| 59 | + |
| 60 | +# parameter defaults |
| 61 | +rp = runparams.RuntimeParameters() |
| 62 | +rp.load_params("../_defaults") |
| 63 | +rp.load_params("../swe/_defaults") |
| 64 | +rp.load_params("../swe/problems/_dam.defaults") |
| 65 | + |
| 66 | +# now read in the inputs file |
| 67 | +if not os.path.isfile(param_file): |
| 68 | + # check if the param file lives in the solver's problems directory |
| 69 | + param_file = "../swe/problems/" + param_file |
| 70 | + if not os.path.isfile(param_file): |
| 71 | + msg.fail("ERROR: inputs file does not exist") |
| 72 | + |
| 73 | +rp.load_params(param_file, no_new=1) |
| 74 | + |
| 75 | +if myg.nx > myg.ny: |
| 76 | + # x-problem |
| 77 | + x = myg.x[myg.ilo:myg.ihi+1] |
| 78 | + jj = myg.ny//2 |
| 79 | + |
| 80 | + h = height[myg.ilo:myg.ihi+1, jj] |
| 81 | + |
| 82 | + u = xmom[myg.ilo:myg.ihi+1, jj]/h |
| 83 | + ut = ymom[myg.ilo:myg.ihi+1, jj]/h |
| 84 | + |
| 85 | +else: |
| 86 | + |
| 87 | + # y-problem |
| 88 | + x = myg.y[myg.jlo:myg.jhi+1] |
| 89 | + ii = myg.nx//2 |
| 90 | + |
| 91 | + h = height[ii, myg.jlo:myg.jhi+1] |
| 92 | + |
| 93 | + u = ymom[ii, myg.jlo:myg.jhi+1]/h |
| 94 | + ut = xmom[ii, myg.jlo:myg.jhi+1]/h |
| 95 | + |
| 96 | +print(myg) |
| 97 | + |
| 98 | +x_exact = x |
| 99 | +h_exact = np.zeros_like(x) |
| 100 | +u_exact = np.zeros_like(x) |
| 101 | + |
| 102 | +# find h0, h1 |
| 103 | +h1 = rp.get_param("dam.h_left") |
| 104 | +h0 = rp.get_param("dam.h_right") |
| 105 | + |
| 106 | + |
| 107 | +def find_h2(h2): |
| 108 | + return (h2/h1)**3 - 9*(h2/h1)**2*(h0/h1) + \ |
| 109 | + 16*(h2/h1)**1.5*(h0/h1) - (h2/h1)*(h0/h1)*(h0/h1+8) + \ |
| 110 | + (h0/h1)**3 |
| 111 | + |
| 112 | + |
| 113 | +h2 = brentq(find_h2, min(h0, h1), max(h0, h1)) |
| 114 | + |
| 115 | +# calculate sound speeds |
| 116 | +g = rp.get_param("swe.grav") |
| 117 | + |
| 118 | +c0 = np.sqrt(g*h0) |
| 119 | +c1 = np.sqrt(g*h1) |
| 120 | +c2 = np.sqrt(g*h2) |
| 121 | + |
| 122 | +u2 = 2 * (c1 - c2) |
| 123 | + |
| 124 | +# shock speed |
| 125 | +xi = c0 * np.sqrt(1/8 * ((2*(c2/c0)**2 + 1)**2 - 1)) |
| 126 | + |
| 127 | +xctr = 0.5*(xmin + xmax) |
| 128 | + |
| 129 | +# h0 |
| 130 | +idx = x >= xctr + xi*t |
| 131 | +h_exact[idx] = h0 |
| 132 | +u_exact[idx] = 0 |
| 133 | + |
| 134 | +# h1 |
| 135 | +idx = x <= xctr - c1*t |
| 136 | +h_exact[idx] = h1 |
| 137 | +u_exact[idx] = 0 |
| 138 | + |
| 139 | +# h2 |
| 140 | +idx = ((x >= xctr + (u2-c2)*t) & (x < xctr + xi*t)) |
| 141 | +h_exact[idx] = h2 |
| 142 | +u_exact[idx] = u2 |
| 143 | + |
| 144 | +# h3 |
| 145 | +idx = ((x >= xctr - c1*t) & (x < xctr + (u2-c2)*t)) |
| 146 | +c3 = 1/3 * (2*c1 - (x-xctr)/t) |
| 147 | +h_exact[idx] = c3[idx]**2 / g |
| 148 | +u_exact[idx] = 2 * (c1-c3[idx]) |
| 149 | + |
| 150 | +# plot |
| 151 | +fig, axes = plt.subplots(nrows=2, ncols=1, num=1) |
| 152 | + |
| 153 | +plt.rc("font", size=10) |
| 154 | + |
| 155 | +ax = axes.flat[0] |
| 156 | + |
| 157 | +ax.plot(x_exact, h_exact, label='Exact') |
| 158 | +ax.scatter(x, h, marker="x", s=7, color="r", label='Pyro') |
| 159 | + |
| 160 | +ax.set_ylabel(r"$h$") |
| 161 | +ax.set_xlim(0, 1.0) |
| 162 | +ax.set_ylim(0, 1.1) |
| 163 | + |
| 164 | +ax = axes.flat[1] |
| 165 | + |
| 166 | +ax.plot(x_exact, u_exact) |
| 167 | +ax.scatter(x, u, marker="x", s=7, color="r") |
| 168 | + |
| 169 | +ax.set_ylabel(r"$u$") |
| 170 | +ax.set_xlim(0, 1.0) |
| 171 | + |
| 172 | +if (myg.nx > myg.ny): |
| 173 | + ax.set_xlabel(r"x") |
| 174 | +else: |
| 175 | + ax.set_xlabel(r"y") |
| 176 | + |
| 177 | +lgd = axes.flat[0].legend() |
| 178 | + |
| 179 | +plt.subplots_adjust(hspace=0.25) |
| 180 | + |
| 181 | +fig.set_size_inches(8.0, 8.0) |
| 182 | + |
| 183 | +plt.savefig("dam_compare.png", bbox_inches="tight") |
0 commit comments