mirror of
https://github.com/wassname/simpeg.git
synced 2026-09-09 11:34:26 +08:00
prototype of defining regularization mesh within Regularization.py for constructing operators for regularization that are not true differential operators
This commit is contained in:
@@ -307,24 +307,28 @@ class DiffOperators(object):
|
|||||||
return BC
|
return BC
|
||||||
_cellGradBC_list = 'neumann'
|
_cellGradBC_list = 'neumann'
|
||||||
|
|
||||||
|
def _cellGradStencil(self):
|
||||||
|
BC = self.setCellGradBC(self._cellGradBC_list)
|
||||||
|
n = self.vnC
|
||||||
|
if(self.dim == 1):
|
||||||
|
G = ddxCellGrad(n[0], BC[0])
|
||||||
|
elif(self.dim == 2):
|
||||||
|
G1 = sp.kron(speye(n[1]), ddxCellGrad(n[0], BC[0]))
|
||||||
|
G2 = sp.kron(ddxCellGrad(n[1], BC[1]), speye(n[0]))
|
||||||
|
G = sp.vstack((G1, G2), format="csr")
|
||||||
|
elif(self.dim == 3):
|
||||||
|
G1 = kron3(speye(n[2]), speye(n[1]), ddxCellGrad(n[0], BC[0]))
|
||||||
|
G2 = kron3(speye(n[2]), ddxCellGrad(n[1], BC[1]), speye(n[0]))
|
||||||
|
G3 = kron3(ddxCellGrad(n[2], BC[2]), speye(n[1]), speye(n[0]))
|
||||||
|
G = sp.vstack((G1, G2, G3), format="csr")
|
||||||
|
return G
|
||||||
|
|
||||||
def cellGrad():
|
def cellGrad():
|
||||||
doc = "The cell centered Gradient, takes you to cell faces."
|
doc = "The cell centered Gradient, takes you to cell faces."
|
||||||
|
|
||||||
def fget(self):
|
def fget(self):
|
||||||
if(self._cellGrad is None):
|
if(self._cellGrad is None):
|
||||||
BC = self.setCellGradBC(self._cellGradBC_list)
|
G = self._cellGradStencil()
|
||||||
n = self.vnC
|
|
||||||
if(self.dim == 1):
|
|
||||||
G = ddxCellGrad(n[0], BC[0])
|
|
||||||
elif(self.dim == 2):
|
|
||||||
G1 = sp.kron(speye(n[1]), ddxCellGrad(n[0], BC[0]))
|
|
||||||
G2 = sp.kron(ddxCellGrad(n[1], BC[1]), speye(n[0]))
|
|
||||||
G = sp.vstack((G1, G2), format="csr")
|
|
||||||
elif(self.dim == 3):
|
|
||||||
G1 = kron3(speye(n[2]), speye(n[1]), ddxCellGrad(n[0], BC[0]))
|
|
||||||
G2 = kron3(speye(n[2]), ddxCellGrad(n[1], BC[1]), speye(n[0]))
|
|
||||||
G3 = kron3(ddxCellGrad(n[2], BC[2]), speye(n[1]), speye(n[0]))
|
|
||||||
G = sp.vstack((G1, G2, G3), format="csr")
|
|
||||||
# Compute areas of cell faces & volumes
|
# Compute areas of cell faces & volumes
|
||||||
S = self.area
|
S = self.area
|
||||||
V = self.aveCC2F*self.vol # Average volume between adjacent cells
|
V = self.aveCC2F*self.vol # Average volume between adjacent cells
|
||||||
@@ -361,19 +365,24 @@ class DiffOperators(object):
|
|||||||
_cellGradBC = None
|
_cellGradBC = None
|
||||||
cellGradBC = property(**cellGradBC())
|
cellGradBC = property(**cellGradBC())
|
||||||
|
|
||||||
|
def _cellGradxStencil(self):
|
||||||
|
BC = ['neumann', 'neumann']
|
||||||
|
n = self.vnC
|
||||||
|
if(self.dim == 1):
|
||||||
|
G1 = ddxCellGrad(n[0], BC)
|
||||||
|
elif(self.dim == 2):
|
||||||
|
G1 = sp.kron(speye(n[1]), ddxCellGrad(n[0], BC))
|
||||||
|
elif(self.dim == 3):
|
||||||
|
G1 = kron3(speye(n[2]), speye(n[1]), ddxCellGrad(n[0], BC))
|
||||||
|
return G1
|
||||||
|
|
||||||
|
|
||||||
def cellGradx():
|
def cellGradx():
|
||||||
doc = "Cell centered Gradient in the x dimension. Has neumann boundary conditions."
|
doc = "Cell centered Gradient in the x dimension. Has neumann boundary conditions."
|
||||||
|
|
||||||
def fget(self):
|
def fget(self):
|
||||||
if getattr(self, '_cellGradx', None) is None:
|
if getattr(self, '_cellGradx', None) is None:
|
||||||
BC = ['neumann', 'neumann']
|
G1 = self._cellGradxStencil()
|
||||||
n = self.vnC
|
|
||||||
if(self.dim == 1):
|
|
||||||
G1 = ddxCellGrad(n[0], BC)
|
|
||||||
elif(self.dim == 2):
|
|
||||||
G1 = sp.kron(speye(n[1]), ddxCellGrad(n[0], BC))
|
|
||||||
elif(self.dim == 3):
|
|
||||||
G1 = kron3(speye(n[2]), speye(n[1]), ddxCellGrad(n[0], BC))
|
|
||||||
# Compute areas of cell faces & volumes
|
# Compute areas of cell faces & volumes
|
||||||
V = self.aveCC2F*self.vol
|
V = self.aveCC2F*self.vol
|
||||||
L = self.r(self.area/V, 'F','Fx', 'V')
|
L = self.r(self.area/V, 'F','Fx', 'V')
|
||||||
@@ -382,17 +391,22 @@ class DiffOperators(object):
|
|||||||
return locals()
|
return locals()
|
||||||
cellGradx = property(**cellGradx())
|
cellGradx = property(**cellGradx())
|
||||||
|
|
||||||
|
def _cellGradyStencil(self):
|
||||||
|
if self.dim < 2: return None
|
||||||
|
BC = ['neumann', 'neumann']
|
||||||
|
n = self.vnC
|
||||||
|
if(self.dim == 2):
|
||||||
|
G2 = sp.kron(ddxCellGrad(n[1], BC), speye(n[0]))
|
||||||
|
elif(self.dim == 3):
|
||||||
|
G2 = kron3(speye(n[2]), ddxCellGrad(n[1], BC), speye(n[0]))
|
||||||
|
return G2
|
||||||
|
|
||||||
def cellGrady():
|
def cellGrady():
|
||||||
doc = "Cell centered Gradient in the x dimension. Has neumann boundary conditions."
|
doc = "Cell centered Gradient in the x dimension. Has neumann boundary conditions."
|
||||||
def fget(self):
|
def fget(self):
|
||||||
if self.dim < 2: return None
|
if self.dim < 2: return None
|
||||||
if getattr(self, '_cellGrady', None) is None:
|
if getattr(self, '_cellGrady', None) is None:
|
||||||
BC = ['neumann', 'neumann']
|
G2 = self._cellGradyStencil()
|
||||||
n = self.vnC
|
|
||||||
if(self.dim == 2):
|
|
||||||
G2 = sp.kron(ddxCellGrad(n[1], BC), speye(n[0]))
|
|
||||||
elif(self.dim == 3):
|
|
||||||
G2 = kron3(speye(n[2]), ddxCellGrad(n[1], BC), speye(n[0]))
|
|
||||||
# Compute areas of cell faces & volumes
|
# Compute areas of cell faces & volumes
|
||||||
V = self.aveCC2F*self.vol
|
V = self.aveCC2F*self.vol
|
||||||
L = self.r(self.area/V, 'F','Fy', 'V')
|
L = self.r(self.area/V, 'F','Fy', 'V')
|
||||||
@@ -401,14 +415,19 @@ class DiffOperators(object):
|
|||||||
return locals()
|
return locals()
|
||||||
cellGrady = property(**cellGrady())
|
cellGrady = property(**cellGrady())
|
||||||
|
|
||||||
|
def _cellGradzStencil(self):
|
||||||
|
if self.dim < 3: return None
|
||||||
|
BC = ['neumann', 'neumann']
|
||||||
|
n = self.vnC
|
||||||
|
G3 = kron3(ddxCellGrad(n[2], BC), speye(n[1]), speye(n[0]))
|
||||||
|
return G3
|
||||||
|
|
||||||
def cellGradz():
|
def cellGradz():
|
||||||
doc = "Cell centered Gradient in the x dimension. Has neumann boundary conditions."
|
doc = "Cell centered Gradient in the x dimension. Has neumann boundary conditions."
|
||||||
def fget(self):
|
def fget(self):
|
||||||
if self.dim < 3: return None
|
if self.dim < 3: return None
|
||||||
if getattr(self, '_cellGradz', None) is None:
|
if getattr(self, '_cellGradz', None) is None:
|
||||||
BC = ['neumann', 'neumann']
|
G3 = self._cellGradzStencil()
|
||||||
n = self.vnC
|
|
||||||
G3 = kron3(ddxCellGrad(n[2], BC), speye(n[1]), speye(n[0]))
|
|
||||||
# Compute areas of cell faces & volumes
|
# Compute areas of cell faces & volumes
|
||||||
V = self.aveCC2F*self.vol
|
V = self.aveCC2F*self.vol
|
||||||
L = self.r(self.area/V, 'F','Fz', 'V')
|
L = self.r(self.area/V, 'F','Fz', 'V')
|
||||||
@@ -565,56 +584,6 @@ class DiffOperators(object):
|
|||||||
|
|
||||||
return Pbc, Pin, Pout
|
return Pbc, Pin, Pout
|
||||||
|
|
||||||
def unitCellGradx():
|
|
||||||
doc = """Cell centered Gradient in the x dimension used for
|
|
||||||
regularization. The gradient operator is square (nC-by-nC)"""
|
|
||||||
def fget(self):
|
|
||||||
if self.dim < 3: return None
|
|
||||||
if getattr(self, '_unitCellGradx', None) is None:
|
|
||||||
|
|
||||||
n = self.vnC
|
|
||||||
gx = ddx(n[0]-1)
|
|
||||||
gx_square = sp.vstack((gx,gx[-1,:]*-1), format="csr")
|
|
||||||
|
|
||||||
self._unitCellGradx = kron3(speye(n[2]), speye(n[1]), gx_square)
|
|
||||||
|
|
||||||
return self._unitCellGradx
|
|
||||||
return locals()
|
|
||||||
unitCellGradx = property(**unitCellGradx())
|
|
||||||
|
|
||||||
def unitCellGrady():
|
|
||||||
doc = """Cell centered Gradient in they dimension used for
|
|
||||||
regularization. The gradient operator is square (nC-by-nC)"""
|
|
||||||
def fget(self):
|
|
||||||
if self.dim < 3: return None
|
|
||||||
if getattr(self, '_unitCellGrady', None) is None:
|
|
||||||
|
|
||||||
n = self.vnC
|
|
||||||
gy = ddx(n[1]-1)
|
|
||||||
gy_square = sp.vstack((gy,gy[-1,:]*-1), format="csr")
|
|
||||||
|
|
||||||
self._unitCellGrady = kron3(speye(n[2]), gy_square, speye(n[0]))
|
|
||||||
|
|
||||||
return self._unitCellGrady
|
|
||||||
return locals()
|
|
||||||
unitCellGrady = property(**unitCellGrady())
|
|
||||||
|
|
||||||
def unitCellGradz():
|
|
||||||
doc = """Cell centered Gradient in they dimension used for
|
|
||||||
regularization. The gradient operator is square (nC-by-nC)"""
|
|
||||||
def fget(self):
|
|
||||||
if self.dim < 3: return None
|
|
||||||
if getattr(self, '_unitCellGradz', None) is None:
|
|
||||||
|
|
||||||
n = self.vnC
|
|
||||||
gz = ddx(n[2]-1)
|
|
||||||
gz_square = sp.vstack((gz,gz[-1,:]*-1), format="csr")
|
|
||||||
|
|
||||||
self._unitCellGradz = kron3( gz_square , speye(n[1]), speye(n[0]))
|
|
||||||
|
|
||||||
return self._unitCellGradz
|
|
||||||
return locals()
|
|
||||||
unitCellGradz = property(**unitCellGradz())
|
|
||||||
|
|
||||||
# --------------- Averaging ---------------------
|
# --------------- Averaging ---------------------
|
||||||
|
|
||||||
|
|||||||
+222
-122
@@ -1,61 +1,35 @@
|
|||||||
import Utils, Maps, Mesh, numpy as np, scipy.sparse as sp
|
import Utils, Maps, Mesh, numpy as np, scipy.sparse as sp
|
||||||
|
|
||||||
class BaseRegularization(object):
|
class RegularizationMesh(object):
|
||||||
"""
|
|
||||||
**Base Regularization Class**
|
|
||||||
|
|
||||||
This is used to regularize the model space::
|
def __init__(self, mesh, indActive=None):
|
||||||
|
|
||||||
reg = Regularization(mesh)
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
__metaclass__ = Utils.SimPEGMetaClass
|
|
||||||
|
|
||||||
counter = None
|
|
||||||
|
|
||||||
mapPair = Maps.IdentityMap #: A SimPEG.Map Class
|
|
||||||
|
|
||||||
mapping = None #: A SimPEG.Map instance.
|
|
||||||
mesh = None #: A SimPEG.Mesh instance.
|
|
||||||
mref = None #: Reference model.
|
|
||||||
|
|
||||||
def __init__(self, mesh, mapping=None, indActive=None, **kwargs):
|
|
||||||
Utils.setKwargs(self, **kwargs)
|
|
||||||
self.mesh = mesh
|
self.mesh = mesh
|
||||||
assert isinstance(mesh, Mesh.BaseMesh), "mesh must be a SimPEG.Mesh object."
|
|
||||||
self.mapping = mapping or self.mapPair(mesh)
|
|
||||||
self.mapping._assertMatchesPair(self.mapPair)
|
|
||||||
self.indActive = indActive
|
self.indActive = indActive
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def parent(self):
|
def vol(self):
|
||||||
"""This is the parent of the regularization."""
|
if getattr(self, '_vol', None) is None:
|
||||||
return getattr(self,'_parent',None)
|
self._vol = self._Pac.T * self.mesh.vol
|
||||||
@parent.setter
|
return self._vol
|
||||||
def parent(self, p):
|
|
||||||
if getattr(self,'_parent',None) is not None:
|
|
||||||
print 'Regularization has switched to a new parent!'
|
|
||||||
self._parent = p
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def inv(self): return self.parent.inv
|
def nC(self):
|
||||||
@property
|
if getattr(self, '_nC', None) is None:
|
||||||
def invProb(self): return self.parent
|
if self.indActive is None:
|
||||||
@property
|
self._nC = self.mesh.nC
|
||||||
def reg(self): return self
|
else:
|
||||||
@property
|
if self.indActive.dtype == 'bool':
|
||||||
def opt(self): return self.parent.opt
|
self._nC = sum(self.indActive)
|
||||||
@property
|
else:
|
||||||
def prob(self): return self.parent.prob
|
self._nC = len(self.indActive) # you shouldn't pass a vector of int 0, 1 's
|
||||||
@property
|
return self._nC
|
||||||
def survey(self): return self.parent.survey
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def W(self):
|
def dim(self):
|
||||||
"""Full regularization weighting matrix W."""
|
if getattr(self, '_dim', None) is None:
|
||||||
return self._Pac.T * sp.identity(self.mesh.nC) * self._Pac # or do we want sp.identity(self.mesh.nC) or even just Utils.Identity() ?
|
self._dim = self.mesh.dim
|
||||||
|
return self._dim
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _Pac(self):
|
def _Pac(self):
|
||||||
@@ -95,7 +69,170 @@ class BaseRegularization(object):
|
|||||||
indActive_Fz = (self.mesh.aveFz2CC.T * self.indActive) == 1
|
indActive_Fz = (self.mesh.aveFz2CC.T * self.indActive) == 1
|
||||||
self.__Pafz = Utils.speye(self.mesh.nFz)[:,indActive_Fz]
|
self.__Pafz = Utils.speye(self.mesh.nFz)[:,indActive_Fz]
|
||||||
return self.__Pafz
|
return self.__Pafz
|
||||||
|
|
||||||
|
@property
|
||||||
|
def aveFx2CC(self):
|
||||||
|
if getattr(self, '_aveFx2CC', None) is None:
|
||||||
|
self._aveFx2CC = self._Pac.T * self.mesh.aveFx2CC * self._Pafx
|
||||||
|
return self._aveFx2CC
|
||||||
|
|
||||||
|
@property
|
||||||
|
def aveCC2Fx(self):
|
||||||
|
if getattr(self, '_aveCC2Fx', None) is None:
|
||||||
|
self._aveCC2Fx = Utils.sdiag(1./(self.aveFx2CC.T).sum(1)) * self.aveFx2CC.T
|
||||||
|
return self._aveCC2Fx
|
||||||
|
|
||||||
|
@property
|
||||||
|
def aveFy2CC(self):
|
||||||
|
if getattr(self, '_aveFy2CC', None) is None:
|
||||||
|
self._aveFy2CC = self._Pac.T * self.mesh.aveFy2CC * self._Pafy
|
||||||
|
return self._aveFy2CC
|
||||||
|
|
||||||
|
@property
|
||||||
|
def aveCC2Fy(self):
|
||||||
|
if getattr(self, '_aveCC2Fy', None) is None:
|
||||||
|
self._aveCC2Fy = Utils.sdiag(1./(self.aveFy2CC.T).sum(1)) * self.aveFy2CC.T
|
||||||
|
return self._aveCC2Fy
|
||||||
|
|
||||||
|
@property
|
||||||
|
def aveFz2CC(self):
|
||||||
|
if getattr(self, '_aveFz2CC', None) is None:
|
||||||
|
self._aveFz2CC = self._Pac.T * self.mesh.aveFz2CC * self._Pafz
|
||||||
|
return self._aveFz2CC
|
||||||
|
|
||||||
|
@property
|
||||||
|
def aveCC2Fz(self):
|
||||||
|
if getattr(self, '_aveCC2Fz', None) is None:
|
||||||
|
self._aveCC2Fz = Utils.sdiag(1./(self.aveFz2CC.T).sum(1)) * self.aveFz2CC.T
|
||||||
|
return self._aveCC2Fz
|
||||||
|
|
||||||
|
@property
|
||||||
|
def cellGradx(self):
|
||||||
|
if getattr(self, '_cellGradx', None) is None:
|
||||||
|
self._cellGradx = self._Pafx.T * self.mesh.cellGradx * self._Pac
|
||||||
|
return self._cellGradx
|
||||||
|
|
||||||
|
@property
|
||||||
|
def cellGrady(self):
|
||||||
|
if getattr(self, '_cellGrady', None) is None:
|
||||||
|
self._cellGrady = self._Pafy.T * self.mesh.cellGrady * self._Pac
|
||||||
|
return self._cellGrady
|
||||||
|
|
||||||
|
@property
|
||||||
|
def cellGradz(self):
|
||||||
|
if getattr(self, '_cellGradz', None) is None:
|
||||||
|
self._cellGradz = self._Pafz.T * self.mesh.cellGradz * self._Pac
|
||||||
|
return self._cellGradz
|
||||||
|
|
||||||
|
@property
|
||||||
|
def faceDivx(self):
|
||||||
|
if getattr(self, '_faceDivx', None) is None:
|
||||||
|
self._faceDivx = self._Pac.T * self.mesh.faceDivx * self._Pafx
|
||||||
|
return self._faceDivx
|
||||||
|
|
||||||
|
@property
|
||||||
|
def faceDivy(self):
|
||||||
|
if getattr(self, '_faceDivy', None) is None:
|
||||||
|
self._faceDivy = self._Pac.T * self.mesh.faceDivy * self._Pafy
|
||||||
|
return self._faceDivy
|
||||||
|
|
||||||
|
@property
|
||||||
|
def faceDivz(self):
|
||||||
|
if getattr(self, '_faceDivz', None) is None:
|
||||||
|
self._faceDivz = self._Pac.T * self.mesh.faceDivz * self._Pafz
|
||||||
|
return self._faceDivz
|
||||||
|
|
||||||
|
@property
|
||||||
|
def cellGradxStencil(self):
|
||||||
|
"""Cell centered Gradient in the x dimension used for
|
||||||
|
regularization. The gradient operator is square (nC-by-nC)"""
|
||||||
|
|
||||||
|
# if self.dim < 3: return None
|
||||||
|
if getattr(self, '_cellGradxStencil', None) is None:
|
||||||
|
|
||||||
|
self._cellGradxStencil = self._Pafx.T * self.mesh._cellGradxStencil() * self._Pac
|
||||||
|
return self._cellGradxStencil
|
||||||
|
|
||||||
|
@property
|
||||||
|
def cellGradyStencil(self):
|
||||||
|
"""Cell centered Gradient in the x dimension used for
|
||||||
|
regularization. The gradient operator is square (nC-by-nC)"""
|
||||||
|
|
||||||
|
if self.dim < 2: return None
|
||||||
|
if getattr(self, '_cellGradyStencil', None) is None:
|
||||||
|
|
||||||
|
self._cellGradyStencil = self._Pafy.T * self.mesh._cellGradyStencil() * self._Pac
|
||||||
|
return self._cellGradyStencil
|
||||||
|
|
||||||
|
@property
|
||||||
|
def cellGradzStencil(self):
|
||||||
|
"""Cell centered Gradient in the x dimension used for
|
||||||
|
regularization. The gradient operator is square (nC-by-nC)"""
|
||||||
|
|
||||||
|
if self.dim < 3: return None
|
||||||
|
if getattr(self, '_cellGradzStencil', None) is None:
|
||||||
|
|
||||||
|
self._cellGradzStencil = self._Pafz.T * self.mesh._cellGradzStencil() * self._Pac
|
||||||
|
return self._cellGradzStencil
|
||||||
|
|
||||||
|
|
||||||
|
class BaseRegularization(object):
|
||||||
|
"""
|
||||||
|
**Base Regularization Class**
|
||||||
|
|
||||||
|
This is used to regularize the model space::
|
||||||
|
|
||||||
|
reg = Regularization(mesh)
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
__metaclass__ = Utils.SimPEGMetaClass
|
||||||
|
|
||||||
|
counter = None
|
||||||
|
|
||||||
|
mapPair = Maps.IdentityMap #: A SimPEG.Map Class
|
||||||
|
|
||||||
|
mapping = None #: A SimPEG.Map instance.
|
||||||
|
mesh = None #: A SimPEG.Mesh instance.
|
||||||
|
mref = None #: Reference model.
|
||||||
|
|
||||||
|
def __init__(self, mesh, mapping=None, indActive=None, **kwargs):
|
||||||
|
Utils.setKwargs(self, **kwargs)
|
||||||
|
assert isinstance(mesh, Mesh.BaseMesh), "mesh must be a SimPEG.Mesh object."
|
||||||
|
self.regmesh = RegularizationMesh(mesh,indActive)
|
||||||
|
self.mapping = mapping or self.mapPair(mesh)
|
||||||
|
self.mapping._assertMatchesPair(self.mapPair)
|
||||||
|
self.indActive = indActive
|
||||||
|
|
||||||
|
@property
|
||||||
|
def parent(self):
|
||||||
|
"""This is the parent of the regularization."""
|
||||||
|
return getattr(self,'_parent',None)
|
||||||
|
@parent.setter
|
||||||
|
def parent(self, p):
|
||||||
|
if getattr(self,'_parent',None) is not None:
|
||||||
|
print 'Regularization has switched to a new parent!'
|
||||||
|
self._parent = p
|
||||||
|
|
||||||
|
@property
|
||||||
|
def inv(self): return self.parent.inv
|
||||||
|
@property
|
||||||
|
def invProb(self): return self.parent
|
||||||
|
@property
|
||||||
|
def reg(self): return self
|
||||||
|
@property
|
||||||
|
def opt(self): return self.parent.opt
|
||||||
|
@property
|
||||||
|
def prob(self): return self.parent.prob
|
||||||
|
@property
|
||||||
|
def survey(self): return self.parent.survey
|
||||||
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def W(self):
|
||||||
|
"""Full regularization weighting matrix W."""
|
||||||
|
return sp.identity(self.regmesh.nC)
|
||||||
|
# self.regmesh._Pac.T * sp.identity(self.regmesh.nC) * self.regmesh._Pac # or do we want sp.identity(self.mesh.nC) or even just Utils.Identity() ?
|
||||||
|
|
||||||
@Utils.timeIt
|
@Utils.timeIt
|
||||||
def eval(self, m):
|
def eval(self, m):
|
||||||
@@ -151,7 +288,6 @@ class BaseRegularization(object):
|
|||||||
|
|
||||||
return mD.T * ( self.W.T * ( self.W * ( mD * v) ) )
|
return mD.T * ( self.W.T * ( self.W * ( mD * v) ) )
|
||||||
|
|
||||||
|
|
||||||
class Tikhonov(BaseRegularization):
|
class Tikhonov(BaseRegularization):
|
||||||
"""
|
"""
|
||||||
"""
|
"""
|
||||||
@@ -165,66 +301,58 @@ class Tikhonov(BaseRegularization):
|
|||||||
alpha_zz = Utils.dependentProperty('_alpha_zz', 0.0, ['_W', '_Wzz'], "Weight for the second derivative in the z direction")
|
alpha_zz = Utils.dependentProperty('_alpha_zz', 0.0, ['_W', '_Wzz'], "Weight for the second derivative in the z direction")
|
||||||
|
|
||||||
def __init__(self, mesh, mapping=None, indActive = None, **kwargs):
|
def __init__(self, mesh, mapping=None, indActive = None, **kwargs):
|
||||||
BaseRegularization.__init__(self, mesh, mapping=mapping, **kwargs)
|
BaseRegularization.__init__(self, mesh, mapping=mapping, indActive=indActive, **kwargs)
|
||||||
self.indActive = indActive
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Ws(self):
|
def Ws(self):
|
||||||
"""Regularization matrix Ws"""
|
"""Regularization matrix Ws"""
|
||||||
if getattr(self,'_Ws', None) is None:
|
if getattr(self,'_Ws', None) is None:
|
||||||
Ws = Utils.sdiag((self.mesh.vol*self.alpha_s)**0.5)
|
self._Ws = Utils.sdiag((self.regmesh.vol*self.alpha_s)**0.5)
|
||||||
self._Ws = self._Pac.T * Ws * self._Pac
|
|
||||||
return self._Ws
|
return self._Ws
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Wx(self):
|
def Wx(self):
|
||||||
"""Regularization matrix Wx"""
|
"""Regularization matrix Wx"""
|
||||||
if getattr(self, '_Wx', None) is None:
|
if getattr(self, '_Wx', None) is None:
|
||||||
Ave_x_vol = self.mesh.aveF2CC[:,:self.mesh.nFx].T*self.mesh.vol
|
Ave_x_vol = self.regmesh.aveCC2Fx * self.regmesh.vol
|
||||||
Wx = Utils.sdiag((Ave_x_vol*self.alpha_x)**0.5)*self.mesh.cellGradx
|
self._Wx = Utils.sdiag((Ave_x_vol*self.alpha_x)**0.5)*self.regmesh.cellGradx
|
||||||
self._Wx = self._Pafx.T*Wx*self.self._Pac
|
|
||||||
return self._Wx
|
return self._Wx
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Wy(self):
|
def Wy(self):
|
||||||
"""Regularization matrix Wy"""
|
"""Regularization matrix Wy"""
|
||||||
if getattr(self, '_Wy', None) is None:
|
if getattr(self, '_Wy', None) is None:
|
||||||
Ave_y_vol = self.mesh.aveF2CC[:,self.mesh.nFx:np.sum(self.mesh.vnF[:2])].T*self.mesh.vol
|
Ave_y_vol = self.regmesh.aveCC2Fy * self.regmesh.vol
|
||||||
Wy = Utils.sdiag((Ave_y_vol*self.alpha_y)**0.5)*self.mesh.cellGrady
|
self._Wy = Utils.sdiag((Ave_y_vol*self.alpha_y)**0.5)*self.regmesh.cellGrady
|
||||||
self._Wy = self._Pafy.T*Wy*self._Pac
|
|
||||||
return self._Wy
|
return self._Wy
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Wz(self):
|
def Wz(self):
|
||||||
"""Regularization matrix Wz"""
|
"""Regularization matrix Wz"""
|
||||||
if getattr(self, '_Wz', None) is None:
|
if getattr(self, '_Wz', None) is None:
|
||||||
Ave_z_vol = self.mesh.aveF2CC[:,np.sum(self.mesh.vnF[:2]):].T*self.mesh.vol
|
Ave_z_vol = self.regmesh.aveCC2Fz * self.regmesh.vol
|
||||||
Wz = Utils.sdiag((Ave_z_vol*self.alpha_z)**0.5)*self.mesh.cellGradz
|
self._Wz = Utils.sdiag((Ave_z_vol*self.alpha_z)**0.5)*self.regmesh.cellGradz
|
||||||
self._Wz = self._Pafz.T*Wz*self._Pac
|
|
||||||
return self._Wz
|
return self._Wz
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Wxx(self):
|
def Wxx(self):
|
||||||
"""Regularization matrix Wxx"""
|
"""Regularization matrix Wxx"""
|
||||||
if getattr(self, '_Wxx', None) is None:
|
if getattr(self, '_Wxx', None) is None:
|
||||||
Wxx = Utils.sdiag((self.mesh.vol*self.alpha_xx)**0.5)*self.mesh.faceDivx*self.mesh.cellGradx
|
self._Wxx = Utils.sdiag((self.regmesh.vol*self.alpha_xx)**0.5)*self.regmesh.faceDivx*self.regmesh.cellGradx
|
||||||
self._Wxx = self._Pac.T*Wxx*self._Pac
|
|
||||||
return self._Wxx
|
return self._Wxx
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Wyy(self):
|
def Wyy(self):
|
||||||
"""Regularization matrix Wyy"""
|
"""Regularization matrix Wyy"""
|
||||||
if getattr(self, '_Wyy', None) is None:
|
if getattr(self, '_Wyy', None) is None:
|
||||||
Wyy = Utils.sdiag((self.mesh.vol*self.alpha_yy)**0.5)*self.mesh.faceDivy*self.mesh.cellGrady
|
self._Wyy = Utils.sdiag((self.regmesh.vol*self.alpha_yy)**0.5)*self.regmesh.faceDivy*self.regmesh.cellGrady
|
||||||
self._Wyy = self._Pac.T*self._Wyy*self._Pac
|
|
||||||
return self._Wyy
|
return self._Wyy
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Wzz(self):
|
def Wzz(self):
|
||||||
"""Regularization matrix Wzz"""
|
"""Regularization matrix Wzz"""
|
||||||
if getattr(self, '_Wzz', None) is None:
|
if getattr(self, '_Wzz', None) is None:
|
||||||
Wzz = Utils.sdiag((self.mesh.vol*self.alpha_zz)**0.5)*self.mesh.faceDivz*self.mesh.cellGradz
|
self._Wzz = Utils.sdiag((self.regmesh.vol*self.alpha_zz)**0.5)*self.regmesh.faceDivz*self.regmesh.cellGradz
|
||||||
self._Wzz = self._Pac.T*Wzz*self._Pac
|
|
||||||
return self._Wzz
|
return self._Wzz
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -232,9 +360,9 @@ class Tikhonov(BaseRegularization):
|
|||||||
"""Full smoothness regularization matrix W"""
|
"""Full smoothness regularization matrix W"""
|
||||||
if getattr(self, '_Wsmooth', None) is None:
|
if getattr(self, '_Wsmooth', None) is None:
|
||||||
wlist = (self.Wx, self.Wxx)
|
wlist = (self.Wx, self.Wxx)
|
||||||
if self.mesh.dim > 1:
|
if self.regmesh.dim > 1:
|
||||||
wlist += (self.Wy, self.Wyy)
|
wlist += (self.Wy, self.Wyy)
|
||||||
if self.mesh.dim > 2:
|
if self.regmesh.dim > 2:
|
||||||
wlist += (self.Wz, self.Wzz)
|
wlist += (self.Wz, self.Wzz)
|
||||||
self._Wsmooth = sp.vstack(wlist)
|
self._Wsmooth = sp.vstack(wlist)
|
||||||
return self._Wsmooth
|
return self._Wsmooth
|
||||||
@@ -301,35 +429,35 @@ class Simple(BaseRegularization):
|
|||||||
alpha_y = Utils.dependentProperty('_alpha_y', 1.0, ['_W', '_Wy'], "Weight for the first derivative in the y direction")
|
alpha_y = Utils.dependentProperty('_alpha_y', 1.0, ['_W', '_Wy'], "Weight for the first derivative in the y direction")
|
||||||
alpha_z = Utils.dependentProperty('_alpha_z', 1.0, ['_W', '_Wz'], "Weight for the first derivative in the z direction")
|
alpha_z = Utils.dependentProperty('_alpha_z', 1.0, ['_W', '_Wz'], "Weight for the first derivative in the z direction")
|
||||||
|
|
||||||
def __init__(self, mesh, mapping=None, **kwargs):
|
def __init__(self, mesh, mapping=None, indActive=None, **kwargs):
|
||||||
BaseRegularization.__init__(self, mesh, mapping=mapping, **kwargs)
|
BaseRegularization.__init__(self, mesh, mapping=mapping, indActive=indActive, **kwargs)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Ws(self):
|
def Ws(self):
|
||||||
"""Regularization matrix Ws"""
|
"""Regularization matrix Ws"""
|
||||||
if getattr(self,'_Ws', None) is None:
|
if getattr(self,'_Ws', None) is None:
|
||||||
self._Ws = Utils.sdiag((self.mesh.vol*self.alpha_s)**0.5)
|
self._Ws = Utils.sdiag((self.regmesh.vol*self.alpha_s)**0.5)
|
||||||
return self._Ws
|
return self._Ws
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Wx(self):
|
def Wx(self):
|
||||||
"""Regularization matrix Wx"""
|
"""Regularization matrix Wx"""
|
||||||
if getattr(self, '_Wx', None) is None:
|
if getattr(self, '_Wx', None) is None:
|
||||||
self._Wx = Utils.sdiag((self.mesh.vol*self.alpha_x)**0.5)*self.mesh.unitCellGradx
|
self._Wx = Utils.sdiag((self.regmesh.aveCC2Fx * self.regmesh.vol*self.alpha_x)**0.5)*self.regmesh.cellGradxStencil
|
||||||
return self._Wx
|
return self._Wx
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Wy(self):
|
def Wy(self):
|
||||||
"""Regularization matrix Wy"""
|
"""Regularization matrix Wy"""
|
||||||
if getattr(self, '_Wy', None) is None:
|
if getattr(self, '_Wy', None) is None:
|
||||||
self._Wy = Utils.sdiag((self.mesh.vol*self.alpha_y)**0.5)*self.mesh.unitCellGrady
|
self._Wy = Utils.sdiag((self.regmesh.aveCC2Fy * self.regmesh.vol * self.alpha_y)**0.5)*self.regmesh.cellGradyStencil
|
||||||
return self._Wy
|
return self._Wy
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Wz(self):
|
def Wz(self):
|
||||||
"""Regularization matrix Wz"""
|
"""Regularization matrix Wz"""
|
||||||
if getattr(self, '_Wz', None) is None:
|
if getattr(self, '_Wz', None) is None:
|
||||||
self._Wz = Utils.sdiag((self.mesh.vol*self.alpha_z)**0.5)*self.mesh.unitCellGradz
|
self._Wz = Utils.sdiag((self.regmesh.aveCC2Fz * self.regmesh.vol*self.alpha_z)**0.5)*self.regmesh.cellGradzStencil
|
||||||
return self._Wz
|
return self._Wz
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -337,9 +465,9 @@ class Simple(BaseRegularization):
|
|||||||
"""Full smoothness regularization matrix W"""
|
"""Full smoothness regularization matrix W"""
|
||||||
if getattr(self, '_Wsmooth', None) is None:
|
if getattr(self, '_Wsmooth', None) is None:
|
||||||
wlist = (self.Wx,)
|
wlist = (self.Wx,)
|
||||||
if self.mesh.dim > 1:
|
if self.regmesh.dim > 1:
|
||||||
wlist += (self.Wy,)
|
wlist += (self.Wy,)
|
||||||
if self.mesh.dim > 2:
|
if self.regmesh.dim > 2:
|
||||||
wlist += (self.Wz,)
|
wlist += (self.Wz,)
|
||||||
self._Wsmooth = sp.vstack(wlist)
|
self._Wsmooth = sp.vstack(wlist)
|
||||||
return self._Wsmooth
|
return self._Wsmooth
|
||||||
@@ -352,25 +480,16 @@ class Simple(BaseRegularization):
|
|||||||
self._W = sp.vstack(wlist)
|
self._W = sp.vstack(wlist)
|
||||||
return self._W
|
return self._W
|
||||||
|
|
||||||
@Utils.timeIt
|
|
||||||
def _evalSmall(self, m):
|
|
||||||
r = self.W * ( self.mapping * (m - self.mref) )
|
|
||||||
return 0.5*r.dot(r)
|
|
||||||
|
|
||||||
@Utils.timeIt
|
@Utils.timeIt
|
||||||
def _evalSmooth(self, m):
|
def eval(self, m):
|
||||||
if self.smoothModel == True:
|
if self.smoothModel == True:
|
||||||
r1 = self.Wsmooth * ( self.mapping * (m) )
|
r1 = self.Wsmooth * ( self.mapping * (m) )
|
||||||
r2 = self.Ws * ( self.mapping * (m - self.mref) )
|
r2 = self.Ws * ( self.mapping * (m - self.mref) )
|
||||||
return 0.5*(r1.dot(r1)+r2.dot(r2))
|
return 0.5*(r1.dot(r1)+r2.dot(r2))
|
||||||
else:
|
elif self.smoothModel == False:
|
||||||
return None
|
r = self.W * ( self.mapping * (m - self.mref) )
|
||||||
|
return 0.5*r.dot(r)
|
||||||
@Utils.timeIt
|
|
||||||
def eval(self, m):
|
|
||||||
phim = self._evalSmall(m)
|
|
||||||
if self.smoothModel is True:
|
|
||||||
phim += self._evalSmooth(m)
|
|
||||||
return phim
|
return phim
|
||||||
|
|
||||||
|
|
||||||
@@ -417,34 +536,15 @@ class SparseRegularization(Simple):
|
|||||||
qy = 2.
|
qy = 2.
|
||||||
qz = 2.
|
qz = 2.
|
||||||
|
|
||||||
def __init__(self, mesh, mapping=None, **kwargs):
|
def __init__(self, mesh, mapping=None, indActive=None, **kwargs):
|
||||||
Simple.__init__(self, mesh, mapping=mapping, **kwargs)
|
Simple.__init__(self, mesh, mapping=mapping, indActive=indActive, **kwargs)
|
||||||
|
|
||||||
@property
|
|
||||||
def Wsmooth(self):
|
|
||||||
"""Full smoothness regularization matrix W"""
|
|
||||||
if getattr(self, '_Wsmooth', None) is None:
|
|
||||||
wlist = (self.Wx, self.Wxx)
|
|
||||||
if self.mesh.dim > 1:
|
|
||||||
wlist += (self.Wy, self.Wyy)
|
|
||||||
if self.mesh.dim > 2:
|
|
||||||
wlist += (self.Wz, self.Wzz)
|
|
||||||
self._Wsmooth = sp.vstack(wlist)
|
|
||||||
return self._Wsmooth
|
|
||||||
|
|
||||||
@property
|
|
||||||
def W(self):
|
|
||||||
"""Full regularization matrix W"""
|
|
||||||
if getattr(self, '_W', None) is None:
|
|
||||||
wlist = (self.Ws, self.Wsmooth)
|
|
||||||
self._W = sp.vstack(wlist)
|
|
||||||
return self._W
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Ws(self):
|
def Ws(self):
|
||||||
"""Regularization matrix Ws"""
|
"""Regularization matrix Ws"""
|
||||||
if getattr(self, 'm', None) is None:
|
if getattr(self, 'm', None) is None:
|
||||||
self.Rs = Utils.speye(self.mesh.nC)
|
self.Rs = Utils.speye(self.regmesh.nC)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
f_m = self.m
|
f_m = self.m
|
||||||
@@ -452,7 +552,7 @@ class SparseRegularization(Simple):
|
|||||||
#print "Min rs: " + str(np.max(self.rs)) + "Max rs: " + str(np.min(self.rs))
|
#print "Min rs: " + str(np.max(self.rs)) + "Max rs: " + str(np.min(self.rs))
|
||||||
self.Rs = Utils.sdiag( self.rs )
|
self.Rs = Utils.sdiag( self.rs )
|
||||||
|
|
||||||
self._Ws = Utils.sdiag((self.mesh.vol*self.alpha_s*self.gamma)**0.5)*self.Rs
|
self._Ws = Utils.sdiag((self.regmesh.vol*self.alpha_s*self.gamma)**0.5)*self.Rs
|
||||||
|
|
||||||
return self._Ws
|
return self._Ws
|
||||||
|
|
||||||
@@ -461,15 +561,15 @@ class SparseRegularization(Simple):
|
|||||||
"""Regularization matrix Wx"""
|
"""Regularization matrix Wx"""
|
||||||
|
|
||||||
if getattr(self, 'm', None) is None:
|
if getattr(self, 'm', None) is None:
|
||||||
self.Rx = Utils.speye(self.mesh.unitCellGradx.shape[0])
|
self.Rx = Utils.speye(self.regmesh.cellGradxStencil.shape[0])
|
||||||
|
|
||||||
else:
|
else:
|
||||||
f_m = self.mesh.unitCellGradx * self.m
|
f_m = self.regmesh.cellGradxStencil * self.m
|
||||||
self.rx = self.R( f_m , self.qx, self.eps)
|
self.rx = self.R( f_m , self.qx, self.eps)
|
||||||
self.Rx = Utils.sdiag( self.rx )
|
self.Rx = Utils.sdiag( self.rx )
|
||||||
|
|
||||||
if getattr(self, '_Wx', None) is None:
|
if getattr(self, '_Wx', None) is None:
|
||||||
self._Wx = Utils.sdiag((self.mesh.vol*self.alpha_x*self.gamma)**0.5)*self.Rx*self.mesh.unitCellGradx
|
self._Wx = Utils.sdiag(( (self.regmesh.aveCC2Fx * self.regmesh.vol) *self.alpha_x*self.gamma)**0.5)*self.Rx*self.regmesh.cellGradxStencil
|
||||||
return self._Wx
|
return self._Wx
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -477,15 +577,15 @@ class SparseRegularization(Simple):
|
|||||||
"""Regularization matrix Wy"""
|
"""Regularization matrix Wy"""
|
||||||
|
|
||||||
if getattr(self, 'm', None) is None:
|
if getattr(self, 'm', None) is None:
|
||||||
self.Ry = Utils.speye(self.mesh.unitCellGrady.shape[0])
|
self.Ry = Utils.speye(self.regmesh.cellGradyStencil.shape[0])
|
||||||
|
|
||||||
else:
|
else:
|
||||||
f_m = self.mesh.unitCellGrady * self.m
|
f_m = self.regmesh.cellGradyStencil * self.m
|
||||||
self.ry = self.R( f_m , self.qy, self.eps)
|
self.ry = self.R( f_m , self.qy, self.eps)
|
||||||
self.Ry = Utils.sdiag( self.ry )
|
self.Ry = Utils.sdiag( self.ry )
|
||||||
|
|
||||||
if getattr(self, '_Wy', None) is None:
|
if getattr(self, '_Wy', None) is None:
|
||||||
self._Wy = Utils.sdiag((self.mesh.vol*self.alpha_y*self.gamma)**0.5)*self.Ry*self.mesh.unitCellGrady
|
self._Wy = Utils.sdiag(((self.regmesh.aveCC2Fy * self.regmesh.vol)*self.alpha_y*self.gamma)**0.5)*self.Ry*self.regmesh.cellGradyStencil
|
||||||
return self._Wy
|
return self._Wy
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -493,15 +593,15 @@ class SparseRegularization(Simple):
|
|||||||
"""Regularization matrix Wz"""
|
"""Regularization matrix Wz"""
|
||||||
|
|
||||||
if getattr(self, 'm', None) is None:
|
if getattr(self, 'm', None) is None:
|
||||||
self.Rz = Utils.speye(self.mesh.unitCellGradz.shape[0])
|
self.Rz = Utils.speye(self.regmesh.cellGradzStencil.shape[0])
|
||||||
|
|
||||||
else:
|
else:
|
||||||
f_m = self.mesh.unitCellGradz * self.m
|
f_m = self.regmesh.cellGradzStencil * self.m
|
||||||
self.rz = self.R( f_m , self.qz, self.eps)
|
self.rz = self.R( f_m , self.qz, self.eps)
|
||||||
self.Rz = Utils.sdiag( self.rz )
|
self.Rz = Utils.sdiag( self.rz )
|
||||||
|
|
||||||
if getattr(self, '_Wz', None) is None:
|
if getattr(self, '_Wz', None) is None:
|
||||||
self._Wz = Utils.sdiag((self.mesh.vol*self.alpha_z*self.gamma)**0.5)*self.Rz*self.mesh.unitCellGradz
|
self._Wz = Utils.sdiag(((self.regmesh.aveCC2Fz * self.regmesh.vol)*self.alpha_z*self.gamma)**0.5)*self.Rz*self.regmesh.cellGradzStencil
|
||||||
return self._Wz
|
return self._Wz
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ from scipy.sparse.linalg import dsolve
|
|||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
TOL = 1e-20
|
TOL = 1e-20
|
||||||
|
testReg = True
|
||||||
|
testRegMesh = True
|
||||||
|
|
||||||
class RegularizationTests(unittest.TestCase):
|
class RegularizationTests(unittest.TestCase):
|
||||||
|
|
||||||
@@ -16,69 +18,92 @@ class RegularizationTests(unittest.TestCase):
|
|||||||
mesh3 = Mesh.TensorMesh([hx, hy, hz])
|
mesh3 = Mesh.TensorMesh([hx, hy, hz])
|
||||||
self.meshlist = [mesh1,mesh2, mesh3]
|
self.meshlist = [mesh1,mesh2, mesh3]
|
||||||
|
|
||||||
def test_regularization(self):
|
if testReg:
|
||||||
for R in dir(Regularization):
|
def test_regularization(self):
|
||||||
r = getattr(Regularization, R)
|
for R in dir(Regularization):
|
||||||
if not inspect.isclass(r): continue
|
r = getattr(Regularization, R)
|
||||||
if not issubclass(r, Regularization.BaseRegularization):
|
if not inspect.isclass(r): continue
|
||||||
continue
|
if not issubclass(r, Regularization.BaseRegularization):
|
||||||
|
continue
|
||||||
|
|
||||||
for i, mesh in enumerate(self.meshlist):
|
for i, mesh in enumerate(self.meshlist):
|
||||||
|
|
||||||
print 'Testing %iD'%mesh.dim
|
print 'Testing %iD'%mesh.dim
|
||||||
|
|
||||||
mapping = r.mapPair(mesh)
|
mapping = r.mapPair(mesh)
|
||||||
reg = r(mesh, mapping=mapping)
|
reg = r(mesh, mapping=mapping)
|
||||||
m = np.random.rand(mapping.nP)
|
m = np.random.rand(mapping.nP)
|
||||||
reg.mref = np.ones_like(m)*np.mean(m)
|
reg.mref = np.ones_like(m)*np.mean(m)
|
||||||
|
|
||||||
print 'Check: phi_m (mref) = %f' %reg.eval(reg.mref)
|
print 'Check: phi_m (mref) = %f' %reg.eval(reg.mref)
|
||||||
passed = reg.eval(reg.mref) < TOL
|
passed = reg.eval(reg.mref) < TOL
|
||||||
self.assertTrue(passed)
|
self.assertTrue(passed)
|
||||||
|
|
||||||
print 'Check:', R
|
print 'Check:', R
|
||||||
passed = Tests.checkDerivative(lambda m : [reg.eval(m), reg.evalDeriv(m)], m, plotIt=False)
|
passed = Tests.checkDerivative(lambda m : [reg.eval(m), reg.evalDeriv(m)], m, plotIt=False)
|
||||||
self.assertTrue(passed)
|
self.assertTrue(passed)
|
||||||
|
|
||||||
print 'Check 2 Deriv:', R
|
print 'Check 2 Deriv:', R
|
||||||
passed = Tests.checkDerivative(lambda m : [reg.evalDeriv(m), reg.eval2Deriv(m)], m, plotIt=False)
|
passed = Tests.checkDerivative(lambda m : [reg.evalDeriv(m), reg.eval2Deriv(m)], m, plotIt=False)
|
||||||
self.assertTrue(passed)
|
self.assertTrue(passed)
|
||||||
|
|
||||||
def test_regularization_ActiveCells(self):
|
def test_regularization_ActiveCells(self):
|
||||||
for R in dir(Regularization):
|
for R in dir(Regularization):
|
||||||
r = getattr(Regularization, R)
|
r = getattr(Regularization, R)
|
||||||
if not inspect.isclass(r): continue
|
if not inspect.isclass(r): continue
|
||||||
if not issubclass(r, Regularization.BaseRegularization):
|
if not issubclass(r, Regularization.BaseRegularization):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for i, mesh in enumerate(self.meshlist):
|
for i, mesh in enumerate(self.meshlist):
|
||||||
|
|
||||||
print 'Testing Active Cells %iD'%(mesh.dim)
|
print 'Testing Active Cells %iD'%(mesh.dim)
|
||||||
|
|
||||||
if mesh.dim == 1:
|
if mesh.dim == 1:
|
||||||
indAct = Utils.mkvc(mesh.gridCC <= 0.8)
|
indAct = Utils.mkvc(mesh.gridCC <= 0.8)
|
||||||
elif mesh.dim == 2:
|
elif mesh.dim == 2:
|
||||||
indAct = Utils.mkvc(mesh.gridCC[:,-1] <= 2*np.sin(2*np.pi*mesh.gridCC[:,0])+0.5)
|
indAct = Utils.mkvc(mesh.gridCC[:,-1] <= 2*np.sin(2*np.pi*mesh.gridCC[:,0])+0.5)
|
||||||
elif mesh.dim == 3:
|
elif mesh.dim == 3:
|
||||||
indAct = Utils.mkvc(mesh.gridCC[:,-1] <= 2*np.sin(2*np.pi*mesh.gridCC[:,0])+0.5 * 2*np.sin(2*np.pi*mesh.gridCC[:,1])+0.5)
|
indAct = Utils.mkvc(mesh.gridCC[:,-1] <= 2*np.sin(2*np.pi*mesh.gridCC[:,0])+0.5 * 2*np.sin(2*np.pi*mesh.gridCC[:,1])+0.5)
|
||||||
|
|
||||||
mapping = Maps.IdentityMap(nP=indAct.nonzero()[0].size)
|
mapping = Maps.IdentityMap(nP=indAct.nonzero()[0].size)
|
||||||
|
|
||||||
reg = r(mesh, mapping=mapping, indActive=indAct)
|
reg = r(mesh, mapping=mapping, indActive=indAct)
|
||||||
m = np.random.rand(mesh.nC)[indAct]
|
m = np.random.rand(mesh.nC)[indAct]
|
||||||
reg.mref = np.ones_like(m)*np.mean(m)
|
reg.mref = np.ones_like(m)*np.mean(m)
|
||||||
|
|
||||||
print 'Check: phi_m (mref) = %f' %reg.eval(reg.mref)
|
print 'Check: phi_m (mref) = %f' %reg.eval(reg.mref)
|
||||||
passed = reg.eval(reg.mref) < TOL
|
passed = reg.eval(reg.mref) < TOL
|
||||||
self.assertTrue(passed)
|
self.assertTrue(passed)
|
||||||
|
|
||||||
print 'Check:', R
|
print 'Check:', R
|
||||||
passed = Tests.checkDerivative(lambda m : [reg.eval(m), reg.evalDeriv(m)], m, plotIt=False)
|
passed = Tests.checkDerivative(lambda m : [reg.eval(m), reg.evalDeriv(m)], m, plotIt=False)
|
||||||
self.assertTrue(passed)
|
self.assertTrue(passed)
|
||||||
|
|
||||||
print 'Check 2 Deriv:', R
|
print 'Check 2 Deriv:', R
|
||||||
passed = Tests.checkDerivative(lambda m : [reg.evalDeriv(m), reg.eval2Deriv(m)], m, plotIt=False)
|
passed = Tests.checkDerivative(lambda m : [reg.evalDeriv(m), reg.eval2Deriv(m)], m, plotIt=False)
|
||||||
self.assertTrue(passed)
|
self.assertTrue(passed)
|
||||||
|
|
||||||
|
if testRegMesh:
|
||||||
|
def test_regularizationMesh(self):
|
||||||
|
|
||||||
|
for i, mesh in enumerate(self.meshlist):
|
||||||
|
|
||||||
|
print 'Testing %iD'%mesh.dim
|
||||||
|
|
||||||
|
# mapping = r.mapPair(mesh)
|
||||||
|
# reg = r(mesh, mapping=mapping)
|
||||||
|
# m = np.random.rand(mapping.nP)
|
||||||
|
|
||||||
|
if mesh.dim == 1:
|
||||||
|
indAct = Utils.mkvc(mesh.gridCC <= 0.8)
|
||||||
|
elif mesh.dim == 2:
|
||||||
|
indAct = Utils.mkvc(mesh.gridCC[:,-1] <= 2*np.sin(2*np.pi*mesh.gridCC[:,0])+0.5)
|
||||||
|
elif mesh.dim == 3:
|
||||||
|
indAct = Utils.mkvc(mesh.gridCC[:,-1] <= 2*np.sin(2*np.pi*mesh.gridCC[:,0])+0.5 * 2*np.sin(2*np.pi*mesh.gridCC[:,1])+0.5)
|
||||||
|
|
||||||
|
regmesh = Regularization.RegularizationMesh(mesh, indActive=indAct)
|
||||||
|
|
||||||
|
assert (regmesh.vol == mesh.vol[indAct]).all()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user