Repository navigation
Expand file tree
/
Copy pathfit.py
More file actions
105 lines (77 loc) · 3.1 KB
/
Copy pathfit.py
File metadata and controls
105 lines (77 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
fracture: Simulation of a scalar fracture model based on Freitas (2007).
Copyright (C) 2017 Italo Silva
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from __future__ import annotations
import argparse
import subprocess
import sys
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
EXE = Path("out/fracture")
DATA_DIR = Path("data")
PDF_DIR = Path("pdf")
GRAPH_DIR = Path("graphs")
RESULTS_FILE = Path("output.csv")
GRAPH_BEGIN = Path("graph-begin.dot")
GRAPH_END = Path("graph-end.dot")
OPENER = "open" if sys.platform == "darwin" else "xdg-open"
def open_file(path: Path) -> None:
subprocess.run([OPENER, str(path)])
def render_graph(dot_file: Path, pdf_path: Path) -> None:
subprocess.run(["dot", str(dot_file), "-Tpdf", f"-o{pdf_path}"])
dot_file.unlink()
open_file(pdf_path)
def parse_args() -> argparse.Namespace:
opt = argparse.ArgumentParser(
description="Execute fracture simulations.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
opt.add_argument("-g", "--geometry", choices=["t", "h", "s"], default="t")
opt.add_argument("-l", "--length", type=int, default=14)
opt.add_argument("-c", "--count", type=int, default=10)
opt.add_argument("-d", "--disorder", type=float, default=0)
opt.add_argument("--noshow", action="store_true")
opt.add_argument("--graph", action="store_true")
return opt.parse_args()
def main() -> None:
args = parse_args()
G: str = args.geometry
L: int = args.length
D: float = args.disorder
DATA_DIR.mkdir(exist_ok=True)
PDF_DIR.mkdir(exist_ok=True)
GRAPH_DIR.mkdir(exist_ok=True)
plt.style.use("ggplot")
print()
plt.figure()
plt.suptitle(f"L = {L}, G = {G}, D = {D}")
color_map = plt.get_cmap("gist_rainbow")
for r in range(args.count):
subprocess.run([str(EXE), str(L), str(D), G])
if args.graph:
render_graph(GRAPH_BEGIN, PDF_DIR / f"{D}output.pdf")
render_graph(GRAPH_END, PDF_DIR / f"{D}result.pdf")
result_path = DATA_DIR / f"{L}.{G}.{D}-{r}.csv"
RESULTS_FILE.replace(result_path)
out = np.genfromtxt(result_path, delimiter=",", skip_header=1, names=["V", "I"])
lines = plt.plot(out["V"], out["I"], label=f"{r}")
lines[0].set_color(color_map(r / args.count))
plt.legend(loc="upper left")
plt.savefig(GRAPH_DIR / f"{G}.{L}.png", bbox_inches="tight")
plt.savefig(DATA_DIR / f"{G}.{L}.{D}.png", bbox_inches="tight")
if not args.noshow:
plt.show()
if __name__ == "__main__":
main()