Skip to content

Commit

Permalink
create a pre-commit hook that runs black
Browse files Browse the repository at this point in the history
  • Loading branch information
lheagy committed May 26, 2020
1 parent 9cf4622 commit 43686b5
Show file tree
Hide file tree
Showing 332 changed files with 22,604 additions and 18,537 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
@@ -0,0 +1,6 @@
repos:
- repo: https://github.com/psf/black
rev: stable
hooks:
- id: black
language_version: python3.7
92 changes: 47 additions & 45 deletions SimPEG/_EM/Static/SP/ProblemSP.py
Expand Up @@ -13,13 +13,9 @@

class BaseSPProblem(BaseDCProblem):

h, hMap, hDeriv = Props.Invertible(
"Hydraulic Head (m)"
)
h, hMap, hDeriv = Props.Invertible("Hydraulic Head (m)")

q, qMap, qDeriv = Props.Invertible(
"Streaming current source (A/m^3)"
)
q, qMap, qDeriv = Props.Invertible("Streaming current source (A/m^3)")

jsx, jsxMap, jsxDeriv = Props.Invertible(
"Streaming current density in x-direction (A/m^2)"
Expand All @@ -33,13 +29,9 @@ class BaseSPProblem(BaseDCProblem):
"Streaming current density in z-direction (A/m^2)"
)

sigma = Props.PhysicalProperty(
"Electrical conductivity (S/m)"
)
sigma = Props.PhysicalProperty("Electrical conductivity (S/m)")

rho = Props.PhysicalProperty(
"Electrical resistivity (Ohm m)"
)
rho = Props.PhysicalProperty("Electrical resistivity (Ohm m)")

Props.Reciprocal(sigma, rho)

Expand All @@ -53,29 +45,29 @@ def deleteTheseOnModelUpdate(self):
return toDelete

def evalq(self, Qv, vel):
MfQviI = self.mesh.getFaceInnerProduct(1./Qv, invMat=True)
MfQviI = self.mesh.getFaceInnerProduct(1.0 / Qv, invMat=True)
Mf = self.mesh.getFaceInnerProduct()
return self.Div*(Mf*(MfQviI*vel))
return self.Div * (Mf * (MfQviI * vel))


class Problem_CC(BaseSPProblem, Simulation3DCellCentered):

_solutionType = 'phiSolution'
_formulation = 'HJ' # CC potentials means J is on faces
_solutionType = "phiSolution"
_formulation = "HJ" # CC potentials means J is on faces
fieldsPair = Fields3DCellCentered
modelType = None
bc_type = "Mixed"
coordinate_system = properties.StringChoice(
"Type of coordinate system we are regularizing in",
choices=['cartesian', 'spherical'],
default='cartesian'
choices=["cartesian", "spherical"],
default="cartesian",
)

def __init__(self, mesh, **kwargs):
BaseSPProblem.__init__(self, mesh, **kwargs)
self.setBC()

def getADeriv(self, u, v, adjoint= False):
def getADeriv(self, u, v, adjoint=False):
# We assume conductivity is known
return Zero()

Expand All @@ -96,7 +88,7 @@ def G(self):
"""
Inverse of :code:`_G`
"""
if getattr(self, '_G', None) is None:
if getattr(self, "_G", None) is None:
A = self.getA()
self.Ainv = self.Solver(A, **self.solverOpts)
src = self.survey.source_list[0]
Expand All @@ -112,7 +104,7 @@ def G(self):

def getJ(self, m, f=None):

if self.coordinate_system == 'cartesian':
if self.coordinate_system == "cartesian":
return self.G
else:
self.model = m
Expand All @@ -122,7 +114,7 @@ def Jvec(self, m, v, f=None):

self.model = m

if self.coordinate_system == 'cartesian':
if self.coordinate_system == "cartesian":
return self.G.dot(v)
else:
return np.dot(self.G, self.S.dot(v))
Expand All @@ -131,17 +123,17 @@ def Jtvec(self, m, v, f=None):

self.model = m

if self.coordinate_system == 'cartesian':
if self.coordinate_system == "cartesian":
return self.G.T.dot(v)
else:
return self.S.T*(self.G.T.dot(v))
return self.S.T * (self.G.T.dot(v))

@Utils.count
def fields(self, m):

self.model = m

if self.coordinate_system == 'spherical':
if self.coordinate_system == "spherical":
m = Utils.mat_utils.atp2xyz(m)

return self.G.dot(m)
Expand All @@ -151,30 +143,42 @@ def S(self):
"""
Derivatives for the spherical transformation
"""
if getattr(self, '_S', None) is None:
if getattr(self, "_S", None) is None:
if self.verbose:
print ("Compute S")
print("Compute S")
if self.model is None:
raise Exception('Requires a model')
raise Exception("Requires a model")

# Assume it is vector model in spherical coordinates
nC = int(self.model.shape[0]/3)
nC = int(self.model.shape[0] / 3)

a = self.model[:nC]
t = self.model[nC:2*nC]
p = self.model[2*nC:]

Sx = sp.hstack([sp.diags(np.cos(t)*np.cos(p), 0),
sp.diags(-a*np.sin(t)*np.cos(p), 0),
sp.diags(-a*np.cos(t)*np.sin(p), 0)])
t = self.model[nC : 2 * nC]
p = self.model[2 * nC :]

Sx = sp.hstack(
[
sp.diags(np.cos(t) * np.cos(p), 0),
sp.diags(-a * np.sin(t) * np.cos(p), 0),
sp.diags(-a * np.cos(t) * np.sin(p), 0),
]
)

Sy = sp.hstack([sp.diags(np.cos(t)*np.sin(p), 0),
sp.diags(-a*np.sin(t)*np.sin(p), 0),
sp.diags(a*np.cos(t)*np.cos(p), 0)])
Sy = sp.hstack(
[
sp.diags(np.cos(t) * np.sin(p), 0),
sp.diags(-a * np.sin(t) * np.sin(p), 0),
sp.diags(a * np.cos(t) * np.cos(p), 0),
]
)

Sz = sp.hstack([sp.diags(np.sin(t), 0),
sp.diags(a*np.cos(t), 0),
sp.csr_matrix((nC, nC))])
Sz = sp.hstack(
[
sp.diags(np.sin(t), 0),
sp.diags(a * np.cos(t), 0),
sp.csr_matrix((nC, nC)),
]
)

self._S = sp.vstack([Sx, Sy, Sz])

Expand All @@ -184,14 +188,12 @@ def S(self):
def deleteTheseOnModelUpdate(self):
toDelete = super(BaseDCProblem, self).deleteTheseOnModelUpdate
if self._S is not None:
toDelete += ['_S']
toDelete += ["_S"]
return toDelete


class SurveySP_store(Survey):
@Utils.count
@Utils.requires('prob')
@Utils.requires("prob")
def dpred(self, m=None, f=None):
return self.prob.fields(m)


72 changes: 35 additions & 37 deletions SimPEG/_EM/Static/SP/SrcSP.py
Expand Up @@ -18,9 +18,7 @@ def __init__(self, rxList, **kwargs):
Src.BaseSrc.__init__(self, rxList, **kwargs)
if self.modelType == "Head":
if self.L is None:
raise Exception(
"SP source requires cross coupling coefficient L"
)
raise Exception("SP source requires cross coupling coefficient L")
elif self.modelType == "CurrentDensity":
if self.indActive is None:
self.indActive = np.ones(self.mesh.nC, dtype=bool)
Expand All @@ -38,7 +36,7 @@ def __init__(self, rxList, **kwargs):
raise Exception("SP source requires mesh")

def getq_from_j(self, j):
q = self.Grad.T*self.mesh.aveCCV2F*j
q = self.Grad.T * self.mesh.aveCCV2F * j
return q

def eval(self, prob):
Expand All @@ -54,58 +52,57 @@ def eval(self, prob):
-\nabla \cdot \vec{j}^s = \nabla \cdot L \nabla \phi \\
"""
if prob._formulation == 'HJ':
if prob._formulation == "HJ":
if self.modelType == "Head":
q = prob.Grad.T*self.MfLiI*prob.Grad*prob.h
q = prob.Grad.T * self.MfLiI * prob.Grad * prob.h
elif self.modelType == "CurrentSource":
q = self.V * prob.q
elif self.modelType == "CurrentDensity":
q = self.Grad.T*self.mesh.aveCCV2F*np.r_[
prob.jsx, prob.jsy, prob.jsz
]
q = (
self.Grad.T
* self.mesh.aveCCV2F
* np.r_[prob.jsx, prob.jsy, prob.jsz]
)
else:
raise NotImplementedError()
elif prob._formulation == 'EB':
elif prob._formulation == "EB":
raise NotImplementedError()
return q

def evalDeriv(self, prob, v=None, adjoint=False):
if prob._formulation == 'HJ':
if prob._formulation == "HJ":
if adjoint:
if self.modelType == "Head":
srcDeriv = prob.hDeriv.T * prob.Grad.T * self.MfLiI.T * (
prob.Grad * v
srcDeriv = (
prob.hDeriv.T * prob.Grad.T * self.MfLiI.T * (prob.Grad * v)
)
elif self.modelType == "CurrentSource":
srcDeriv = prob.qDeriv.T * (self.V * v)
elif self.modelType == "CurrentDensity":
jsDeriv = sp.vstack(
(prob.jsxDeriv, prob.jsyDeriv, prob.jszDeriv)
)
srcDeriv = jsDeriv.T * self.mesh.aveCCV2F.T * (self.Grad*v)
jsDeriv = sp.vstack((prob.jsxDeriv, prob.jsyDeriv, prob.jszDeriv))
srcDeriv = jsDeriv.T * self.mesh.aveCCV2F.T * (self.Grad * v)
else:
raise NotImplementedError()
else:
if self.modelType == "Head":
srcDeriv = prob.Grad.T*self.MfLiI*prob.Grad*(prob.hDeriv*v)
srcDeriv = prob.Grad.T * self.MfLiI * prob.Grad * (prob.hDeriv * v)
elif self.modelType == "CurrentSource":
srcDeriv = self.V * (prob.qDeriv * v)
elif self.modelType == "CurrentDensity":
jsDeriv = sp.vstack(
(prob.jsxDeriv, prob.jsyDeriv, prob.jszDeriv)
)
srcDeriv = self.Grad.T * self.mesh.aveCCV2F*(jsDeriv*v)
jsDeriv = sp.vstack((prob.jsxDeriv, prob.jsyDeriv, prob.jszDeriv))
srcDeriv = self.Grad.T * self.mesh.aveCCV2F * (jsDeriv * v)
else:
raise NotImplementedError()
elif prob._formulation == 'EB':
elif prob._formulation == "EB":
raise NotImplementedError()
return srcDeriv

@property
def V(self):
"""
:code:`V`
"""
if getattr(self, '_V', None) is None:
if getattr(self, "_V", None) is None:
self._V = Utils.sdiag(self.mesh.vol)
return self._V

Expand All @@ -114,17 +111,17 @@ def MfLi(self):
"""
:code:`MfLi`
"""
if getattr(self, '_MfLi', None) is None:
self._MfLi = self.mesh.getFaceInnerProduct(1./self.L)
if getattr(self, "_MfLi", None) is None:
self._MfLi = self.mesh.getFaceInnerProduct(1.0 / self.L)
return seself.lf._MfLi

@property
def MfLiI(self):
"""
Inverse of :code:`_MfLiI`
"""
if getattr(self, '_MfLiI', None) is None:
self._MfLiI = self.mesh.getFaceInnerProduct(1./self.L, invMat=True)
if getattr(self, "_MfLiI", None) is None:
self._MfLiI = self.mesh.getFaceInnerProduct(1.0 / self.L, invMat=True)
return self._MfLiI

@property
Expand All @@ -135,12 +132,12 @@ def Pac(self):
:rtype: scipy.sparse.csr_matrix
:return: active cell diagonal matrix
"""
if getattr(self, '_Pac', None) is None:
if getattr(self, "_Pac", None) is None:
if self.indActive is None:
self._Pac = Utils.speye(self.mesh.nC)
else:
e = np.zeros(self.mesh.nC)
e[self.indActive] = 1.
e[self.indActive] = 1.0
# self._Pac = Utils.speye(self.mesh.nC)[:, self.indActive]
self._Pac = Utils.sdiag(e)
return self._Pac
Expand All @@ -154,13 +151,13 @@ def Pafx(self):
:rtype: scipy.sparse.csr_matrix
:return: active face-x diagonal matrix
"""
if getattr(self, '_Pafx', None) is None:
if getattr(self, "_Pafx", None) is None:
if self.indActive is None:
self._Pafx = Utils.speye(self.mesh.nFx)
else:
indActive_Fx = self.mesh.aveFx2CC.T * self.indActive >= 1
e = np.zeros(self.mesh.nFx)
e[indActive_Fx] = 1.
e[indActive_Fx] = 1.0
self._Pafx = Utils.sdiag(e)
return self._Pafx

Expand All @@ -173,13 +170,13 @@ def Pafy(self):
:rtype: scipy.sparse.csr_matrix
:return: active face-y diagonal matrix
"""
if getattr(self, '_Pafy', None) is None:
if getattr(self, "_Pafy", None) is None:
if self.indActive is None:
self._Pafy = Utils.speye(self.mesh.nFy)
else:
indActive_Fy = (self.mesh.aveFy2CC.T * self.indActive) >= 1
e = np.zeros(self.mesh.nFy)
e[indActive_Fy] = 1.
e[indActive_Fy] = 1.0
self._Pafy = Utils.sdiag(e)
return self._Pafy

Expand All @@ -192,19 +189,20 @@ def Pafz(self):
:rtype: scipy.sparse.csr_matrix
:return: active face-z diagonal matrix
"""
if getattr(self, '_Pafz', None) is None:
if getattr(self, "_Pafz", None) is None:
if self.indActive is None:
self._Pafz = Utils.speye(self.mesh.nFz)
else:
indActive_Fz = (self.mesh.aveFz2CC.T * self.indActive) >= 1
e = np.zeros(self.mesh.nFz)
e[indActive_Fz] = 1.
e[indActive_Fz] = 1.0
self._Pafz = Utils.sdiag(e)
return self._Pafz


if __name__ == '__main__':
if __name__ == "__main__":
from SimPEG import Mesh, np

mesh = Mesh.TensorMesh([10, 10])
L = np.ones(mesh.nC)
src = StreamingCurrents([], L=L, mesh=mesh)
Expand Down

0 comments on commit 43686b5

Please sign in to comment.