-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix gradient in turbine example (#38)
Closes #37. While #36 re-enables the turbines in the turbine example, there's still something not right because the optimisation progress is non-monotonic and the control turbine area occasionally goes to zero. I added a Taylor test and found that the gradient of the QoI wasn't being computed correctly with the existing setup. I tracked the problem down to the `turbine_density` expression, which includes several terms. By projecting this expression into $\mathbb{P}1_{DG}$ space, the Taylor test passed. In addition, this PR overhauls the plotting functionality.
- Loading branch information
1 parent
8f29976
commit 20f2026
Showing
7 changed files
with
163 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ go: | |
done | ||
|
||
plot: | ||
python3 plot_qoi.py | ||
python3 plot_results.py | ||
|
||
clean: | ||
rm *.log |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import glob | ||
from warnings import warn | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
from thetis import create_directory | ||
|
||
create_directory("plots") | ||
|
||
data_dict = { | ||
"uniform": { | ||
"label": "Uniform meshing", | ||
}, | ||
"hessian": { | ||
"label": "Hessian-based", | ||
}, | ||
# "go": { | ||
# "label": "Goal-oriented", | ||
# }, | ||
} | ||
|
||
# Load data from files | ||
variables = ("nc", "J", "t", "m") | ||
for method, data in data_dict.items(): | ||
for variable in variables: | ||
data[variable] = [] | ||
for fname in glob.glob(f"data/{method}_*.log"): | ||
ext = "_".join(fname.split("_")[1:])[:-4] | ||
for variable in variables: | ||
fname = f"data/{method}_progress_{variable}_{ext}.npy" | ||
try: | ||
value = np.load(fname)[-1] | ||
except (FileNotFoundError, IndexError): | ||
print(f"Can't load {fname}") | ||
continue | ||
if variable == "J": | ||
descaled = -value / 10000 | ||
kw = descaled / 1000 | ||
if kw <= 0.0: | ||
warn(f"Negative power from {fname}", stacklevel=1) | ||
kw = np.nan | ||
data[variable].append(kw) | ||
elif variable == "m" and (value <= 0.0 or value >= 500.0): | ||
warn(f"Control out of bounds from {fname}", stacklevel=1) | ||
data[variable].append(np.nan) | ||
else: | ||
data[variable].append(value) | ||
|
||
metadata = { | ||
"J": {"label": "qoi", "name": r"Power output ($\mathrm{kW}$)"}, | ||
"nc": {"label": "elements", "name": "Number of mesh elements"}, | ||
"t": {"label": "time", "name": r"CPU time ($\mathrm{s}$)"}, | ||
"m": {"label": "control", "name": r"Control ($\mathrm{m}$)"}, | ||
} | ||
|
||
|
||
def plot(v1, v2): | ||
fig, axes = plt.subplots(figsize=(5, 3)) | ||
for data in data_dict.values(): | ||
axes.semilogx(data[v1], data[v2], "x", label=data["label"]) | ||
axes.set_xlabel(metadata[v1]["name"]) | ||
axes.set_ylabel(metadata[v2]["name"]) | ||
axes.grid(True, which="both") | ||
axes.legend() | ||
plt.tight_layout() | ||
fname = f"converged_{metadata[v2]['label']}_vs_{metadata[v1]['label']}" | ||
for ext in ("pdf", "jpg"): | ||
plt.savefig(f"plots/{fname}.{ext}") | ||
|
||
|
||
plot("nc", "J") | ||
plot("t", "J") | ||
plot("nc", "m") | ||
plot("t", "m") |
Oops, something went wrong.