/[escript]/trunk/doc/examples/inversion/gravmag_nodriver.py
ViewVC logotype

Contents of /trunk/doc/examples/inversion/gravmag_nodriver.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6812 - (show annotations)
Fri May 3 00:36:47 2019 UTC (6 months, 1 week ago) by aellery
File MIME type: text/x-python
File size: 5337 byte(s)
Another silo fix


1 ##############################################################################
2 #
3 # Copyright (c) 2012-2018 by The University of Queensland
4 # http://www.uq.edu.au
5 #
6 # Primary Business: Queensland, Australia
7 # Licensed under the Apache License, version 2.0
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
11 # Development 2012-2013 by School of Earth Sciences
12 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
13 #
14 ##############################################################################
15 from __future__ import division, print_function
16
17 """
18 Advanced 3D gravity/magnetic joint inversion example without using any
19 inversion drivers
20 """
21
22 __copyright__="""Copyright (c) 2012-2018 by The University of Queensland
23 http://www.uq.edu.au
24 Primary Business: Queensland, Australia"""
25 __license__="""Licensed under the Apache License, version 2.0
26 http://www.apache.org/licenses/LICENSE-2.0"""
27 __url__="https://launchpad.net/escript-finley"
28
29 # Import required modules
30 import numpy as np
31 from esys.downunder import *
32 from esys.escript import unitsSI as U
33 from esys.escript import *
34 from esys.weipa import *
35
36 try:
37 import pyproj
38 havePyProj=True
39 except ImportError:
40 havePyProj=False
41
42 # Set parameters
43 MAGNETIC_DATASET = 'data/MagneticSmall.nc'
44 MAG_UNITS = U.Nano * U.Tesla
45 GRAVITY_DATASET = 'data/GravitySmall.nc'
46 GRAV_UNITS = 1e-6 * U.m/(U.sec**2)
47 # background magnetic field components (B_East, B_North, B_Vertical)
48 B_b = [2201.*U.Nano*U.Tesla, 31232.*U.Nano*U.Tesla, -41405.*U.Nano*U.Tesla]
49
50 thickness = 40. * U.km # below surface
51 l_air = 6. * U.km # above surface
52 n_cells_v = 25 # number of cells in vertical direction
53
54 # apply 20% padding
55 PAD_X = 0.2
56 PAD_Y = 0.2
57
58 MU_GRAVITY = 10.
59 MU_MAGNETIC = 0.1
60
61 COORDINATES=CartesianReferenceSystem()
62
63 def work():
64 # read data:
65 source_g=NetCdfData(NetCdfData.GRAVITY, GRAVITY_DATASET, scale_factor=GRAV_UNITS, reference_system=COORDINATES)
66 source_m=NetCdfData(NetCdfData.MAGNETIC, MAGNETIC_DATASET, scale_factor=MAG_UNITS, reference_system=COORDINATES)
67
68 # create domain:
69 db=DomainBuilder(dim=3, reference_system=COORDINATES)
70 db.addSource(source_g)
71 db.addSource(source_m)
72 db.setVerticalExtents(depth=thickness, air_layer=l_air, num_cells=n_cells_v)
73 db.setFractionalPadding(pad_x=PAD_X, pad_y=PAD_Y)
74 db.fixDensityBelow(depth=thickness)
75 db.fixSusceptibilityBelow(depth=thickness)
76
77 dom=db.getDomain()
78 DIM=dom.getDim()
79
80 # create mappings with standard parameters
81 rho_mapping=DensityMapping(dom)
82 k_mapping=SusceptibilityMapping(dom)
83
84 # create regularization with two level set functions:
85 reg_mask=Data(0.,(2,), Solution(dom))
86 reg_mask[0] = db.getSetDensityMask() # mask for locations where m[0]~rho is known
87 reg_mask[1] = db.getSetSusceptibilityMask() # mask for locations where m[0]~k is known
88 regularization=Regularization(dom, numLevelSets=2,
89 w1=np.ones((2,DIM)), # consider gradient terms
90 wc=[[0,1],[0,0]], # and cross-gradient term
91 coordinates=COORDINATES,
92 location_of_set_m=reg_mask)
93
94 # create forward model for gravity
95 # get data with deviation
96 g,sigma_g=db.getGravitySurveys()[0]
97 # turn the scalars into vectors (vertical direction)
98 d=kronecker(DIM)[DIM-1] # == (0 0 1)
99 w=safeDiv(1., sigma_g)
100
101 # multipling by d extracts only the z component (since we only measure in vertical)
102 gravity_model=GravityModel(dom, w*d, g*d, coordinates=COORDINATES)
103 gravity_model.rescaleWeights(rho_scale=rho_mapping.getTypicalDerivative())
104
105 # create forward model for magnetic
106 d=normalize(np.array(B_b)) # direction of measurement
107 B,sigma_B=db.getMagneticSurveys()[0]
108 w=safeDiv(1., sigma_B)
109
110 magnetic_model=MagneticModel(dom, w*d, B*d, B_b, coordinates=COORDINATES)
111 # or:
112 # magnetic_model=SelfDemagnetizationModel(dom, w*d, B*d, B_b, coordinates=COORDINATES)
113 magnetic_model.rescaleWeights(k_scale=k_mapping.getTypicalDerivative())
114
115
116 # finally we can set up the cost function:
117 cf=InversionCostFunction(regularization,
118 ((rho_mapping,0), (k_mapping, 1)),
119 ((gravity_model,0), (magnetic_model,1)) )
120
121 cf.setTradeOffFactorsModels([MU_GRAVITY, MU_MAGNETIC])
122
123 # sun solver:
124 solver=MinimizerLBFGS()
125 solver.setCostFunction(cf)
126 solver.setTolerance(1e-4)
127 # solver.setMaxIterations(50)
128 solver.setMaxIterations(100)
129 solver.run(Data(0.,(2,),Solution(dom)))
130 m=solver.getResult()
131 density, susceptibility = cf.getProperties(m)
132
133
134 # write everything to file:
135 saveSilo("result_gravmag.silo",
136 density=density, susceptability=susceptibility,
137 g_data=g, sigma_g=sigma_g, B_data=B, sigma_B=sigma_B)
138 saveVTK("result_gravmag.vtu",
139 density=density, susceptability=susceptibility,
140 g_data=g, sigma_g=sigma_g, B_data=B, sigma_B=sigma_B)
141
142 print("All done. Have a nice day!")
143
144 try:
145 import esys.ripley
146 HAVE_RIPLEY = True
147 except ImportError:
148 HAVE_RIPLEY = False
149
150 if 'NetCdfData' not in dir():
151 print("This example requires scipy's netcdf support which does not appear to be installed.")
152 elif not HAVE_RIPLEY:
153 print("Ripley module not available")
154 elif not havePyProj:
155 print("This example requires pyproj.")
156 else:
157 work()
158

  ViewVC Help
Powered by ViewVC 1.1.26