Save forward models in joint inversion

Asked by Kasemsak Saetang

I just completely ran joint inversion. Later I try to save the result in forward models of gravity and magnetic map for comparisons with observed data. Does anyone show me how could I add functions in the command lines?
 My joint inversion code is shown below. How could I add the save forward command?

 ##############################################################################
#
# Copyright (c) 2009-2015 by The University of Queensland
# http://www.uq.edu.au
#
# Primary Business: Queensland, Australia
# Licensed under the Open Software License version 3.0
# http://www.opensource.org/licenses/osl-3.0.php
#
# Development until 2012 by Earth Systems Science Computational Center (ESSCC)
# Development 2012-2013 by School of Earth Sciences
# Development from 2014 by Centre for Geoscience Computing (GeoComp)
#
##############################################################################

"""3D gravity/magnetic joint inversion example using netCDF data"""
from __future__ import division, print_function

__copyright__="""Copyright (c) 2009-2015 by The University of Queensland
http://www.uq.edu.au
Primary Business: Queensland, Australia"""
__license__="""Licensed under the Open Software License version 3.0
http://www.opensource.org/licenses/osl-3.0.php"""
__url__="https://launchpad.net/escript-finley"

# Import required modules
from esys.downunder import *
from esys.escript import unitsSI as U
from esys.escript import saveDataCSV, getEscriptParamInt
from esys.weipa import *

haveNetcdf=(getEscriptParamInt("NETCDF_BUILD",0)==1)

# Set parameters
MAGNETIC_DATASET = 'mag_CB2_UTMzone47N.ers'
MAG_UNITS = U.Nano * U.Tesla
GRAVITY_DATASET = 'grav_CB2_UTMzone47N.ers'
GRAV_UNITS = U.mgal
# background magnetic field components (B_East, B_North, B_Vertical)
B_b = [39708.2*U.Nano*U.Tesla, -528.9*U.Nano*U.Tesla, 19456.9*U.Nano*U.Tesla]
PAD_X = 0.2
PAD_Y = 0.2
thickness = 40. * U.km
l_air = 6. * U.km
n_cells_v = 25
mu_gravity = 10.
mu_magnetic = 0.1
COORDINATES=CartesianReferenceSystem()
#COORDINATES=WGS84ReferenceSystem()

def work():
  # Setup and run the inversion
  grav_source=ErMapperData(ErMapperData.GRAVITY, GRAVITY_DATASET, scale_factor=GRAV_UNITS, reference_system=COORDINATES)
  mag_source=ErMapperData(ErMapperData.MAGNETIC, MAGNETIC_DATASET, scale_factor=MAG_UNITS, reference_system=COORDINATES, altitude=5*U.km)
  db=DomainBuilder(dim=3, reference_system=COORDINATES)
  db.addSource(grav_source)
  db.addSource(mag_source)
  db.setVerticalExtents(depth=thickness, air_layer=l_air, num_cells=n_cells_v)
  db.setFractionalPadding(pad_x=PAD_X, pad_y=PAD_Y)
  db.setBackgroundMagneticFluxDensity(B_b)
  db.fixDensityBelow(depth=thickness)
  db.fixSusceptibilityBelow(depth=thickness)

  inv=JointGravityMagneticInversion()
  inv.setSolverTolerance(1e-4)
  inv.setSolverMaxIterations(500)
  inv.setup(db)
  inv.getCostFunction().setTradeOffFactorsModels([mu_gravity, mu_magnetic])
  inv.getCostFunction().setTradeOffFactorsRegularization(mu = [1.,1.], mu_c=1.)

  density, susceptibility = inv.run()
  print("density = %s"%density)
  print("susceptibility = %s"%susceptibility)

  g, wg = db.getGravitySurveys()[0]
  B, wB = db.getMagneticSurveys()[0]
  if saveSilo("result_gravmag_CB2.silo", density=density, gravity_anomaly=g, gravity_weight=wg, susceptibility=susceptibility, magnetic_anomaly=B, magnetic_weight=wB):
      print("Results saved in result_gravmag_CB2.silo")
  else:
      print("Failed to save result_gravmag.silo. Possibly no Silo support.")

  saveVTK("result_gravmag_CB2.vtu", density=density, gravity_anomaly=g, gravity_weight=wg, susceptibility=susceptibility, magnetic_anomaly=B, magnetic_weight=wB)
  print("Results saved in result_gravmag_CB2.vtu")

  saveDataCSV("result_gravmag_CB2.csv", density=density, susceptibility=susceptibility, x=susceptibility.getFunctionSpace().getX())
  print("Results saved in result_gravmag_CB2.csv")

  print("All done. Have a nice day!")

try:
    import pyproj
except ImportError:
    print("This example requires pyproj to be installed.")
    import sys
    sys.exit(0)

try:
  import esys.ripley
  HAVE_RIPLEY = True
except ImportError:
  HAVE_RIPLEY = False

if 'NetCdfData' not in dir():
    print("This example requires scipy's netcdf support which does not appear to be installed.")
elif not HAVE_RIPLEY:
    print("Ripley module not available")
elif not haveNetcdf:
    print("netCDF not available.")
else:
    work()

Question information

Language:
English Edit question
Status:
Solved
For:
esys-escript Edit question
Assignee:
No assignee Edit question
Solved by:
Bob
Solved:
Last query:
Last reply:
Revision history for this message
Bob (caltinay) said :
#1

You need to do something like the following.
First get a reference to the forward models:

gravity_model = inv.getForwardModel(0)
magnetic_model = inv.getForwardModel(0)

Then apply the forward models to the computed density/susceptibility, respectively:

phi_g, gf = gravity_model.getArguments(density)
phi_m, Bf = magnetic_model.getArguments(susceptibility)

gf is the resulting gravity force, Bf the magnetic flux density, which you can save to Silo for example.

Revision history for this message
Bob (caltinay) said :
#2

Sorry, there is a typo in my last comment, it should read:

magnetic_model = inv.getForwardModel(1)

Revision history for this message
Kasemsak Saetang (light2529) said :
#3

Dear Cihan Altinary,

I followed your answers, but it showed error texts.

Could I use this code ?

 phi_g, gf = inv.getCostFunction().getForwardModel(1).getArguments(density)
 phi_m, Bf = inv.getCostFunction().getForwardModel(1).getArguments(susceptibility)
 saveDataCSV("result_gravmag2.csv", gf=gf, Bf=Bf)

The results are

Bf_0, Bf_1, Bf_2, gf_0, gf_1, gf_2
3.138039392079889e-10, 3.102061372650754e-09, 6.836873338174803e-08, -1.402879203447635e-06, -1.605879394528585e-05, -2.180649163104763e-05

I am not sure the results are correct?

Regards,
Kasemsak Saetang

Revision history for this message
Best Bob (caltinay) said :
#4

Make sure you use getForwardModel(0) (not 1) in the first call with density, otherwise it looks correct.

Revision history for this message
Kasemsak Saetang (light2529) said :
#5

Thanks Cihan Altinay, that solved my question.