Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial pathlib port #233

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/precice-aste-join
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import argparse
import json
import logging
import os
import os.path
import pathlib

import vtk

Expand Down Expand Up @@ -54,6 +54,7 @@ class MeshJoiner:
"--recovery",
dest="recovery",
help="The path to the recovery file to fully recover it's state.",
type=pathlib.Path,
)
parser.add_argument(
"--numparts",
Expand All @@ -68,6 +69,7 @@ class MeshJoiner:
dest="directory",
default=None,
help="Directory for output files (optional)",
type=pathlib.Path,
)
parser.add_argument(
"--log",
Expand Down Expand Up @@ -110,10 +112,17 @@ class MeshJoiner:
num_points = joined_mesh.GetNumberOfPoints()
num_cells = joined_mesh.GetNumberOfCells()
logger.info(f"Final mesh contains {num_points} points, {num_cells} cells")
MeshJoiner.write_mesh(joined_mesh, out_meshname, args.directory)

filename = (
pathlib.Path(args.directory, out_meshname)
if args.directory
else pathlib.Path(out_meshname)
)

MeshJoiner.write_mesh(joined_mesh, filename)

@staticmethod
def read_meshes(prefix: str, partitions=None, recovery_path=None):
def read_meshes(prefix: str, partitions: int, recovery_path: pathlib.Path):
"""
Reads meshes with given prefix.
"""
Expand All @@ -126,7 +135,7 @@ class MeshJoiner:
if partitions == 0:
raise PartitionError("No partitions found")

if os.path.exists(recovery_path):
if recovery_path.is_file():
logger.info("Recovery data found. Full recovery will be executed")
return MeshJoiner.join_mesh_recovery(prefix, partitions, recovery_path)
else:
Expand Down Expand Up @@ -213,12 +222,14 @@ class MeshJoiner:
return joined_mesh

@staticmethod
def join_mesh_recovery(prefix: str, partitions: int, recovery_path: str):
def join_mesh_recovery(prefix: str, partitions: int, recovery_path: pathlib.Path):
"""
Partition merge with full recovery

This recovers the original mesh.
"""
assert recovery_path.is_file()

logger = MeshJoiner.get_logger()
logger.info("Starting full mesh recovery")
recovery = json.load(open(recovery_path, "r"))
Expand Down Expand Up @@ -335,29 +346,24 @@ class MeshJoiner:
"""
detected = 0
while True:
partition_file = prefix + "_" + str(detected) + ".vtu"
if not os.path.isfile(partition_file):
partition_file = pathlib.Path(f"{prefix}_{detected}.vtu")
if not partition_file.is_file():
break
detected += 1
return detected

@staticmethod
def write_mesh(meshfile, filename, directory=None):

filename = os.path.basename(os.path.normpath(filename))
if directory:
directory = os.path.abspath(directory)
os.makedirs(directory, exist_ok=True)
filename = os.path.join(directory, filename)
def write_mesh(meshfile, filename: pathlib.Path):
filename.parent.mkdir(exist_ok=True, parents=True)

extension = os.path.splitext(filename)[1]
extension = filename.suffix
if extension == ".vtk": # VTK Legacy format
writer = vtk.vtkUnstructuredGridWriter()
writer.SetFileTypeToBinary()
elif extension == ".vtu": # VTK XML Unstructured Grid format
writer = vtk.vtkXMLUnstructuredGridWriter()
else:
raise ExtensionError("Unknown File extension: " + extension)
raise ExtensionError(f"Unknown File extension: {extension}")
writer.SetFileName(filename)
writer.SetInputData(meshfile)
writer.Write()
Expand Down
56 changes: 31 additions & 25 deletions tools/mapping-tester/gatherstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import argparse
import csv
import glob
import json
import os
import pathlib
import subprocess
from concurrent.futures import ThreadPoolExecutor

Expand All @@ -16,6 +16,7 @@ def parseArguments(args):
"--outdir",
default="cases",
help="Directory to generate the test suite in.",
type=pathlib.Path,
)
parser.add_argument(
"-f",
Expand All @@ -36,14 +37,17 @@ def run_checked(args):
r.check_returncode()


def timingStats(dir):
assert os.path.isdir(dir)
def timingStats(dir: pathlib.Path):
assert dir.is_dir()
assert (
os.system("command -v precice-profiling > /dev/null") == 0
), 'Could not find the profiling tool "precice-profiling", which is part of the preCICE installation.'
event_dir = os.path.join(dir, "precice-profiling")
json_file = os.path.join(dir, "profiling.json")
timings_file = os.path.join(dir, "timings.csv")
event_dir = dir / "precice-profiling"
json_file = dir / "profiling.json"
timings_file = dir / "timings.csv"

if not event_dir.is_dir():
return {}

try:
subprocess.run(
Expand All @@ -56,9 +60,8 @@ def timingStats(dir):
check=True,
capture_output=True,
)
file = timings_file
stats = {}
with open(file, "r") as csvfile:
with open(timings_file, "r") as csvfile:
timings = csv.reader(csvfile)
for row in timings:
if row[0] == "_GLOBAL":
Expand All @@ -84,13 +87,13 @@ def timingStats(dir):
return {}


def memoryStats(dir):
assert os.path.isdir(dir)
def memoryStats(dir: pathlib.Path):
assert dir.is_dir()
stats = {}
for P in "A", "B":
memfile = os.path.join(dir, f"memory-{P}.log")
memfile = dir / f"memory-{P}.log"
total = 0
if os.path.isfile(memfile):
if memfile.is_file():
try:
with open(memfile, "r") as file:
total = sum([float(e) / 1.0 for e in file.readlines()])
Expand All @@ -101,23 +104,22 @@ def memoryStats(dir):
return stats


def mappingStats(dir):
globber = os.path.join(dir, "*.stats.json")
statFiles = list(glob.iglob(globber))
if len(statFiles) == 0:
def mappingStats(dir: pathlib.Path):
statFiles = list(dir.glob("*.stats.json"))
if not statFiles:
return {}

statFile = statFiles[0]
assert os.path.exists(statFile)
assert statFile.is_file()
with open(statFile, "r") as jsonfile:
return dict(json.load(jsonfile))


def gatherCaseStats(casedir):
assert os.path.exists(casedir)
parts = os.path.normpath(casedir).split(os.sep)
assert len(parts) >= 5
mapping, constraint, meshes, ranks = parts[-4:]
def gatherCaseStats(casedir: pathlib.Path):
assert casedir.is_dir()
parts = [casedir.name] + [p.name for p in casedir.parents]
assert len(parts) >= 4
ranks, meshes, constraint, mapping = parts[:4]
meshA, meshB = meshes.split("-")
ranksA, ranksB = ranks.split("-")

Expand All @@ -138,12 +140,16 @@ def gatherCaseStats(casedir):
def main(argv):
args = parseArguments(argv[1:])

globber = os.path.join(args.outdir, "**", "done")
cases = [os.path.dirname(path) for path in glob.iglob(globber, recursive=True)]
cases = [d.parent for d in args.outdir.rglob("done")]

if not cases:
print(f"No cases found in {args.outdir.absolute()}")
return 1

allstats = []

def wrapper(case):
print("Found: " + os.path.relpath(case, args.outdir))
print(f"Found: {case.relative_to(args.outdir)}")
return gatherCaseStats(case)

with ThreadPoolExecutor() as pool:
Expand Down
Loading
Loading