Skip to content

Commit 19b6764

Browse files
committed
allow numbering for small fields
1 parent 7485861 commit 19b6764

File tree

2 files changed

+110
-58
lines changed

2 files changed

+110
-58
lines changed

contrib/perplex/create_phase_diagram.py

Lines changed: 74 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -45,67 +45,79 @@ def run_perplex(
4545
run_pssect(perplex_bindir, project_name, convert_to_pdf=False, verbose=False)
4646

4747

48+
# The following compositions are from Xu et al. (2008)
49+
# (https://doi.org/10.1016/j.epsl.2008.08.012)
50+
compositions = {
51+
"pyrolite": burnman.Composition(
52+
{
53+
"SiO2": 38.71,
54+
"MgO": 49.85,
55+
"FeO": 6.17,
56+
"CaO": 2.94,
57+
"Al2O3": 2.22,
58+
"Na2O": 0.11,
59+
},
60+
"molar",
61+
),
62+
"basalt": burnman.Composition(
63+
{
64+
"SiO2": 51.75,
65+
"MgO": 14.94,
66+
"FeO": 7.06,
67+
"CaO": 13.88,
68+
"Al2O3": 10.19,
69+
"Na2O": 2.18,
70+
},
71+
"molar",
72+
),
73+
"harzburgite": burnman.Composition(
74+
{
75+
"SiO2": 36.07,
76+
"MgO": 56.51,
77+
"FeO": 6.07,
78+
"CaO": 0.81,
79+
"Al2O3": 0.53,
80+
"Na2O": 0.001,
81+
},
82+
"molar",
83+
),
84+
"modified_harzburgite": burnman.Composition(
85+
{
86+
"SiO2": 36.04,
87+
"MgO": 56.54,
88+
"FeO": 5.97,
89+
"CaO": 0.79,
90+
"Al2O3": 0.65,
91+
"Na2O": 0.001,
92+
},
93+
"molar",
94+
),
95+
}
96+
97+
4898
if __name__ == "__main__":
4999
# Define the project parameters
50-
compositions = {
51-
"pyrolite": burnman.Composition(
52-
{
53-
"SiO2": 38.71,
54-
"MgO": 49.85,
55-
"FeO": 6.17,
56-
"CaO": 2.94,
57-
"Al2O3": 2.22,
58-
"Na2O": 0.11,
59-
},
60-
"molar",
61-
),
62-
"basalt": burnman.Composition(
63-
{
64-
"SiO2": 51.75,
65-
"MgO": 14.94,
66-
"FeO": 7.06,
67-
"CaO": 13.88,
68-
"Al2O3": 10.19,
69-
"Na2O": 2.18,
70-
},
71-
"molar",
72-
),
73-
"harzburgite": burnman.Composition(
74-
{
75-
"SiO2": 36.07,
76-
"MgO": 56.51,
77-
"FeO": 6.07,
78-
"CaO": 0.81,
79-
"Al2O3": 0.53,
80-
"Na2O": 0.001,
81-
},
82-
"molar",
83-
),
84-
"modified_harzburgite": burnman.Composition(
85-
{
86-
"SiO2": 36.04,
87-
"MgO": 56.54,
88-
"FeO": 5.97,
89-
"CaO": 0.79,
90-
"Al2O3": 0.65,
91-
"Na2O": 0.001,
92-
},
93-
"molar",
94-
),
95-
}
96100
project_name = "basalt"
97101
database = databases["stx21"]
98102
composition = compositions[project_name]
99103
pressure_range_total = [1.0e5, 140.0e9]
100104
temperature_range_total = [200.0, 3000.0]
101-
n_pressures_per_split = 241
102-
n_temperatures_per_split = 201
103-
n_splits_pressure = 7
104-
n_splits_temperature = 3
105105

106-
create_plots = True
107-
outfile_base = f"{project_name}_table"
106+
# Split pressure and temperature so that PerpleX
107+
# no temperature splits seems to make diagram with
108+
# less prominent discontinuities
109+
n_pressures_per_split = 121
110+
n_temperatures_per_split = 601
111+
n_splits_pressure = 14
112+
n_splits_temperature = 1
113+
114+
# If this script has already been run, and you just want to
115+
# tweak the figure, perplex should not be run again.
116+
perplex_should_be_run = True
108117

118+
# End of project definitions
119+
120+
outfile_base = f"{project_name}_table"
109121
perplex_dir = os.path.join(os.getcwd(), "perplex-installer/Perple_X")
110122
perplex_bindir = os.path.join(os.getcwd(), "perplex-installer/Perple_X/bin")
111123

@@ -114,7 +126,7 @@ def run_perplex(
114126
temperature_range_total[1] - temperature_range_total[0]
115127
) / n_splits_temperature
116128

117-
if create_plots:
129+
if perplex_should_be_run:
118130
# Check if the project directory already exists
119131
# If it does, delete it to start fresh.
120132
try:
@@ -249,19 +261,26 @@ def run_perplex(
249261
fig = plt.figure(figsize=(12, 8))
250262
ax = fig.add_subplot(1, 1, 1)
251263

252-
pretty_plot_phase_diagram(
264+
small_fields = pretty_plot_phase_diagram(
253265
ax,
254266
modes_filenames,
255267
phase_name_replacements,
256268
bounding_colors,
269+
n_phases_bounds=[2.0, 7.0],
257270
smoothing_window=8,
258271
smoothing_order=1,
259272
linewidth=0.5,
260273
label_scaling=3.0,
261274
label_clearance=0.01,
262-
n_phases_bounds=[2.0, 7.0],
275+
number_small_fields=True,
263276
)
264277

278+
with open(f"{project_name}_field_ids.txt", "w") as f:
279+
for i, small_field in enumerate(small_fields):
280+
line = f"{i+1}: {small_field}"
281+
print(line)
282+
f.write(f"{line}\n")
283+
265284
fig.savefig(f"{project_name}.pdf")
266285
plt.show()
267286

contrib/perplex/perplex_utils.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,12 +1060,13 @@ def pretty_plot_phase_diagram(
10601060
werami_mode_tab_filenames,
10611061
phase_name_replacements,
10621062
bounding_colors=["#44015a", "#ffffff"],
1063+
n_phases_bounds=[2, 10],
10631064
smoothing_window=4,
10641065
smoothing_order=1,
10651066
linewidth=0.5,
10661067
label_scaling=3.0,
10671068
label_clearance=0.01,
1068-
n_phases_bounds=[2, 10],
1069+
number_small_fields=True,
10691070
):
10701071
"""
10711072
Plot the Perple_X calculated phase diagram on a matplotlib axis.
@@ -1078,17 +1079,26 @@ def pretty_plot_phase_diagram(
10781079
:type phase_name_replacements: dict[str, str]
10791080
:param bounding_colors: List of two colors for bounding colormap.
10801081
:type bounding_colors: [str, str]
1082+
:param n_phases_bounds: Minimum and maximum number of phases for the colormap.
1083+
:type n_phases_bounds: [int, int]
10811084
:param smoothing_window: Savitzky-Golay smoothing window length.
10821085
:type smoothing_window: int
10831086
:param smoothing_order: Savitzky-Golay smoothing polynomial order.
10841087
:type smoothing_order: int
1088+
:param linewidth: Linewidth for the edges of fields.
1089+
:type linewidth: float
10851090
:param label_scaling: Scaling factor corresponding to an
10861091
approximate value of the width:height ratio of the label.
10871092
:type label_scaling: float
10881093
:param label_clearance: Only plot the label if there is a certain
1089-
clearance to the edge of the field. Scaled to the height of the domain.
1094+
clearance between the center and the edge of the field.
1095+
Scaled to the height of the domain.
10901096
:type label_clearance: float
1091-
:return: None
1097+
:param number_small_fields: Replace assemblage labels with numbers if
1098+
there is not enough clearance around the label.
1099+
:type number_small_fields: bool
1100+
:return: List of assemblages corresponding to numbered small fields.
1101+
:rtype: [str]
10921102
"""
10931103

10941104
polygon_data = []
@@ -1131,6 +1141,8 @@ def pretty_plot_phase_diagram(
11311141
# ScalarMappable for colorbar
11321142
sm = ScalarMappable(cmap=cmap, norm=norm)
11331143

1144+
small_fields = []
1145+
large_fields = set()
11341146
for polygon in polygon_data:
11351147

11361148
color = cmap(norm(polygon["n_phases"]))
@@ -1151,8 +1163,27 @@ def pretty_plot_phase_diagram(
11511163
label_pos, label_dist = get_label_position_from_polygon_contour(
11521164
contour, P_range, T_range, label_scaling
11531165
)
1166+
11541167
if label_dist > label_clearance:
1168+
large_fields.add(polygon["label"])
11551169
ax.text(*label_pos, polygon["label"], fontsize=6, ha="center", va="center")
1170+
elif number_small_fields:
1171+
if len(polygon["edges"]) > 0:
1172+
small_fields.append([label_pos, polygon["label"]])
1173+
1174+
unique_ids = {}
1175+
unique_small_fields = []
1176+
i = 1
1177+
for _, label in small_fields:
1178+
if label not in unique_ids.keys() and label not in large_fields:
1179+
unique_ids[label] = i
1180+
unique_small_fields.append(label)
1181+
i = i + 1
1182+
1183+
for label_pos, label in small_fields:
1184+
ax.text(
1185+
*label_pos, f"{unique_ids[label]:02d}", fontsize=6, ha="center", va="center"
1186+
)
11561187

11571188
cbar = plt.colorbar(sm, ax=ax, boundaries=boundaries, ticks=n_phases_range)
11581189
cbar.set_label("n phases")
@@ -1164,3 +1195,5 @@ def pretty_plot_phase_diagram(
11641195
ax.set_ylim(bounds[1])
11651196
ax.grid(True, linestyle="--", color="gray", alpha=0.7)
11661197
ax.set_axisbelow(False)
1198+
1199+
return unique_small_fields

0 commit comments

Comments
 (0)