Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_ISRead(self):
"""Test reading an IS"""
indices = np.array([3,4,5])
anis = PETSc.IS().createGeneral(list(indices))
viewer = PETSc.Viewer().createBinary('test.dat', PETSc.Viewer.Mode.W)
anis.view(viewer)
viewer.destroy()
anis.destroy()
result, = PetscBinaryIO().readBinaryFile('test.dat')
self.assertTrue((indices == result).all())
def _petsc_view(obj, filename):
"""Saves object to disk using a PETSc binary viewer.
"""
viewer = p4pyPETSc.Viewer().createBinary(filename, 'w')
viewer(obj)
viewer2 = p4pyPETSc.Viewer().createASCII(filename+".m", 'w')
viewer2.setFormat(1)
viewer2(obj)
def solve():
opts = PETSc.Options()
mat_filename = opts.getString('-mtx', 'mtx.dat')
rhs_filename = opts.getString('-rhs', 'rhs.dat')
sol0_filename = opts.getString('-sol0', '')
sol_filename = opts.getString('-sol', 'sol.dat')
status_filename = opts.getString('-status', 'status.txt')
view_mtx = PETSc.Viewer().createBinary(mat_filename, mode='r')
view_rhs = PETSc.Viewer().createBinary(rhs_filename, mode='r')
mtx = PETSc.Mat().load(view_mtx)
rhs = PETSc.Vec().load(view_rhs)
ksp = PETSc.KSP().create()
ksp.setOperators(mtx)
ksp.setFromOptions()
if not sol0_filename:
sol = rhs.duplicate()
else:
view_sol0 = PETSc.Viewer().createBinary(sol0_filename, mode='r')
sol = PETSc.Vec().load(view_sol0)
ksp.setInitialGuessNonzero(True)
else:
outDir = './'+_os.path.dirname(filename)
# create output directory if it doesn't exist
try:
_os.mkdir(outDir)
except OSError as exception:
if exception.errno != _errno.EEXIST:
raise
vecArray = vecToArray(vec)
matArray = vecArray.reshape([_np.sqrt(vecArray.size),_np.sqrt(vecArray.size)])
if filetype == 'txt':
#if rank == 0: _np.savetxt(filename,matArray)
txtSave = _PETSc.Viewer().createASCII(filename, 'w',
format=_PETSc.Viewer.Format.ASCII_DENSE, comm=_PETSc.COMM_WORLD)
txtSave(arrayToMat(matArray))
txtSave.destroy()
if rank == 0:
for line in _fl.FileInput(filename,inplace=1):
if line[2] != 't':
line = line.replace(" i","j")
line = line.replace(" -","-")
line = line.replace("+-","-")
print line,
elif filetype == 'bin':
binSave = _PETSc.Viewer().createBinary(filename, 'w', comm=_PETSc.COMM_WORLD)
binSave(arrayToMat(matArray))
binSave.destroy()
#if rank == 0: _np.savetxt(filename,matArray)
txtSave = _PETSc.Viewer().createASCII(filename, 'w',
format=_PETSc.Viewer.Format.ASCII_DENSE, comm=_PETSc.COMM_WORLD)
txtSave(arrayToMat(matArray))
txtSave.destroy()
if rank == 0:
for line in _fl.FileInput(filename,inplace=1):
if line[2] != 't':
line = line.replace(" i","j")
line = line.replace(" -","-")
line = line.replace("+-","-")
print line,
elif filetype == 'bin':
binSave = _PETSc.Viewer().createBinary(filename, 'w', comm=_PETSc.COMM_WORLD)
binSave(arrayToMat(matArray))
binSave.destroy()
vec.comm.barrier()
t0 = 0.0
Tend = 1.0
# instantiate controller
controller = controller_nonMPI(num_procs=1, controller_params=controller_params, description=description)
# get initial values on finest level
P = controller.MS[0].levels[0].prob
uinit = P.u_exact(t0)
# call main function to get things done...
uend, stats = controller.run(u0=uinit, t0=t0, Tend=Tend)
# load reference solution to compare with
fname = cwd + 'data/GS_reference.dat'
viewer = PETSc.Viewer().createBinary(fname, 'r')
uex = P.u_exact(t0)
uex.values = PETSc.Vec().load(viewer)
err = abs(uex - uend)
# filter statistics by variant (number of iterations)
filtered_stats = filter_stats(stats, type='niter')
# convert filtered statistics to list of iterations count, sorted by process
iter_counts = sort_stats(filtered_stats, sortby='time')
# compute and print statistics
niters = np.array([item[1] for item in iter_counts])
out = ' Mean number of iterations: %4.2f' % np.mean(niters)
print(out)
out = ' Range of values for number of iterations: %2i ' % np.ptp(niters)
print(out)
def _petsc_view(obj, filename):
"""Saves petsc object to disk using a PETSc binary viewer.
Parameters
----------
obj : PETSc obj
PETSc4py object to be saved (e.g. vector, matrix, etc)
filename : str
String with PETSc filename
"""
viewer = p4pyPETSc.Viewer().createBinary(filename, 'w')
viewer(obj)
viewer2 = p4pyPETSc.Viewer().createASCII(filename+".m", 'w')
viewer2.pushFormat(1)
viewer2(obj)
viewer2.popFormat()
def _vector_load(directory, filename, mpi_comm):
if _file_exists(directory, filename + ".dat", mpi_comm):
viewer = PETSc.Viewer().createBinary(os.path.join(str(directory), filename + ".dat"), "r", mpi_comm)
return PETSc.Vec().load(viewer)
else:
raise OSError
rank = _PETSc.Comm.Get_rank(_PETSc.COMM_WORLD)
if _os.path.isabs(filename):
outDir = _os.path.dirname(filename)
else:
outDir = './'+_os.path.dirname(filename)
# create output directory if it doesn't exist
try:
_os.mkdir(outDir)
except OSError as exception:
if exception.errno != _errno.EEXIST:
raise
if filetype == 'txt':
txtSave = _PETSc.Viewer().createASCII(filename, 'w',
format=_PETSc.Viewer.Format.ASCII_DENSE, comm=_PETSc.COMM_WORLD)
txtSave(mat)
txtSave.destroy()
if rank == 0:
for line in _fl.FileInput(filename,inplace=1):
if line[2] != 't':
if mattype == 'adj':
line = line.replace(" i","j")
line = line.replace(" -","-")
line = line.replace("+-","-")
line = line.replace("0000e+01+0.00000e+00j","")
line = line.replace(".00000e+00+0.00000e+00j","")
line = line.replace(".","")
line = line.replace(" -","\t-")
line = line.replace(" ","\t")
out = ' Position of max/min number of iterations: %2i -- %2i' % \
(int(np.argmax(niters)), int(np.argmin(niters)))
print(out)
out = ' Std and var for number of iterations: %4.2f -- %4.2f' % (float(np.std(niters)), float(np.var(niters)))
print(out)
print('Iteration count (nonlinear/linear): %i / %i' % (P.snes_itercount, P.ksp_itercount))
print('Mean Iteration count per call: %4.2f / %4.2f' % (P.snes_itercount / max(P.snes_ncalls, 1),
P.ksp_itercount / max(P.ksp_ncalls, 1)))
timing = sort_stats(filter_stats(stats, type='timing_run'), sortby='time')
print('Time to solution: %6.4f sec.' % timing[0][1])
fname = 'data/GS_reference.dat'
viewer = PETSc.Viewer().createBinary(fname, 'w')
viewer.view(uend.values)
assert os.path.isfile(fname), 'ERROR: PETSc did not create file'
return None