Nektar++
Loading...
Searching...
No Matches
Functions | Variables
check_hopbc_time_order Namespace Reference

Functions

 curl_curl_expressions ()
 
 advective_expressions ()
 
 set_parameter (root, name, value)
 
 set_function (root, function_name, values)
 
 build_session (args, workdir, formulation, pressure_form, order, nsteps)
 
 run_case (args, workdir, formulation, pressure_form, order, nsteps)
 
 convergence_rate (coarse, fine, coarse_steps, fine_steps)
 
 main ()
 

Variables

dict MIN_RATES = {2: 1.7, 3: 2.5}
 
list DEFAULT_STEPS = [8, 16, 32]
 
list VARIABLES = ["u", "v", "p"]
 
dict MANUFACTURED_SOLUTIONS
 

Function Documentation

◆ advective_expressions()

check_hopbc_time_order.advective_expressions ( )

Definition at line 44 of file check_hopbc_time_order.py.

44def advective_expressions():
45 # u = cos(t)*(x^2, -2*x*y) + sin(t)*(2*x*y, -y^2), the velocities of the
46 # stream functions x^2*y and x*y^2. Its time derivative is not parallel
47 # to it, so (du/dt . grad) u is non-zero, and not a gradient, which the
48 # pressure could absorb: advecting with the latest velocity u^n rather
49 # than u^{n+1}, or an extrapolation of it to the new time level, leaves
50 # an O(dt) consistency error in the momentum equation. The flow is
51 # quadratic, so the spatial discretisation represents it, and its cubic
52 # advection, exactly.
53 a = "cos(t)"
54 b = "sin(t)"
55 pressure = "(x-0.5)*(y-0.5)"
56 pressure_amplitude = "exp(2*t)"
57 values = {
58 "u": f"{a}*x*x+2*{b}*x*y",
59 "v": f"-2*{a}*x*y-{b}*y*y",
60 "p": f"{pressure_amplitude}*{pressure}",
61 }
62 initial = {"u": "x*x", "v": "-2*x*y", "p": pressure}
63 # du/dt + (u . grad) u - Kinvis * laplacian(u) + grad(p)
64 force = {
65 "u": (
66 f"-{b}*x*x+2*{a}*x*y"
67 f"+2*{a}*{a}*x*x*x+2*{a}*{b}*x*x*y+2*{b}*{b}*x*y*y"
68 f"-2*Kinvis*{a}+{pressure_amplitude}*(y-0.5)"
69 ),
70 "v": (
71 f"2*{b}*x*y-{a}*y*y"
72 f"+2*{a}*{a}*x*x*y+2*{a}*{b}*x*y*y+2*{b}*{b}*y*y*y"
73 f"+2*Kinvis*{b}+{pressure_amplitude}*(x-0.5)"
74 ),
75 }
76 return initial, values, force
77
78

◆ build_session()

check_hopbc_time_order.build_session (   args,
  workdir,
  formulation,
  pressure_form,
  order,
  nsteps 
)

Definition at line 104 of file check_hopbc_time_order.py.

104def build_session(args, workdir, formulation, pressure_form, order, nsteps):
105 tree = ET.parse(args.session)
106 root = tree.getroot()
107 initial, exact, force = MANUFACTURED_SOLUTIONS[args.manufactured]()
108
109 root.find(".//TIMEINTEGRATIONSCHEME/METHOD").text = " IMEX "
110 root.find(".//TIMEINTEGRATIONSCHEME/ORDER").text = f" {order} "
111 set_parameter(root, "TimeStep", f"{args.final_time / nsteps:.16g}")
112 set_parameter(root, "NumSteps", str(nsteps))
113 set_parameter(root, "IO_InfoSteps", str(nsteps))
114 set_parameter(root, "IterativeSolverTolerance", "1e-12")
115
116 for expansion in root.findall(".//EXPANSIONS/E"):
117 expansion.set("NUMMODES", str(args.num_modes))
118
119 set_function(root, "InitialConditions", initial)
120 set_function(root, "ExactSolution", exact)
121 set_function(root, "BodyForce", force)
122
123 for condition in root.findall(".//BOUNDARYCONDITIONS/REGION/*"):
124 variable = condition.attrib["VAR"]
125 if args.pressure_dirichlet and variable == "p":
126 condition.tag = "D"
127 if variable in exact and condition.tag == "D":
128 condition.set("VALUE", exact[variable])
129 condition.set("USERDEFINEDTYPE", "TimeDependent")
130
131 name = (
132 f"hopbc_{args.manufactured}_order{order}_{formulation}_"
133 f"{pressure_form}_{nsteps}.xml"
134 )
135 path = workdir / name
136 tree.write(path, encoding="utf-8", xml_declaration=True)
137 return path
138
139

References set_function(), and set_parameter().

Referenced by run_case().

◆ convergence_rate()

check_hopbc_time_order.convergence_rate (   coarse,
  fine,
  coarse_steps,
  fine_steps 
)

Definition at line 184 of file check_hopbc_time_order.py.

184def convergence_rate(coarse, fine, coarse_steps, fine_steps):
185 return math.log(coarse / fine) / math.log(fine_steps / coarse_steps)
186
187

Referenced by main().

◆ curl_curl_expressions()

check_hopbc_time_order.curl_curl_expressions ( )

Definition at line 19 of file check_hopbc_time_order.py.

19def curl_curl_expressions():
20 # The velocity has a non-zero curl-curl, extrapolated from the velocity
21 # history into the higher-order pressure flux, and a time-dependent
22 # boundary value, whose acceleration enters it on the left edge. Its
23 # advection is zero.
24 velocity = "y*(1-y)"
25 pressure = "(x-0.5)*(y-0.5)"
26 velocity_amplitude = "exp(t)"
27 pressure_amplitude = "exp(2*t)"
28 values = {
29 "u": f"{velocity_amplitude}*{velocity}",
30 "v": "0",
31 "p": f"{pressure_amplitude}*{pressure}",
32 }
33 initial = {"u": velocity, "v": "0", "p": pressure}
34 # du/dt - Kinvis * laplacian(u) + grad(p)
35 grad_p_x = f"{pressure_amplitude}*(y-0.5)"
36 grad_p_y = f"{pressure_amplitude}*(x-0.5)"
37 force = {
38 "u": f"{velocity_amplitude}*({velocity}+2*Kinvis)+{grad_p_x}",
39 "v": grad_p_y,
40 }
41 return initial, values, force
42
43

◆ main()

check_hopbc_time_order.main ( )

Definition at line 188 of file check_hopbc_time_order.py.

188def main():
189 parser = argparse.ArgumentParser()
190 parser.add_argument("--solver", type=pathlib.Path, required=True)
191 parser.add_argument("--session", type=pathlib.Path, required=True)
192 parser.add_argument("--op-exec-space", default="Serial")
193 parser.add_argument("--op-impl", default="SumFac")
194 parser.add_argument("--orders", nargs="+", type=int, default=[2, 3])
195 parser.add_argument(
196 "--formulations", nargs="+", default=["semiimplicit", "linearimplicit"]
197 )
198 # The strong pressure form takes the predictor, advection and forcing
199 # into the higher-order pressure flux; the weak form integrates them by
200 # parts onto the volume, and its flux holds the viscous term and the
201 # boundary acceleration only.
202 parser.add_argument(
203 "--pressure-forms",
204 nargs="+",
205 choices=["strong", "weak"],
206 default=["strong"],
207 )
208 parser.add_argument("--steps", nargs="+", type=int, default=DEFAULT_STEPS)
209 parser.add_argument("--final-time", type=float, default=0.2)
210 parser.add_argument("--num-modes", type=int, default=8)
211 parser.add_argument("--timeout", type=float, default=120.0)
212 parser.add_argument("--pressure-dirichlet", action="store_true")
213 # Thresholds for a formulation below the order of the scheme: the final
214 # rate of every variable must reach --min-rate, and that of the pressure
215 # --min-pressure-rate where given; the defaults are MIN_RATES[order].
216 parser.add_argument("--min-rate", type=float)
217 parser.add_argument("--min-pressure-rate", type=float)
218 # curlcurl: a channel flow with a non-zero curl-curl and no advection;
219 # advective: an unsteady flow whose advection changes in time.
220 parser.add_argument(
221 "--manufactured",
222 choices=sorted(MANUFACTURED_SOLUTIONS),
223 default="curlcurl",
224 )
225 args = parser.parse_args()
226 args.solver = args.solver.resolve()
227 args.session = args.session.resolve()
228
229 failed = False
230 with tempfile.TemporaryDirectory(prefix="incns_hopbc_time_order_") as tmp:
231 workdir = pathlib.Path(tmp)
232 cases = [
233 (order, formulation, pressure_form)
234 for order in args.orders
235 for formulation in args.formulations
236 for pressure_form in args.pressure_forms
237 ]
238 for order, formulation, pressure_form in cases:
239 label = f"order={order} {formulation} {pressure_form}"
240 errors = [
241 run_case(
242 args, workdir, formulation, pressure_form, order, nsteps
243 )
244 for nsteps in args.steps
245 ]
246 rates = {
247 variable: [
248 convergence_rate(
249 errors[i - 1][variable],
250 errors[i][variable],
251 args.steps[i - 1],
252 args.steps[i],
253 )
254 for i in range(1, len(errors))
255 ]
256 for variable in VARIABLES
257 }
258 print(
259 f"{label}: errors={errors} rates={rates}",
260 flush=True,
261 )
262
263 # Pressure directly exercises the extrapolated higher-order
264 # Neumann data; velocity verifies its coupling through the
265 # projection.
266 min_rate = (
267 MIN_RATES[order] if args.min_rate is None else args.min_rate
268 )
269 min_pressure_rate = (
270 min_rate
271 if args.min_pressure_rate is None
272 else args.min_pressure_rate
273 )
274 for variable in VARIABLES:
275 expected = min_pressure_rate if variable == "p" else min_rate
276 observed = rates[variable][-1]
277 if observed < expected:
278 print(
279 f"{label}: expected final rate of {variable} >= "
280 f"{expected}, got {observed}",
281 flush=True,
282 )
283 failed = True
284
285 if failed:
286 raise SystemExit(1)
287
288
Definition main.py:1

References convergence_rate(), and run_case().

◆ run_case()

check_hopbc_time_order.run_case (   args,
  workdir,
  formulation,
  pressure_form,
  order,
  nsteps 
)

Definition at line 140 of file check_hopbc_time_order.py.

140def run_case(args, workdir, formulation, pressure_form, order, nsteps):
141 session = build_session(
142 args, workdir, formulation, pressure_form, order, nsteps
143 )
144 command = [
145 str(args.solver),
146 f"--opExecSpace={args.op_exec_space}",
147 f"--opImpl={args.op_impl}",
148 "-I",
149 f"Formulation={formulation}",
150 "-I",
151 f"PressureFormulation={pressure_form}",
152 str(session),
153 ]
154 result = subprocess.run(
155 command,
156 cwd=workdir,
157 text=True,
158 stdout=subprocess.PIPE,
159 stderr=subprocess.PIPE,
160 timeout=args.timeout,
161 )
162 if result.returncode:
163 print(result.stdout)
164 print(result.stderr)
165 raise RuntimeError(
166 f"order={order} {formulation} {pressure_form} nsteps={nsteps} "
167 "failed"
168 )
169
170 errors = {}
171 for variable in VARIABLES:
172 match = re.search(
173 rf"L 2 error \‍(variable {variable}\‍) : ([0-9.eE+-]+)",
174 result.stdout,
175 )
176 if not match:
177 print(result.stdout)
178 print(result.stderr)
179 raise RuntimeError(f"missing L2 error for variable {variable}")
180 errors[variable] = float(match.group(1))
181 return errors
182
183

References build_session().

Referenced by main().

◆ set_function()

check_hopbc_time_order.set_function (   root,
  function_name,
  values 
)

Definition at line 94 of file check_hopbc_time_order.py.

94def set_function(root, function_name, values):
95 function = root.find(f".//FUNCTION[@NAME='{function_name}']")
96 if function is None:
97 raise RuntimeError(f"session does not define function {function_name}")
98 for expression in function.findall("E"):
99 variable = expression.attrib["VAR"]
100 if variable in values:
101 expression.set("VALUE", values[variable])
102
103

Referenced by build_session().

◆ set_parameter()

check_hopbc_time_order.set_parameter (   root,
  name,
  value 
)

Definition at line 85 of file check_hopbc_time_order.py.

85def set_parameter(root, name, value):
86 for parameter in root.findall(".//PARAMETERS/P"):
87 lhs, separator, _ = (parameter.text or "").partition("=")
88 if separator and lhs.strip() == name:
89 parameter.text = f" {name} = {value} "
90 return
91 raise RuntimeError(f"session does not define parameter {name}")
92
93

Referenced by build_session().

Variable Documentation

◆ DEFAULT_STEPS

list check_hopbc_time_order.DEFAULT_STEPS = [8, 16, 32]

Definition at line 15 of file check_hopbc_time_order.py.

◆ MANUFACTURED_SOLUTIONS

dict check_hopbc_time_order.MANUFACTURED_SOLUTIONS
Initial value:
1= {
2 "curlcurl": curl_curl_expressions,
3 "advective": advective_expressions,
4}

Definition at line 79 of file check_hopbc_time_order.py.

◆ MIN_RATES

dict check_hopbc_time_order.MIN_RATES = {2: 1.7, 3: 2.5}

Definition at line 12 of file check_hopbc_time_order.py.

◆ VARIABLES

list check_hopbc_time_order.VARIABLES = ["u", "v", "p"]

Definition at line 16 of file check_hopbc_time_order.py.