-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlot_Unstrucuted.py
186 lines (145 loc) · 5.59 KB
/
Plot_Unstrucuted.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import matplotlib.pyplot as plt
import os
import sys
import random
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.tri import Triangulation
import flopy
import matplotlib.patches as patches
from matplotlib.collections import PatchCollection
import matplotlib.tri as mtri
# Simple functions to load vertices and incidence lists
def load_verts(fname):
return(np.genfromtxt(fname))
def load_iverts(fname):
f = open(fname, 'r')
iverts = []
xc = []
yc = []
for line in f:
ll = line.strip().split()
iverts.append([int(i) - 1 for i in ll[4:]])
xc.append(float(ll[1]))
yc.append(float(ll[2]))
return iverts, np.array(xc), np.array(yc)
def plot_head_distribution(xc, yc, iverts, reshaped_head):
# Create a figure and axis
fig, ax = plt.subplots()
# Create a list of polygons for each cell
polygons = []
for cell_verts in iverts:
if len(cell_verts) >= 3: # Ensure there are at least 3 vertices for a cell
cell_coords = np.array([[xc[i], yc[i]] for i in cell_verts])
polygon = patches.Polygon(cell_coords, closed=True)
polygons.append(polygon)
# Create a PatchCollection with the polygons and assign head values
collection = PatchCollection(polygons, cmap='viridis', edgecolor='0.5')
collection.set_array(reshaped_head)
# Add the PatchCollection to the axis
ax.add_collection(collection)
# Add colorbar
cbar = plt.colorbar(collection, ax=ax, label='Head')
# Set equal aspect ratio and labels
ax.set_aspect('equal')
ax.set_xlabel('X Coordinate')
ax.set_ylabel('Y Coordinate')
# Show the plot
plt.title('Head Distribution')
plt.show()
print(sys.version)
print('numpy version: {}'.format(np.__version__))
print('matplotlib version: {}'.format(mpl.__version__))
print('flopy version: {}'.format(flopy.__version__))
# This is a folder containing some unstructured grids
absolute_path = os.path.dirname(__file__)
datapth = os.path.join(absolute_path, 'data', 'unstructured')
absolute_path = os.path.dirname(__file__)
inter_path = "/NeckartalModel1718/NeckartalCalib_try_models/MODFLOW 6"
ens_path = "/NeckartalModel1718/NeckartalCalib_try_models/MODFLOW 6/ensemble"
model_path = absolute_path + inter_path + "/sim"
sim_orig = flopy.mf6.modflow.MFSimulation.load(
# mname,
version = 'mf6',
exe_name = 'mf6',
sim_ws = model_path,
verbosity_level = 0
)
model = sim_orig.get_model()
model.npf.save_specific_discharge = True
sim_orig.run_simulation()
head = model.output.head().get_data()
reshaped_head = head.flatten()
# load vertices
fname = os.path.join("/home/janek/Documents/Python/NeckarDISU/data/unstructured", 'ugrid_verts.dat')
verts = load_verts(fname)[:, 1:]
# load the incidence list into iverts
fname = os.path.join(datapth, 'ugrid_iverts.dat')
iverts, xc, yc = load_iverts(fname)
print("Length of reshaped_head:", len(reshaped_head))
print("Length of xc:", len(xc))
print("Length of yc:", len(yc))
# Convert iverts to a list of triangles
triangles = []
for cell_verts in iverts:
if len(cell_verts) >= 3: # Ensure there are at least 3 vertices for a triangle
for i in range(1, len(cell_verts) - 1):
triangles.append([cell_verts[0], cell_verts[i], cell_verts[i + 1]])
# Check triangles
print("Number of triangles:", len(triangles))
# Create a triangulation
triang = Triangulation(xc, yc, triangles=triangles)
reshaped_head[reshaped_head > 500] = np.nan
# Create a scatter plot with the head values
# plt.figure()
# plt.tripcolor(triang, reshaped_head, shading='flat', cmap='viridis')
# plt.colorbar(label='Head')
# plt.gca().set_aspect('equal')
# plt.xlabel('X Coordinate')
# plt.ylabel('Y Coordinate')
# plt.title('Head Distribution')
# plt.show()
# Call the plotting function
plot_head_distribution(xc, yc, iverts, reshaped_head)
# mm = flopy.plot.map.PlotMapView(model=model)
# mm.plot_array(reshaped_head, edgecolor='0.5')
# mm.plot_grid()
# cs = mm.contour_array(head)
# mm.ax.clabel(cs)
# # mm.plot_vector(qx, qy, normalize=True)
# plt.show()
# Simple functions to load vertices and incidence lists
# def load_verts(fname):
# return(np.genfromtxt(fname))
# def load_iverts(fname):
# f = open(fname, 'r')
# iverts = []
# xc = []
# yc = []
# for line in f:
# ll = line.strip().split()
# iverts.append([int(i) - 1 for i in ll[4:]])
# xc.append(float(ll[1]))
# yc.append(float(ll[2]))
# return iverts, np.array(xc), np.array(yc)
# # load vertices
# fname = os.path.join("/home/janek/Documents/Python/NeckarDISU/data/unstructured", 'ugrid_verts.dat')
# verts = load_verts(fname)[:, 1:]
# # load the incidence list into iverts
# fname = os.path.join(datapth, 'ugrid_iverts.dat')
# iverts, xc, yc = load_iverts(fname)
# # Print the first 5 entries in verts and iverts
# for ivert, v in enumerate(verts[:5]):
# print('Vertex coordinate pair for vertex {}: {}'.format(ivert, v))
# print('...\n')
# for icell, vertlist in enumerate(iverts[:5]):
# print('List of vertices for cell {}: {}'.format(icell, vertlist))
# ncpl = np.array(5 * [len(iverts)])
# sr = flopy.utils.reference.SpatialReferenceUnstructured(xc, yc, verts, iverts, ncpl)
# print(ncpl)
# print(sr)
# print("Length of xc:", len(xc))
# print("Length of yc:", len(yc))
# print("Length of verts:", len(verts))
# print("Length of iverts:", len(iverts))