Skip to content

Commit

Permalink
Add a quiet flag for cashocs-convert
Browse files Browse the repository at this point in the history
  • Loading branch information
sblauth committed Nov 2, 2022
1 parent db0c296 commit fc86748
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
15 changes: 10 additions & 5 deletions cashocs/_cli/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import meshio
import numpy as np

from cashocs import _loggers


def _generate_parser() -> argparse.ArgumentParser:
"""Returns a parser for command line arguments."""
Expand All @@ -44,6 +46,7 @@ def _generate_parser() -> argparse.ArgumentParser:
default=None,
metavar="outfile",
)
parser.add_argument("-q", "--quiet", action="store_true")

return parser

Expand Down Expand Up @@ -213,6 +216,8 @@ def convert(argv: Optional[List[str]] = None) -> None:
outputfile = f"{inputfile[:-4]}.xdmf"
check_file_extension(outputfile, "xdmf")

quiet = args.quiet

ostring = outputfile.rsplit(".", 1)[0]

mesh_collection = meshio.read(inputfile)
Expand All @@ -239,11 +244,11 @@ def convert(argv: Optional[List[str]] = None) -> None:
check_for_physical_names(inputfile, topological_dimension, ostring)

end_time = time.time()
print(
f"cashocs - info: Successfully converted {inputfile} to {outputfile} "
f"in {end_time - start_time:.2f} s",
flush=True,
)
if not quiet:
_loggers.info(
f"Successfully converted {inputfile} to {outputfile} "
f"in {end_time - start_time:.2f} s"
)


if __name__ == "__main__":
Expand Down
17 changes: 12 additions & 5 deletions cashocs/io/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,21 +289,28 @@ def import_mesh(input_arg: Union[str, config_module.Config]) -> _typing.MeshTupl
return mesh, subdomains, boundaries, dx, ds, d_interior_facet


def convert(input_file: str, output_file: Optional[str] = None) -> None:
def convert(
input_file: str, output_file: Optional[str] = None, quiet: bool = False
) -> None:
"""Converts the input mesh file to a xdmf mesh file for cashocs to work with.
Args:
input_file: A gmsh .msh file.
output_file: The name of the output .xdmf file or ``None``. If this is ``None``,
then a file name.msh will be converted to name.xdmf, i.e., the name of the
input file stays the same
quiet: A boolean flag which silences the output.
"""
if fenics.MPI.rank(fenics.MPI.comm_world) == 0:
if output_file is None:
cli_convert([input_file])
else:
cli_convert([input_file, "-o", output_file])
args = [input_file]

if output_file is not None:
args += ["-o", output_file]
if quiet:
args += ["-q"]

cli_convert(args)

fenics.MPI.barrier(fenics.MPI.comm_world)

Expand Down

0 comments on commit fc86748

Please sign in to comment.