Skip to content
Snippets Groups Projects
Commit 178cc601 authored by Tobias Meisel's avatar Tobias Meisel
Browse files

Merge branch 'small_tweaks' into 'main'

Small tweaks

See merge request ogs/tools/ogstools!113
parents 2186d090 03429a4b
No related branches found
No related tags found
No related merge requests found
import time
import warnings
from collections.abc import Sequence
from functools import partial
......@@ -79,7 +78,6 @@ def save_animation(anim: FuncAnimation, filename: str, fps: int) -> bool:
:param filename: the name of the resulting file
:param fps: the number of frames per second
"""
start_time = time.time()
print("Start saving animation...")
codec_args = (
"-crf 28 -preset ultrafast -pix_fmt yuv420p "
......@@ -102,8 +100,7 @@ def save_animation(anim: FuncAnimation, filename: str, fps: int) -> bool:
)
try:
anim.save(filename, writer=writer)
print("\ndone!")
print(f"Elapsed time: {(time.time() - start_time):.2f}")
print("Successful!")
return True
except Exception as err:
print("\nSaving Animation failed with the following error:")
......
File deleted
File deleted
......@@ -51,7 +51,10 @@ def get_levels(lower: float, upper: float, n_ticks: int) -> np.ndarray:
The length of the arrays will be close to n_ticks.
At the boundaries the tickspacing may differ from the remaining array.
"""
if np.abs(upper - lower) <= 1e-6:
return lower + np.array([0.0, 1e-6])
n_digits = 6
if lower * (1 - 10**-n_digits) <= upper <= lower * (1 + 10**-n_digits):
return lower + np.array([0.0, 10**-n_digits])
levels = nice_range(lower, upper, n_ticks)
return np.append(np.append(lower, adaptive_rounding(levels, 6)), upper)
return np.unique(
np.append(np.append(lower, adaptive_rounding(levels, n_digits)), upper)
)
......@@ -31,7 +31,7 @@ def plot_layer_boundaries(
x_b, y_b = setup.length.strip_units(
edges.points[edges.lines % edges.n_points].T[[x_id, y_id]]
)
lw = 0.5 * setup.rcParams_scaled["lines.linewidth"]
lw = setup.rcParams_scaled["lines.linewidth"]
ax.plot(x_b, y_b, "-k", lw=lw)
x_pos = 0.01 if mat_id % 2 == 0 else 0.99
ha = "left" if mat_id % 2 == 0 else "right"
......
......@@ -9,18 +9,12 @@ from dataclasses import dataclass, replace
from typing import Any, Callable, Union
import numpy as np
from pint import UnitRegistry
from pint.facets.plain import PlainQuantity
from .unit_registry import u_reg
from .utils import identity, sym_tensor_to_mat
from .vector2scalar import trace
u_reg: UnitRegistry = UnitRegistry(
preprocessors=[lambda s: s.replace("%", "percent")]
)
u_reg.default_format = "~.12g"
u_reg.setup_matplotlib(True)
# TODO: rename to BaseProperty?? / GenericProperty
@dataclass
......
from pint import UnitRegistry
u_reg: UnitRegistry = UnitRegistry(
preprocessors=[lambda s: s.replace("%", "percent")]
)
u_reg.default_format = "~.12g"
u_reg.setup_matplotlib(True)
......@@ -34,6 +34,8 @@ class MeshplotlibTest(unittest.TestCase):
equality(get_levels(1e-3, 1.2, 5), [1e-3, *np.arange(0.2, 1.4, 0.2)])
equality(get_levels(1e5, 9e6, 20), [1e5, *np.arange(5e5, 9.5e6, 5e5)])
equality(get_levels(1, 40, 20), [1, *range(2, 42, 2)])
equality(get_levels(0.0, 0.0, 10), [0.0, 1e-6])
equality(get_levels(1e9, 1e9, 10), [1e9, 1e9 + 1e-6])
def test_missing_data(self):
"""Test missing data in mesh."""
......
......@@ -11,7 +11,8 @@ from unittest.mock import patch
import meshio
from ogstools import meshlib, msh2vtu
from ogstools.meshlib import gmsh_meshing
from ogstools.msh2vtu import msh2vtu
from ogstools.msh2vtu._cli import cli
......@@ -24,14 +25,14 @@ def test_rect(tmp_path: Path):
msh_file = Path(tmp_path, "rect.msh")
permutations = product([1.0, 2.0], [1, 2], [True, False], [1, 2])
for edge_length, n_edge_cells, structured, order in permutations:
meshlib.gmsh_meshing.rect(
gmsh_meshing.rect(
lengths=edge_length,
n_edge_cells=n_edge_cells,
structured_grid=structured,
order=order,
out_name=msh_file,
)
assert msh2vtu.msh2vtu(msh_file, tmp_path, output_prefix="rect") == 0
assert msh2vtu(msh_file, tmp_path, output_prefix="rect") == 0
def test_cuboid(tmp_path: Path):
......@@ -39,14 +40,14 @@ def test_cuboid(tmp_path: Path):
msh_file = Path(tmp_path, "cuboid.msh")
permutations = product([1.0, 2.0], [1, 2], [True, False], [1, 2])
for edge_length, n_edge_cells, structured, order in permutations:
meshlib.gmsh_meshing.cuboid(
gmsh_meshing.cuboid(
lengths=edge_length,
n_edge_cells=n_edge_cells,
structured_grid=structured,
order=order,
out_name=msh_file,
)
assert msh2vtu.msh2vtu(msh_file, tmp_path, output_prefix="cuboid") == 0
assert msh2vtu(msh_file, tmp_path, output_prefix="cuboid") == 0
def test_gmsh(tmp_path: Path):
......@@ -60,7 +61,7 @@ def test_gmsh(tmp_path: Path):
runpy.run_module(f"ogstools.msh2vtu.examples.gmsh.{Path(script).stem}")
prefix = str(Path(script).stem)
msh_file = Path(tmp_path, prefix + ".msh")
assert msh2vtu.msh2vtu(msh_file, tmp_path, output_prefix=prefix) == 0
assert msh2vtu(msh_file, tmp_path, output_prefix=prefix) == 0
testargs = ["msh2vtu", str(msh_file), "-o", str(tmp_path), "-p", prefix]
with patch.object(sys, "argv", testargs):
cli()
......
......@@ -8,7 +8,9 @@ from tempfile import mkdtemp
import numpy as np
from ogs6py import ogs
from ogstools import meshlib, msh2vtu, propertylib
from ogstools.meshlib import MeshSeries, gmsh_meshing
from ogstools.msh2vtu import msh2vtu
from ogstools.propertylib import Scalar
from ogstools.studies import convergence
from ogstools.studies.convergence.examples import (
steady_state_diffusion_analytical_solution,
......@@ -24,12 +26,12 @@ class ConvergenceTest(unittest.TestCase):
edge_cells = [2**i for i in range(3, 6)]
for n_edge_cells in edge_cells:
msh_path = temp_dir / "square.msh"
meshlib.gmsh_meshing.rect(
gmsh_meshing.rect(
n_edge_cells=n_edge_cells,
structured_grid=True,
out_name=msh_path,
)
msh2vtu.msh2vtu(
msh2vtu(
input_filename=msh_path, output_path=temp_dir, log_level="ERROR"
)
model = ogs.OGS(
......@@ -42,13 +44,13 @@ class ConvergenceTest(unittest.TestCase):
ogs_args = f"-m {temp_dir} -o {temp_dir}"
model.run_model(write_logs=False, args=ogs_args)
sim_results += [
meshlib.MeshSeries(str(temp_dir / (prefix + ".pvd"))).read(-1)
MeshSeries(str(temp_dir / (prefix + ".pvd"))).read(-1)
]
topology = sim_results[-3]
spacing = convergence.add_grid_spacing(topology)["grid_spacing"]
np.testing.assert_array_less(0.0, spacing)
mesh_property = propertylib.Scalar("pressure", "m", "m")
mesh_property = Scalar("pressure", "m", "m")
conv = convergence.grid_convergence(
sim_results, mesh_property, topology, refinement_ratio=2.0
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment