Skip to content

Commit

Permalink
Silence more warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
cgcgcg committed Dec 20, 2023
1 parent 473d88c commit e1ca2c3
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 63 deletions.
1 change: 1 addition & 0 deletions base/PyNucleus_base/plot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def myFmt(a, fmt):


def formatScientificLatex(a, useEnotation=True):
import numpy as np
if abs(a) > 0:
exp = int(np.floor(np.log10(a)))
mantissa = a/10**exp
Expand Down
4 changes: 3 additions & 1 deletion base/PyNucleus_base/solver_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def build(self, name, **kwargs):
kwargs.pop('A', None)
hierarchy = kwargs.pop('hierarchy')
smoother = kwargs.pop('smoother', 'jacobi')
if not isinstance(hierarchy, list) and isinstance(hierarchy.builtHierarchies[-1].algebraicLevels[-1].A, ComplexLinearOperator) and self.multiLevelSolverFactory.isRegistered('complex_'+name):
if (not isinstance(hierarchy, list) and
isinstance(hierarchy.builtHierarchies[-1].algebraicLevels[-1].A, ComplexLinearOperator) and
self.multiLevelSolverFactory.isRegistered('complex_'+name)):
name = 'complex_'+name
solver = self.multiLevelSolverFactory.build(name, hierarchy, smoother, **kwargs)
else:
Expand Down
65 changes: 37 additions & 28 deletions base/PyNucleus_base/utilsFem.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,20 +262,20 @@ def saveDictToHDF5(params, f, ignore=set()):
if isinstance(val[0], list) and isinstance(val[0][0], (int, float, INDEX, REAL)):
g = f.create_group(key)
g.attrs['type'] = 'compressedList'
l = 0
listItems = 0
for i in range(len(val)):
l += len(val[i])
listItems += len(val[i])
indptr = uninitialized((len(val)+1), dtype=INDEX)
if isinstance(val[0][0], (int, INDEX)):
data = uninitialized((l), dtype=INDEX)
data = uninitialized((listItems), dtype=INDEX)
else:
data = uninitialized((l), dtype=REAL)
l = 0
data = uninitialized((listItems), dtype=REAL)
listItems = 0
for i in range(len(val)):
indptr[i] = l
data[l:l+len(val[i])] = val[i]
l += len(val[i])
indptr[-1] = l
indptr[i] = listItems
data[listItems:listItems+len(val[i])] = val[i]
listItems += len(val[i])
indptr[-1] = listItems
g.create_dataset('indptr', data=indptr)
g.create_dataset('data', data=data)
elif isinstance(val[0], str):
Expand Down Expand Up @@ -325,20 +325,20 @@ def loadDictFromHDF5(f):
if isinstance(f[key], h5py.Group):
if 'type' in f[key].attrs:
if f[key].attrs['type'] == 'list':
l = []
myList = []
for k in range(len(f[key].attrs)-1):
l.append(f[key].attrs[str(k)])
params[key] = l
myList.append(f[key].attrs[str(k)])
params[key] = myList
elif f[key].attrs['type'] == 'compressedList':
l = []
myCompressedList = []
indptr = np.array(f[key]['indptr'], dtype=INDEX)
if isinstance(f[key]['data'], (int, INDEX)):
data = np.array(f[key]['data'], dtype=INDEX)
else:
data = np.array(f[key]['data'], dtype=REAL)
for i in range(len(indptr)-1):
l.append(data[indptr[i]:indptr[i+1]].tolist())
params[key] = l
myCompressedList.append(data[indptr[i]:indptr[i+1]].tolist())
params[key] = myCompressedList
elif f[key].attrs['type'] == 'series':
d = loadDictFromHDF5(f[key])
grp = seriesOutputGroup(key)
Expand All @@ -359,10 +359,10 @@ def loadDictFromHDF5(f):
else:
params[key] = np.array(f[key])
try:
l = []
myList = []
for i in range(len(params[key])):
l.append(params[key][i].decode('utf-8'))
params[key] = l
myList.append(params[key][i].decode('utf-8'))
params[key] = myList
except:
pass
return params
Expand Down Expand Up @@ -501,7 +501,8 @@ def __init__(self, filename, comm, mode=MPI.MODE_WRONLY | MPI.MODE_CREATE):
# keep the absolute path, otherwise derived classes which use this
# may come a cropper when the current directory changes
self.baseFilename = os.path.abspath(filename)
assert len(self.baseFilename) <= 245, 'The length of the log file path \"{}\" is too long and will probably crash MPI. Try running with \"--disableFileLog\"'.format(self.baseFilename)
assert len(self.baseFilename) <= 245, ('The length of the log file path \"{}\" is too long ' +
'and will probably crash MPI. Try running with \"--disableFileLog\"').format(self.baseFilename)
if Path(self.baseFilename).exists() and comm.rank == 0:
from os import remove
remove(self.baseFilename)
Expand Down Expand Up @@ -538,16 +539,16 @@ def close(self):

def columns(lines, returnColWidth=False, colWidth=0):
if colWidth == 0:
for l, _, _ in lines:
colWidth = max(len(l), colWidth)
for line, _, _ in lines:
colWidth = max(len(line), colWidth)
s = []
for l, f, v in lines:
for line, f, v in lines:
if isinstance(f, str):
lf = '{:<'+str(colWidth+2)+'}'+f
s.append(lf.format(l+':', v))
s.append(lf.format(line+':', v))
else:
lf = '{:<'+str(colWidth+2)+'}'+'{}'
s.append(lf.format(l+':', f(v)))
s.append(lf.format(line+':', f(v)))
s = '\n'.join(s)
if returnColWidth:
return s, colWidth
Expand Down Expand Up @@ -1488,18 +1489,22 @@ def __call__(self):
if isinstance(newValue, np.ndarray):
cached_args[prop] = newValue.copy()
if (newValue != oldValue).any():
dependencyLogger.log(self.logLevel, 'Values for {} differ: \'{}\' != \'{}\', calling \'{}\''.format(prop, oldValue, newValue, self.fun.__name__))
dependencyLogger.log(self.logLevel, 'Values for {} differ: \'{}\' != \'{}\', calling \'{}\''.format(prop, oldValue,
newValue, self.fun.__name__))
needToBuild = True
else:
dependencyLogger.log(self.logLevel, 'Values for {} are identical: \'{}\' == \'{}\''.format(prop, oldValue, newValue))
elif newValue != oldValue:
cached_args[prop] = newValue
dependencyLogger.log(self.logLevel, 'Values for {} differ: \'{}\' != \'{}\', calling \'{}\''.format(prop, oldValue, newValue, self.fun.__name__))
dependencyLogger.log(self.logLevel, 'Values for {} differ: \'{}\' != \'{}\', calling \'{}\''.format(prop, oldValue,
newValue, self.fun.__name__))
needToBuild = True
else:
dependencyLogger.log(self.logLevel, 'Values for {} are identical: \'{}\' == \'{}\''.format(prop, oldValue, newValue))
except Exception as e:
dependencyLogger.log(logging.WARN, 'Cannot compare values {}, {} for property \'{}\', exception {}, force call \'{}\''.format(oldValue, newValue, prop, e, self.fun.__name__))
dependencyLogger.log(logging.WARN,
'Cannot compare values {}, {} for property \'{}\', exception {}, force call \'{}\''.format(oldValue, newValue,
prop, e, self.fun.__name__))
needToBuild = True
except AttributeError:
raise AttributeError('Method \'{}\' has unsatisfied dependency on \'{}\''.format(self.fun.__name__, prop))
Expand Down Expand Up @@ -1768,6 +1773,7 @@ def changeLogLevel(self, properties, logLevel):
class driverAddon:
def __init__(self, driver):
self.driver = driver
self._timer = None
self.__parametrized_args__ = {}
self.flags = []
self.setDriverArgs()
Expand All @@ -1788,7 +1794,10 @@ def interpreter(v):
for p in parametrizedArgs:
if self.parametrizedArg(p).match(v):
return v
raise ArgumentTypeError("\"{}\" is not in list of accepted values {} or cannot be interpreted as parametrized arg {}.".format(v, acceptedValues, [repr(self.parametrizedArg(p)) for p in parametrizedArgs]))
raise ArgumentTypeError(("\"{}\" is not in list of accepted values {} " +
"or cannot be interpreted as parametrized arg {}.").format(v, acceptedValues,
[repr(self.parametrizedArg(p))
for p in parametrizedArgs]))

return interpreter

Expand Down
15 changes: 10 additions & 5 deletions drivers/runParallelGMG.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,10 @@
P0.num_columns = numGlobalDoFs0
P.append(P0.to_csr())

coords.append(d.comm.reduce(P[-1].T @ (overlaps.getDistributeAsDiagonalOperator(lvl).to_csr()@hM[FINE].algebraicLevels[lvl].DoFMap.getDoFCoordinates())))
nullspace.append(d.comm.reduce(P[-1].T @ (overlaps.getDistributeAsDiagonalOperator(lvl).to_csr()@hM[FINE].algebraicLevels[lvl].DoFMap.ones())))
coords.append(d.comm.reduce(P[-1].T @ (overlaps.getDistributeAsDiagonalOperator(lvl).to_csr() @
hM[FINE].algebraicLevels[lvl].DoFMap.getDoFCoordinates())))
nullspace.append(d.comm.reduce(P[-1].T @ (overlaps.getDistributeAsDiagonalOperator(lvl).to_csr() @
hM[FINE].algebraicLevels[lvl].DoFMap.ones())))

A.append(d.comm.reduce((P[-1].T @ hM[FINE].algebraicLevels[lvl].A.to_csr() @ P[-1]).tocsr()))

Expand All @@ -294,7 +296,8 @@
for k in range(len(lvls)-1):
lvlC = lvls[k]
lvlF = lvls[k+1]
P_ops.append(d.comm.reduce((P[lvlF].T @ overlaps.getDistributeAsDiagonalOperator(lvlF).to_csr() @ hM[FINE].algebraicLevels[lvlF].P.to_csr() @ P[lvlC]).tocsr()))
P_ops.append(d.comm.reduce((P[lvlF].T @ overlaps.getDistributeAsDiagonalOperator(lvlF).to_csr() @
hM[FINE].algebraicLevels[lvlF].P.to_csr() @ P[lvlC]).tocsr()))

if d.comm.rank == 0:
from scipy.io import mmwrite
Expand All @@ -319,8 +322,10 @@
# A_global = CSR_LinearOperator.from_csr(A_global)
# A2_global = CSR_LinearOperator.from_csr(A2_global)
# P_global = CSR_LinearOperator.from_csr(P_global)
# mg = solverFactory('mg', hierarchy=[{'A': A2_global}, {'A': A_global, 'P': P_global, 'R': P_global.transpose()}], setup=True, smoother=('jacobi', {'presmoothingSteps': 2,
# 'postsmoothingSteps': 2}))
# mg = solverFactory('mg', hierarchy=[{'A': A2_global}, {'A': A_global, 'P': P_global, 'R': P_global.transpose()}],
# setup=True,
# smoother=('jacobi', {'presmoothingSteps': 2,
# 'postsmoothingSteps': 2}))
# print(mg)
# cg = solverFactory('cg', A=A_global, setup=True, maxIter=d.maxiter, tolerance=tol)
# cg.setPreconditioner(mg.asPreconditioner(), False)
Expand Down
3 changes: 3 additions & 0 deletions fem/PyNucleus_fem/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ def __call__(self, mesh, *args, **kwargs):
meshFactory.register('circle', circle, 2, aliases=['disc', 'unitDisc', 'ball2d', '2dball'])
meshFactory.register('graded_circle', graded_circle, 2, aliases=['gradedCircle'])
meshFactory.register('discWithInteraction', discWithInteraction, 2)
meshFactory.register('twinDisc', twinDisc, 2)
meshFactory.register('dumbbell', dumbbell, 2)
meshFactory.register('wrench', wrench, 2)
meshFactory.register('cutoutCircle', cutoutCircle, 2, aliases=['cutoutDisc'])
meshFactory.register('squareWithCircularCutout', squareWithCircularCutout, 2)
meshFactory.register('boxWithBallCutout', boxWithBallCutout, 3, aliases=['boxMinusBall'])
Expand Down
18 changes: 9 additions & 9 deletions fem/PyNucleus_fem/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ def doubleIntervalWithInteractions(a=0., b=1., c=2.,
horizon1=0.1, horizon2=0.2,
h=None):

def getNumCells(l, r):
def getNumCells(left, right):
eps = 1e-8
return int(np.ceil((r-l-eps)/h))
return int(np.ceil((right-left-eps)/h))

assert horizon2 >= horizon1
assert horizon1 >= 0
Expand Down Expand Up @@ -341,7 +341,7 @@ def squareWithInteractions(ax, ay, bx, by,
d1 = (circularSegment(bottomLeft, horizon, np.pi, 1.5*np.pi, numPointsPerUnitLength) +
line(bottomLeft, bottomLeft-horizontalOffset) +
line(bottomLeft, bottomLeft-verticalOffset) +
(lineHorizontal+bottomLeft)+
(lineHorizontal+bottomLeft) +
(lineHorizontal+(bottomLeft-verticalOffset)))

d2 = (circularSegment(bottomRight, horizon, -0.5*np.pi, 0., numPointsPerUnitLength) +
Expand Down Expand Up @@ -386,11 +386,11 @@ def squareWithInteractions(ax, ay, bx, by,
assert np.allclose(yVals1, yVals2), (yVals1, yVals2)

idx3 = np.logical_and(np.absolute(mesh.vertices_as_array[:, 1]-ay) < eps,
np.logical_and(mesh.vertices_as_array[:, 0] >= ax-eps,
mesh.vertices_as_array[:, 0] <= bx+eps))
np.logical_and(mesh.vertices_as_array[:, 0] >= ax-eps,
mesh.vertices_as_array[:, 0] <= bx+eps))
idx4 = np.logical_and(np.absolute(mesh.vertices_as_array[:, 1]-by) < eps,
np.logical_and(mesh.vertices_as_array[:, 0] >= ax-eps,
mesh.vertices_as_array[:, 0] <= bx+eps))
np.logical_and(mesh.vertices_as_array[:, 0] >= ax-eps,
mesh.vertices_as_array[:, 0] <= bx+eps))
xVals3 = np.sort(mesh.vertices_as_array[idx3, 0])
xVals4 = np.sort(mesh.vertices_as_array[idx4, 0])
assert np.allclose(xVals3, xVals4), (xVals3, xVals4)
Expand Down Expand Up @@ -2676,7 +2676,7 @@ class mesh3d(meshNd):

def plot(self):
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import Axes3D # noqa: F401
from itertools import combinations
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
Expand All @@ -2688,7 +2688,7 @@ def plot(self):

def plot_surface(self, boundary=False):
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import Axes3D # noqa: F401
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
# from matplotlib import rcParams
# from itertools import combinations
Expand Down
4 changes: 2 additions & 2 deletions fem/PyNucleus_fem/meshConstruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def rectangle(a, b, num_points=None, num_points_per_unit_len=None):
def meshTransformation(x1, x2, xNew):
eps = 1e-10
if ((a[0]-eps <= x1[0] <= b[0]+eps) and (a[1]-eps <= x1[1] <= b[1]+eps) and
(a[0]-eps <= x2[0] <= b[0]+eps) and (a[1]-eps <= x2[1] <= b[1]+eps)):
(a[0]-eps <= x2[0] <= b[0]+eps) and (a[1]-eps <= x2[1] <= b[1]+eps)):
xNew[:] = 0.5*(x1+x2)
return True

Expand All @@ -290,6 +290,6 @@ def __init__(self, seg, p1, p2):
for t in seg.meshTransformations:
def transform(x1, x2, xNew):
if ((p1[0] <= xNew[0]) and (xNew[0] <= p2[0]) and
(p1[1] <= xNew[1]) and (xNew[1] <= p2[1])):
(p1[1] <= xNew[1]) and (xNew[1] <= p2[1])):
t(x1, x2, xNew)
self.meshTransformations.append(transform)
18 changes: 9 additions & 9 deletions multilevelSolver/PyNucleus_multilevelSolver/connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
from mpi4py import MPI
import logging
import numpy as np
from PyNucleus_base.myTypes import REAL, INDEX, TAG
from PyNucleus_base import uninitialized
from PyNucleus_base.myTypes import REAL, INDEX
from . levels import meshLevel, algebraicLevel
from . hierarchies import EmptyHierarchy, hierarchy, pCoarsenHierarchy
from PyNucleus_base.utilsFem import TimerManager
from PyNucleus_fem.factories import meshFactory
from PyNucleus_fem.repartitioner import Repartitioner
from PyNucleus_fem.meshOverlaps import meshOverlap, interfaceManager

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -214,13 +212,15 @@ def getLocalOverlap(self):
if self.is_overlapping and self.comm1 is not None:
subdomain = self.hierarchy1.meshLevels[-1].mesh
if self.global_comm.rank in self.OM.overlaps:
print('cells kept local on rank {} in repartitioning: {:,} / target: {:,}'.format(self.global_comm.rank,
self.OM.overlaps[self.global_comm.rank].num_cells/subdomain.num_cells,
self.comm1.size/self.global_comm.size))
print(('cells kept local on rank {} in repartitioning: ' +
'{:,} / target: {:,}').format(self.global_comm.rank,
self.OM.overlaps[self.global_comm.rank].num_cells/subdomain.num_cells,
self.comm1.size/self.global_comm.size))
else:
print('cells kept local on rank {} in repartitioning: {:,} / target: {:,}'.format(self.global_comm.rank,
0.,
self.comm1.size/self.global_comm.size))
print(('cells kept local on rank {} in repartitioning: ' +
'{:,} / target: {:,}').format(self.global_comm.rank,
0.,
self.comm1.size/self.global_comm.size))

def build(self):
if self.hierarchy1 is not None:
Expand Down
4 changes: 1 addition & 3 deletions multilevelSolver/PyNucleus_multilevelSolver/geometricMG.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
from __future__ import division
import logging
import numpy as np
from PyNucleus_base.utilsFem import getLoggingTimer
from PyNucleus_base import REAL, INDEX, uninitialized
from PyNucleus_fem import P1_DoFMap
from PyNucleus_base import INDEX, uninitialized
from PyNucleus_base.linear_operators import LinearOperator

LOGGER = logging.getLogger(__name__)
Expand Down
Loading

0 comments on commit e1ca2c3

Please sign in to comment.