Skip to content

Commit

Permalink
Fix fullmodel model2gimli/model2emg3d
Browse files Browse the repository at this point in the history
  • Loading branch information
prisae committed Jun 28, 2024
1 parent e3158df commit ded6583
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions emg3d/inversion/pygimli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class Kernel(pygimli.Modelling if pygimli else object):
The simulation; a :class:`emg3d.simulations.Simulation` instance.
markers : ndarray of dtype int, default: None
An ndarray of ints of the same shapes as the model. All cells with the
same number belong to the same region with this number, which can
An ndarray of integers of the same shape as the model. All cells with
the same number belong to the same region with this number, which can
subsequently be defined through
:func:`pygimli.frameworks.modelling.Modelling.setRegionProperties`.
Expand Down Expand Up @@ -97,14 +97,15 @@ def __init__(self, simulation, markers=None, pgthreads=2):
mesh.setCellMarkers(markers.ravel('F'))
self.markers = markers
else:
self.markes = np.zeros(simulation.model.size, dtype=int)
self.markers = np.zeros(simulation.model.size, dtype=int)
# Store original props; required if a region is set to ``background``.
self._model = simulation.model.property_x.copy()
# Store volumes; required if a region is set to ``single``.
self._volumes = simulation.model.grid.cell_volumes.reshape(
self._model.shape, order='F')
# Set mesh.
self.setMesh(mesh)
self._fullmodel = None

# Create J, store and set it.
self.J = self.Jacobian(
Expand Down Expand Up @@ -165,7 +166,12 @@ def model2gimli(self, model):

# If the inversion model is smaller than the model, we have to
# take care of the regions.
if len(model) != self.simulation.model.size:
if self.fullmodel:

out = np.empty(model.size)
out[self.mesh().cellMarkers()] = model.ravel('F')

else:

out = np.empty(self.simulation.model.size)
i = 0
Expand All @@ -184,10 +190,6 @@ def model2gimli(self, model):

out = out[:i]

else:
out = np.empty(model.size)
out[self.mesh().cellMarkers()] = model.ravel('F')

return out

def model2emg3d(self, model):
Expand All @@ -198,7 +200,12 @@ def model2emg3d(self, model):

# If the inversion model is smaller than the model, we have to
# take care of the regions.
if len(model) != self.simulation.model.size:
if self.fullmodel:

out = np.asarray(model[self.mesh().cellMarkers()]).reshape(
self.simulation.model.shape, order='F')

else:

out = np.empty(self.simulation.model.shape)
i = 0
Expand All @@ -219,12 +226,22 @@ def model2emg3d(self, model):
out[ni] = model[i:ii+i]
i += ii

else:
out = np.asarray(model[self.mesh().cellMarkers()]).reshape(
self.simulation.model.shape, order='F')

return out

@property
def fullmodel(self):
"""Flag if the full model is used for the inversion or not."""
if self._fullmodel is None:
self._fullmodel = True
if self.regionProperties():
keys = ['background', 'fix', 'single']
for n, v in self.regionProperties().items():
if np.any([v[k] is True for k in keys]):
self._fullmodel = False
break

return self._fullmodel

class Jacobian(pygimli.Matrix if pygimli else object):
"""Return Jacobian operator for pyGIMLi(emg3d)."""

Expand Down Expand Up @@ -289,6 +306,9 @@ def run(self, dataVals=None, errorVals=None, **kwargs):
self.fop.simulation.survey.standard_deviation.data)
errorVals = std_dev / abs(dataVals)

# Reset full-model flag.
self.fop._fullmodel = None

# Run the inversion
out = super().run(dataVals=dataVals, errorVals=errorVals, **kwargs)

Expand Down

0 comments on commit ded6583

Please sign in to comment.