use f-strings in place of .format()

This commit is contained in:
Jan Petykiewicz 2024-07-17 23:15:34 -07:00
parent 639f88bba8
commit 95e3f71b40
8 changed files with 17 additions and 16 deletions

View File

@ -157,7 +157,8 @@ def main():
e[1][tuple(grid.shape//2)] += field_source(t)
update_H(e, h)
print('iteration {}: average {} iterations per sec'.format(t, (t+1)/(time.perf_counter()-start)))
avg_rate = (t + 1)/(time.perf_counter() - start))
print(f'iteration {t}: average {avg_rate} iterations per sec')
sys.stdout.flush()
if t % 20 == 0:

View File

@ -781,7 +781,7 @@ def linmin(x_guess, f0, df0, x_max, f_tol=0.1, df_tol=min(tolerance, 1e-6), x_to
x_min, x_max, isave, dsave)
for i in range(int(1e6)):
if task != 'F':
logging.info('search converged in {} iterations'.format(i))
logging.info(f'search converged in {i} iterations')
break
fx = f(x, dfx)
x, fx, dfx, task = minpack2.dsrch(x, fx, dfx, f_tol, df_tol, x_tol, task,

View File

@ -43,7 +43,8 @@ def _scipy_qmr(
nonlocal ii
ii += 1
if ii % 100 == 0:
logger.info('Solver residual at iteration {} : {}'.format(ii, norm(A @ xk - b)))
cur_norm = norm(A @ xk - b)
logger.info(f'Solver residual at iteration {ii} : {cur_norm}')
if 'callback' in kwargs:
def augmented_callback(xk: ArrayLike) -> None:

View File

@ -420,7 +420,7 @@ def _normalized_fields(
Sz_a = E[0] * numpy.conj(H[1] * phase) * dxes_real[0][1] * dxes_real[1][0]
Sz_b = E[1] * numpy.conj(H[0] * phase) * dxes_real[0][0] * dxes_real[1][1]
Sz_tavg = numpy.real(Sz_a.sum() - Sz_b.sum()) * 0.5 # 0.5 since E, H are assumed to be peak (not RMS) amplitudes
assert Sz_tavg > 0, 'Found a mode propagating in the wrong direction! Sz_tavg={}'.format(Sz_tavg)
assert Sz_tavg > 0, f'Found a mode propagating in the wrong direction! {Sz_tavg=}'
energy = epsilon * e.conj() * e

View File

@ -29,9 +29,9 @@ def shift_circ(
Sparse matrix for performing the circular shift.
"""
if len(shape) not in (2, 3):
raise Exception('Invalid shape: {}'.format(shape))
raise Exception(f'Invalid shape: {shape}')
if axis not in range(len(shape)):
raise Exception('Invalid direction: {}, shape is {}'.format(axis, shape))
raise Exception(f'Invalid direction: {axis}, shape is {shape}')
shifts = [abs(shift_distance) if a == axis else 0 for a in range(3)]
shifted_diags = [(numpy.arange(n) + s) % n for n, s in zip(shape, shifts)]
@ -69,12 +69,11 @@ def shift_with_mirror(
Sparse matrix for performing the shift-with-mirror.
"""
if len(shape) not in (2, 3):
raise Exception('Invalid shape: {}'.format(shape))
raise Exception(f'Invalid shape: {shape}')
if axis not in range(len(shape)):
raise Exception('Invalid direction: {}, shape is {}'.format(axis, shape))
raise Exception(f'Invalid direction: {axis}, shape is {shape}')
if shift_distance >= shape[axis]:
raise Exception('Shift ({}) is too large for axis {} of size {}'.format(
shift_distance, axis, shape[axis]))
raise Exception(f'Shift ({shift_distance}) is too large for axis {axis} of size {shape[axis]}')
def mirrored_range(n: int, s: int) -> NDArray[numpy.int_]:
v = numpy.arange(n) + s
@ -198,7 +197,7 @@ def avg_forward(axis: int, shape: Sequence[int]) -> sparse.spmatrix:
Sparse matrix for forward average operation.
"""
if len(shape) not in (2, 3):
raise Exception('Invalid shape: {}'.format(shape))
raise Exception(f'Invalid shape: {shape}')
n = numpy.prod(shape)
return 0.5 * (sparse.eye(n) + shift_circ(axis, shape))

View File

@ -15,7 +15,7 @@ def conducting_boundary(
) -> tuple[fdfield_updater_t, fdfield_updater_t]:
dirs = [0, 1, 2]
if direction not in dirs:
raise Exception('Invalid direction: {}'.format(direction))
raise Exception(f'Invalid direction: {direction}')
dirs.remove(direction)
u, v = dirs
@ -64,4 +64,4 @@ def conducting_boundary(
return ep, hp
raise Exception('Bad polarity: {}'.format(polarity))
raise Exception(f'Bad polarity: {polarity}')

View File

@ -33,10 +33,10 @@ def cpml_params(
) -> dict[str, Any]:
if axis not in range(3):
raise Exception('Invalid axis: {}'.format(axis))
raise Exception(f'Invalid axis: {axis}')
if polarity not in (-1, 1):
raise Exception('Invalid polarity: {}'.format(polarity))
raise Exception(f'Invalid polarity: {polarity}')
if thickness <= 2:
raise Exception('It would be wise to have a pml with 4+ cells of thickness')

View File

@ -101,7 +101,7 @@ def test_poynting_divergence(sim: 'TDResult') -> None:
def test_poynting_planes(sim: 'TDResult') -> None:
mask = (sim.js[0] != 0).any(axis=0)
if mask.sum() > 1:
pytest.skip('test_poynting_planes can only test single point sources, got {}'.format(mask.sum()))
pytest.skip(f'test_poynting_planes can only test single point sources, got {mask.sum()}')
args: dict[str, Any] = {
'dxes': sim.dxes,