-
Notifications
You must be signed in to change notification settings - Fork 0
/
fit.py
128 lines (100 loc) · 3.01 KB
/
fit.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
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/>.
"""
import argparse
import matplotlib.pyplot as plt
import numpy as np
import os
import sys
from subprocess import run, DEVNULL
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'
)
datafolder = 'data'
pdffolder = 'pdf'
graphfolder = 'graphs'
results = 'output.csv'
pdf = 'output.pdf'
pdf2 = 'result.pdf'
graph = 'graph-begin.dot'
graphend = 'graph-end.dot'
os.makedirs(datafolder, exist_ok=True)
os.makedirs(pdffolder, exist_ok=True)
os.makedirs(graphfolder, exist_ok=True)
exename = 'fracture'
if sys.platform == 'win32':
exename += '.exe'
exe = os.path.join('out', exename)
plt.style.use('ggplot')
args = opt.parse_args();
G = args.geometry
L = args.length
D = args.disorder
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):
run(f'{exe} {L} {D} {G}')
if args.graph:
begin_path = os.path.join(pdffolder, str(D) + pdf)
run(f'dot {graph} -Tpdf -o{begin_path}')
os.remove(graph)
run(f'start {begin_path}', shell=True)
end_path = os.path.join(pdffolder, str(D) + pdf2)
run(f'dot {graphend} -Tpdf -o{end_path}')
os.remove(graphend)
run(f'start {end_path}', shell=True)
result_path = os.path.join(datafolder, f'{L}.{G}.{D}-{r}.csv')
os.replace(results, 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')
fig_path = os.path.join(graphfolder, f'{G}.{L}.png')
plt.savefig(fig_path, bbox_inches='tight')
fig_path2 = os.path.join(datafolder, f'{G}.{L}.{D}.png')
plt.savefig(fig_path2, bbox_inches='tight')
if not args.noshow:
plt.show()