Skip to content
Snippets Groups Projects
Commit 0e0c86a1 authored by Timo Koch's avatar Timo Koch
Browse files

[python][black] Autoformat Python tests

parent d932c7a6
No related branches found
No related tags found
1 merge request!2681Feature/python main file
......@@ -38,6 +38,7 @@ python linting:
# only check the python folder for now (Python code related to bindings)
# TODO: maybe extend this to the utility scripts?
- black --check --verbose -- python
- black --check --verbose -- test/python
select tests:
......
......@@ -16,7 +16,9 @@ try:
discMethod = sys.argv[1]
diffMethod = sys.argv[2]
except IndexError:
print("No discretization method and differentiation method given. Defaulting to box and numeric diff.")
print(
"No discretization method and differentiation method given. Defaulting to box and numeric diff."
)
discMethod = "box"
diffMethod = "numeric"
......@@ -27,27 +29,30 @@ if diffMethod not in ["analytic", "numeric"]:
raise NotImplementedError(diffMethod + " must be analytic or numeric")
# Initialize the paramaters
parameters = Parameters({
"Problem.EnableGravity": True,
"SpatialParams.Porosity": 0.3,
"SpatialParams.Permeability": 1e-8,
"Vtk.AddVelocity": False,
"Assembly.NumericDifference.PriVarMagnitude": 1e5,
})
parameters = Parameters(
{
"Problem.EnableGravity": True,
"SpatialParams.Porosity": 0.3,
"SpatialParams.Permeability": 1e-8,
"Vtk.AddVelocity": False,
"Assembly.NumericDifference.PriVarMagnitude": 1e5,
}
)
# Set up the grid and the grid geometry
gridView = structuredGrid([0,0], [1,1], [10,10])
gridView = structuredGrid([0, 0], [1, 1], [10, 10])
gridGeometry = GridGeometry(gridView=gridView, discMethod=discMethod)
# Set up the model
model = Model(inheritsFrom=['OneP'], gridGeometry=gridGeometry)
model = Model(inheritsFrom=["OneP"], gridGeometry=gridGeometry)
# Tell Dumux to use a particular local residual type
model['LocalResidual'] = Property(
type='OnePIncompressibleLocalResidual<TypeTag>',
includes=['dumux/porousmediumflow/1p/incompressiblelocalresidual.hh']
model["LocalResidual"] = Property.fromCppType(
"OnePIncompressibleLocalResidual<TypeTag>",
cppIncludes=["dumux/porousmediumflow/1p/incompressiblelocalresidual.hh"],
)
@OnePSpatialParams(gridGeometry=gridGeometry)
class SpatialParams:
dimWorld = gridGeometry.gridView.dimWorld
......@@ -57,7 +62,9 @@ class SpatialParams:
def isLens(self, globalPos):
eps = 1.5e-7
for i in range(self.dimWorld):
if (globalPos[i] < self.lensLowerLeft[i] + eps) or (globalPos[i] > self.lensUpperRight[i] - eps):
if (globalPos[i] < self.lensLowerLeft[i] + eps) or (
globalPos[i] > self.lensUpperRight[i] - eps
):
return False
return True
......@@ -66,20 +73,20 @@ class SpatialParams:
# permeability can be either given
# as scalar or tensorial value
if self.isLens(globalPos):
return [[1e-12, 0],
[0, 1e-12]]
return [[1e-12, 0], [0, 1e-12]]
else:
return 1e-10
def porosityAtPos(self, globalPos):
return 0.4
spatialParams = SpatialParams()
model['SpatialParams'] = Property(object=spatialParams)
model["SpatialParams"] = Property.fromInstance(spatialParams)
h20 = Component(name="SimpleH2O")
onePLiquid = FluidSystem(type="OnePLiquid", component=h20, scalar=model['Scalar'])
model['FluidSystem'] = Property(object=onePLiquid)
h20 = Component("SimpleH2O")
onePLiquid = FluidSystem("OnePLiquid", component=h20, scalar=model["Scalar"])
model["FluidSystem"] = Property.fromInstance(onePLiquid)
# define the Problem
@PorousMediumFlowProblem(gridGeometry, spatialParams)
......@@ -98,8 +105,8 @@ class Problem:
return bTypes
def dirichletAtPos(self, globalPos):
dp_dy_ = -1.0e+5
return 1.0e+5 + dp_dy_*(globalPos[1] - gridGeometry.bBoxMax[1])
dp_dy_ = -1.0e5
return 1.0e5 + dp_dy_ * (globalPos[1] - gridGeometry.bBoxMax[1])
def sourceAtPos(self, globalPos):
return 0.0
......@@ -119,8 +126,9 @@ class Problem:
def neumann(self, element, fvGeometry, scvf):
return 0
problem = Problem()
model['Problem'] = Property(object=problem)
model["Problem"] = Property.fromInstance(problem)
# initialize the GridVariables and the Assembler
gridVars = GridVariables(problem=problem, model=model)
......@@ -149,5 +157,5 @@ testName = "test_1p_" + discMethod + "_" + diffMethod
output = VtkOutputModule(gridVariables=gridVars, solutionVector=sol, name=testName)
velocityoutput = PorousMediumFlowVelocityOutput(gridVariables=gridVars)
output.addVelocityOutput(velocityoutput)
output.addVolumeVariable(lambda vv : vv.pressure(), "p")
output.addVolumeVariable(lambda vv: vv.pressure(), "p")
output.write(1.0)
......@@ -9,6 +9,7 @@ import numpy as np
plotting = True
try:
import matplotlib.pyplot as plt
plt.ion()
except ImportError:
print("Warning: Plots are not generated as matplotlib could not be found.")
......@@ -21,7 +22,7 @@ except ImportError:
dimension = 2
cells = 20
gridView = structuredGrid([0]*dimension, [1]*dimension, [cells]*dimension)
gridView = structuredGrid([0] * dimension, [1] * dimension, [cells] * dimension)
gridGeometry = GridGeometry(gridView, discMethod="cctpfa")
......@@ -61,7 +62,7 @@ problem = Problem()
# Transport equation #
######################
velocity = FieldVector([1]*dimension)
velocity = FieldVector([1] * dimension)
upwindWeight = 1.0
......@@ -71,8 +72,9 @@ def advectiveFlux(insideConcentration, outsideConcentration, normal):
downwindConcentration = outsideConcentration
if normalVelocity < 0.0:
upwindConcentration, downwindConcentration = downwindConcentration, upwindConcentration
return normalVelocity*(upwindWeight*upwindConcentration
+ (1.0-upwindWeight)*downwindConcentration)
return normalVelocity * (
upwindWeight * upwindConcentration + (1.0 - upwindWeight) * downwindConcentration
)
##########################
......@@ -86,6 +88,7 @@ solution = np.zeros(gridGeometry.numDofs)
# Enable plotting #
###################
@gridFunction(gridView)
def solutionGridFunction(element, x):
elementIdx = elementMapper.index(element)
......@@ -96,13 +99,15 @@ def plot(time):
if plotting and dimension == 2:
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
solutionGridFunction.plot(figure=fig, clim=[0, 1+1e-6], gridLines=None)
ax.set_title("t = "+"{:0.2f}".format(time))
solutionGridFunction.plot(figure=fig, clim=[0, 1 + 1e-6], gridLines=None)
ax.set_title("t = " + "{:0.2f}".format(time))
plt.show()
plt.pause(1e-3)
gridView.writeVTK(problem.name + "-solution-{:0.2f}".format(time).replace(".", ""),
celldata={"solution": solutionGridFunction},
outputType=OutputType.ascii)
gridView.writeVTK(
problem.name + "-solution-{:0.2f}".format(time).replace(".", ""),
celldata={"solution": solutionGridFunction},
outputType=OutputType.ascii,
)
#######################
......
......@@ -14,6 +14,7 @@ def PrintProblemTest(problem):
module = generator.load(includes, typeName, moduleName)
return module.PrintProblemTest(problem)
############################################################
# The actual Python test
############################################################
......@@ -21,10 +22,11 @@ from dune.grid import structuredGrid
from dumux.discretization import GridGeometry
from dumux.common import BoundaryTypes, FVProblem
gridView = structuredGrid([0,0,0],[1,1,1],[3,3,3])
gridView = structuredGrid([0, 0, 0], [1, 1, 1], [3, 3, 3])
gridGeometry = GridGeometry(gridView, discMethod="box")
@FVProblem(gridGeometry)
class Problem:
numEq = 2
......@@ -46,6 +48,7 @@ class Problem:
def sourceAtPos(self, globalPos):
return [globalPos[0]]
problem = Problem()
print("Name of the problem: {}".format(problem.name))
print("-- Number of equations: {}".format(problem.numEq))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment