-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path02_geomodel_cosim.py
More file actions
320 lines (256 loc) · 9.92 KB
/
02_geomodel_cosim.py
File metadata and controls
320 lines (256 loc) · 9.92 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"""
Advanced Geomodeling: Co-simulation, SGS & Variograms
Features demonstrated:
1. Porosity and Sand Volume co-simulation using FFT-MA
2. Unconditional SGS simulation (Simple Kriging vs Ordinary Kriging)
3. Conditional SGS simulation (honoring well data)
4. Variogram model usage
"""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
# --- Figure style ---
import matplotlib
matplotlib.rcParams.update({
'figure.dpi': 150,
'savefig.dpi': 300,
'savefig.bbox': 'tight',
'savefig.pad_inches': 0.1,
'font.size': 11,
'axes.titlesize': 13,
'axes.labelsize': 11,
'axes.titleweight': 'semibold',
'xtick.labelsize': 10,
'ytick.labelsize': 10,
'legend.fontsize': 10,
'legend.framealpha': 0.9,
'figure.facecolor': 'white',
'axes.facecolor': '#fafafa',
'axes.edgecolor': '#cccccc',
'axes.linewidth': 0.8,
'grid.color': '#e0e0e0',
'grid.linewidth': 0.5,
'lines.linewidth': 1.5,
'image.cmap': 'viridis',
})
FIGS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'figs')
os.makedirs(FIGS_DIR, exist_ok=True)
import torch
import numpy as np
import matplotlib.pyplot as plt
from geobrain.geomodel import (
Simulator,
SimulationConfig,
CoSimConfig,
VariogramModel,
VariogramStructure,
)
from geobrain.data.transforms import sigmoid_transform
from geobrain.vis import plot_comparison
# Device setup
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
torch.manual_seed(2025)
print(f"Using device: {device}")
# =============================================================================
# Define Simulation Parameters
# =============================================================================
# Grid dimensions
NX, NY, NZ = 64, 64, 128
# Property ranges
phi_range = (0.05, 0.40) # Porosity range
vsand_range = (0.20, 0.80) # Sand volume range
# Target correlation between properties
target_corr = 0.7
# =============================================================================
# Configure Co-simulation
# =============================================================================
# Build correlation matrix
correlation_matrix = torch.tensor([
[1.0, target_corr],
[target_corr, 1.0]
])
# Configure co-simulation
config = CoSimConfig(
shape=(NX, NY, NZ),
lh=20.0, # Horizontal correlation length
lv=5.0, # Vertical correlation length
n_variables=2,
correlation_matrix=correlation_matrix,
field_names=["phi_raw", "vsand_raw"],
mean=torch.tensor([0.0, 0.0]),
std=torch.tensor([1.0, 1.0]),
seed=2025,
device=str(device),
)
print(f"Grid shape: {config.shape}")
print(f"Correlation length (H/V): {config.lh} / {config.lv}")
# =============================================================================
# Run Simulation
# =============================================================================
# Create simulator and generate fields
simulator = Simulator.create('fft_ma')
fields = simulator.simulate(config)
print(f"Generated fields: {list(fields.keys())}")
# =============================================================================
# Transform to Physical Properties
# =============================================================================
# Stack raw fields and transform to physical ranges
stacked = torch.stack([fields["phi_raw"], fields["vsand_raw"]])
transformed = sigmoid_transform(stacked, [phi_range, vsand_range])
# Extract transformed properties
phi = transformed[0] # Porosity
vsand = transformed[1] # Sand volume
# Print statistics
print(f"Porosity: mean={phi.mean():.4f}, range=[{phi.min():.4f}, {phi.max():.4f}]")
print(f"Sand Volume: mean={vsand.mean():.4f}, range=[{vsand.min():.4f}, {vsand.max():.4f}]")
# =============================================================================
# Visualization
# =============================================================================
# Choose slice index to visualize
slice_idx = 32
# Plot porosity and sand volume side by side
fig, axes = plot_comparison(
[phi[slice_idx].T, vsand[slice_idx].T],
titles=['Porosity', 'Volume of Sand'],
cmap='viridis',
share_clim=False,
label='Value',
figsize=(12, 5),
)
fig.suptitle(f"Geological Model Slices (Inline = {slice_idx})", fontsize=14)
plt.tight_layout()
plt.savefig(os.path.join(FIGS_DIR, '02_cosim_properties.png'))
plt.show()
# =============================================================================
# Unconditional SGS Simulation
# =============================================================================
print("\n--- Unconditional SGS (3D) ---")
config_sgs = SimulationConfig(
shape=(64, 64, 64),
lh=10.0,
lv=5.0,
n_realizations=2,
seed=42,
device=str(device),
)
# Simple Kriging
sim_sk = Simulator.create('sgs', kriging_type='sk')
fields_sk = sim_sk.simulate(config_sgs)
print(f"SK SGS shape: {fields_sk.shape}")
print(f"SK SGS: mean={fields_sk.mean():.4f}, std={fields_sk.std():.4f}")
# Ordinary Kriging
sim_ok = Simulator.create('sgs', kriging_type='ok')
fields_ok = sim_ok.simulate(config_sgs)
print(f"OK SGS shape: {fields_ok.shape}")
print(f"OK SGS: mean={fields_ok.mean():.4f}, std={fields_ok.std():.4f}")
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
slice_idx = 32 # Middle Z-slice
for i in range(2):
data_sk = fields_sk[i, :, :, slice_idx].detach().cpu().numpy()
im = axes[0, i].imshow(data_sk.T, origin='lower', cmap='viridis', aspect='auto',
interpolation='bilinear')
axes[0, i].set_title(f'SK Realization {i+1}')
plt.colorbar(im, ax=axes[0, i], label='Value')
data_ok = fields_ok[i, :, :, slice_idx].detach().cpu().numpy()
im = axes[1, i].imshow(data_ok.T, origin='lower', cmap='viridis', aspect='auto',
interpolation='bilinear')
axes[1, i].set_title(f'OK Realization {i+1}')
plt.colorbar(im, ax=axes[1, i], label='Value')
fig.suptitle('Unconditional SGS (64x64x64, Z-slice=32): SK vs OK', fontsize=14)
plt.tight_layout()
plt.savefig(os.path.join(FIGS_DIR, '02_sgs_unconditional.png'))
plt.show()
# =============================================================================
# Conditional SGS Simulation (Honoring Well Data)
# =============================================================================
print("\n--- Conditional SGS (3D) ---")
# Define well data (conditioning points with 3D coordinates)
# Wells penetrate at a specific Z-layer (z=32, the middle slice)
well_locations = torch.tensor([
[16., 16., 32.],
[16., 48., 32.],
[48., 16., 32.],
[48., 48., 32.],
[32., 32., 32.],
])
well_values = torch.tensor([0.8, -0.5, 1.2, -1.0, 0.3])
config_cond = SimulationConfig(
shape=(64, 64, 64),
lh=12.0,
lv=5.0,
n_realizations=2,
seed=2025,
device=str(device),
)
sim_cond = Simulator.create('sgs', kriging_type='ok')
sim_cond.set_conditioning(well_values, well_locations)
fields_cond = sim_cond.simulate(config_cond)
print(f"Conditional SGS shape: {fields_cond.shape}")
# Verify conditioning data is honored
print("Well data verification:")
for i in range(len(well_values)):
ix = int(well_locations[i, 0])
iy = int(well_locations[i, 1])
iz = int(well_locations[i, 2])
sim_val = fields_cond[0, ix, iy, iz].item()
true_val = well_values[i].item()
match = "OK" if abs(sim_val - true_val) < 1e-5 else "MISMATCH"
print(f" Well ({ix},{iy},{iz}): expected={true_val:.2f}, simulated={sim_val:.4f} [{match}]")
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
slice_idx = 32 # Z-slice where wells are located
for i in range(2):
data = fields_cond[i, :, :, slice_idx].detach().cpu().numpy()
im = axes[i].imshow(data.T, origin='lower', cmap='viridis', aspect='auto',
interpolation='bilinear')
# Plot well locations on this slice
axes[i].scatter(
well_locations[:, 0].numpy(),
well_locations[:, 1].numpy(),
c=well_values.numpy(), cmap='viridis',
edgecolors='red', linewidths=2, s=100, zorder=5,
vmin=data.min(), vmax=data.max(),
)
axes[i].set_title(f'Conditional Realization {i+1}')
plt.colorbar(im, ax=axes[i], label='Value')
fig.suptitle('Conditional SGS (64x64x64, Z-slice=32): Red circles = well data', fontsize=14)
plt.tight_layout()
plt.savefig(os.path.join(FIGS_DIR, '02_sgs_conditional.png'))
plt.show()
# =============================================================================
# Variogram Model Usage
# =============================================================================
print("\n--- Variogram Model ---")
# Create variogram model manually
vario = VariogramModel(
nugget=0.1,
structures=[
VariogramStructure('spherical', contribution=0.6, range_h=30.0, range_v=10.0),
VariogramStructure('exponential', contribution=0.3, range_h=80.0, range_v=20.0),
]
)
print(f"Sill (nugget + contributions): {vario.sill}")
print(f"C(0,0,0) = {vario.covariance_3d(0, 0, 0):.4f}")
print(f"C(10,0,0) = {vario.covariance_3d(10, 0, 0):.4f}")
print(f"C(50,0,0) = {vario.covariance_3d(50, 0, 0):.4f}")
# Plot variogram curve for different models
distances = np.linspace(0, 50, 200)
fig, ax = plt.subplots(figsize=(8, 5))
colors = ['#1f77b4', '#ff7f0e', '#2ca02c']
for idx, model_name in enumerate(['spherical', 'exponential', 'gaussian']):
vm = VariogramModel(
nugget=0.0,
structures=[VariogramStructure(model_name, 1.0, 20.0)]
)
gamma = [vm.sill - vm.covariance_3d(d, 0, 0) for d in distances]
ax.plot(distances, gamma, label=model_name.capitalize(), linewidth=2, color=colors[idx])
ax.axhline(y=1.0, color='gray', linestyle='--', alpha=0.5, label='Sill')
ax.set_xlabel('Distance h')
ax.set_ylabel('Variogram γ(h)')
ax.set_title('Variogram Models (range=20, sill=1)')
ax.legend()
ax.grid(True, alpha=0.3, linestyle='--')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.tight_layout()
plt.savefig(os.path.join(FIGS_DIR, '02_variogram_models.png'))
plt.show()