Skip to content

Commit

Permalink
Fix bug in heat equation example
Browse files Browse the repository at this point in the history
Only write grdecl file for iteration 0,
otherwise the updated field will be overwritten.
Change seed to create prior that makes the problem easier to solve.
  • Loading branch information
dafeda authored Aug 15, 2024
1 parent aad5994 commit bb5b4fc
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 61 deletions.
2 changes: 1 addition & 1 deletion test-data/heat_equation/config.ert
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ FIELD COND PARAMETER cond.grdecl INIT_FILES:cond.grdecl FORWARD_INIT:True
GEN_DATA MY_RESPONSE RESULT_FILE:gen_data_%d.out REPORT_STEPS:10,71,132,193,255,316,377,438 INPUT_FORMAT:ASCII

INSTALL_JOB heat_equation HEAT_EQUATION
SIMULATION_JOB heat_equation <IENS>
SIMULATION_JOB heat_equation <IENS> <ITER>
4 changes: 3 additions & 1 deletion test-data/heat_equation/generate_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
from definition import Coordinate, obs_coordinates, obs_times
from heat_equation import heat_equation, sample_prior_conductivity

rng = np.random.default_rng(11223344)
# Some seeds produce priors that yield poor results.
# Worth playing around with.
rng = np.random.default_rng(1234)


def create_egrid_file():
Expand Down
21 changes: 10 additions & 11 deletions test-data/heat_equation/heat_equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import numpy.typing as npt
from definition import dx, k_end, k_start, nx, obs_coordinates, obs_times, u_init

from ert.field_utils.grdecl_io import export_grdecl, import_grdecl


def heat_equation(
u: npt.NDArray[np.float64],
Expand Down Expand Up @@ -58,21 +60,18 @@ def sample_prior_conductivity(ensemble_size, nx, rng):

if __name__ == "__main__":
iens = int(sys.argv[1])
iteration = int(sys.argv[2])
rng = np.random.default_rng(iens)
cond = sample_prior_conductivity(ensemble_size=1, nx=nx, rng=rng).reshape(nx, nx)

# Write the array to a GRDECL formatted file
with open("cond.grdecl", "w", encoding="utf-8") as f:
f.write("COND\n") # Write the property name
f.write("-- Conductivity data\n") # Optional comment line

# Write the data
for row in cond:
for value in row:
f.write(f"{value:.6f} ")
f.write("\n")
if iteration == 0:
export_grdecl(cond, "cond.grdecl", "COND", binary=False)
else:
cond = import_grdecl("cond.grdecl", "COND", dimensions=(nx, nx))

f.write("/\n") # End the data section with a slash
# The update may give non-physical parameter values, which here means negative heat conductivity.
# Setting negative values to a small positive value but not zero because we want to be able to divide by them.
cond = cond.clip(min=1e-8)

# Calculate maximum `dt`.
# If higher values are used, the numerical solution will become unstable.
Expand Down
12 changes: 6 additions & 6 deletions test-data/heat_equation/obs_10.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
0.12768275433672183 0.006488917943407967
3.0368036238666027 0.14544770793104408
8.775028931053242 0.4498048818705942
13.059948516348125 0.6600383581024866
0.6246961500720613 0.0325455421675484
12.831585565944733 0.6849812341743811
5.644282891424409e-06 3.0797309738845924e-07
0.4141034913970046 0.020194884393806192
3.6679650461183813 0.16482328265801968
16.248260775729047 0.7711986689272523
1.7635921856785681 0.08703349479288272
22.164467870340125 1.172137832164079
12 changes: 6 additions & 6 deletions test-data/heat_equation/obs_132.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
1.5716360848486481 0.07841198652325546
5.70904881169535 0.27805884604045616
4.6359274211650465 0.23688032310841162
7.690562477594383 0.37684849400298187
2.446579063931665 0.11968462852792194
6.966783167860096 0.36510800657023174
0.44779702579071645 0.02217893764751358
3.24857545175091 0.15681634617125523
2.760504470567121 0.13250138130254852
3.3842521445536913 0.16395530223818405
0.9695985099612162 0.04802326866530623
2.0614711879846066 0.09892536429937095
12 changes: 6 additions & 6 deletions test-data/heat_equation/obs_193.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
1.0502697408535813 0.050249877727283246
3.629866441977895 0.18327680822627104
2.960774288633309 0.13955259374461457
4.481886461832233 0.23409425850515386
1.2993697929710817 0.0686953030021008
4.036911990951465 0.21331180461247895
0.5958134294947841 0.03025694175903222
2.4593591604433196 0.12647358190342392
1.5710086333003375 0.08528870630118744
1.6568063803163675 0.09129362898991836
0.5932408395176263 0.029074626755391172
1.2126053149741356 0.05481999368425038
12 changes: 6 additions & 6 deletions test-data/heat_equation/obs_255.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
0.6310622418009374 0.032488575019987025
2.521390080041612 0.12059552143095845
1.6503563354895465 0.08558627403540048
2.924686653695442 0.14697797215599903
0.7653506764858619 0.041125619497665494
2.426252682276415 0.12838898993061534
0.6227307374702258 0.03097070678698683
1.9783727463142318 0.0986479450282891
1.2544246608567926 0.05970441297989272
1.1879846428445189 0.057891275403232016
0.35755049482970386 0.019673050795920986
0.7009343241465649 0.03513379961138451
12 changes: 6 additions & 6 deletions test-data/heat_equation/obs_316.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
0.41917915231764985 0.02138290844488322
1.4914525747093 0.08018501607364692
1.0928690326051034 0.05428245248478661
1.9832718843085502 0.09390177155175192
0.46698555594867486 0.025630460596029542
1.5797253869062449 0.07975283486740689
0.5524614350856832 0.028125076406103185
1.412412637815958 0.07717531221040153
0.9199307278481795 0.04439419911256866
0.7604110043935532 0.040243854530727374
0.2833847824680585 0.014347799635988293
0.5029466084851733 0.02474437100264124
12 changes: 6 additions & 6 deletions test-data/heat_equation/obs_377.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
0.2931205763589738 0.014140217358236368
1.078094078363641 0.053365783886943366
0.7192203876910312 0.034901828394380906
1.2109229760377362 0.060403462021153945
0.33739602727437984 0.01626283811285199
1.0303481357741326 0.05027127570950877
0.5145913371974361 0.024116728328300304
1.2130120795386947 0.06058558661585877
0.6612465555505336 0.03405614051381544
0.6018131783406871 0.029445994400563853
0.23849281866670452 0.01088287872549113
0.35652866446374504 0.01832894260196113
12 changes: 6 additions & 6 deletions test-data/heat_equation/obs_438.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
0.1664581863654361 0.009364965672609851
0.7300976951071562 0.035495489261655876
0.46912528290095123 0.02261674411907338
0.7072805644338298 0.03906096830312989
0.21631992781141549 0.010430508490340842
0.6447669420574793 0.032010045332414874
0.39953767757628283 0.020053305021488894
0.9295898697400751 0.04770972792415899
0.5497094384908743 0.02657803741684105
0.4480479415467605 0.022226512076016683
0.17204797615905992 0.008437608752461214
0.2758280704559395 0.013980361434629683
12 changes: 6 additions & 6 deletions test-data/heat_equation/obs_71.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
2.2438672355992333 0.11671718386773551
7.892807449865204 0.40982953403161915
8.92516752247771 0.43228873919471245
12.500888303994804 0.628865108680274
4.1069538345763466 0.20842320133194528
12.762539072098512 0.651852316119926
0.12972441046117011 0.006459468053345893
3.4099513667440347 0.1623673652866253
4.751474206590012 0.235669334834115
7.793057950541453 0.36514142638442393
2.0595838023223245 0.09825818825978899
4.514816590427751 0.24168886166817694

0 comments on commit bb5b4fc

Please sign in to comment.