Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: Errors & Linting #5124

Merged
merged 9 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import urllib.request

import pybtex.plugin
import sphinx_rtd_theme
import sphinx_rtd_theme # noqa
ax3l marked this conversation as resolved.
Dismissed
Show resolved Hide resolved
from pybtex.style.formatting.unsrt import Style as UnsrtStyle

sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../Regression/Checksum'))
Expand Down
1 change: 0 additions & 1 deletion Docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ Maintenance
:hidden:

maintenance/release
maintenance/performance_tests

Epilogue
--------
Expand Down
135 changes: 0 additions & 135 deletions Docs/source/maintenance/performance_tests.rst

This file was deleted.

2 changes: 1 addition & 1 deletion Docs/source/usage/workflows/ml_materials/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
train_loss_list = []
test_loss_list = []

model.to(device=device);
model.to(device=device)

########## train and test functions ####
# Manual: Train function START
Expand Down
2 changes: 1 addition & 1 deletion Docs/source/usage/workflows/ml_materials/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
act = activation_type
)
model.load_state_dict(model_data['model_state_dict'])
model.to(device=device);
model.to(device=device)

###### load model data ###############
dataset_filename = f'dataset_{species}.pt'
Expand Down
2 changes: 1 addition & 1 deletion Examples/Tests/AcceleratorLattice/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
# The simulation data is in the boosted frame.
# Transform the z position to the lab frame.
time = ds.current_time.value
zz_sim = gamma_boost*zz_sim + uz_boost*time;
zz_sim = gamma_boost*zz_sim + uz_boost*time

# Fetch the quadrupole lattice data
quad_starts = []
Expand Down
2 changes: 1 addition & 1 deletion Examples/Tests/btd_rz/analysis_BTD_laser_antenna.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def fit_function(z, z0_phase):

# The values must be consistent with the values provided in the simulation input
t_current = 80e-15 # Time of the snapshot1
c = 299792458;
c = 299792458
z0_antenna = -1.e-6 # position of laser
lambda0 = 0.8e-6 # wavelength of the signal
tau0 = 10e-15 # duration of the signal
Expand Down
20 changes: 10 additions & 10 deletions Examples/Tests/collision/analysis_collision_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@

# Find the index 'Npmin' that separates macroparticles from group A and group B
Np = len(sorted_wp)
wpmin = sorted_wp.min();
wpmax = sorted_wp.max();
wpmin = sorted_wp.min()
wpmax = sorted_wp.max()
for i in range(len(sorted_wp)):
if sorted_wp[i] > wpmin:
Npmin = i
break

NpA = Npmin
wpA = wpmin;
wpA = wpmin
NpB = Np - Npmin
wpB = wpmax;
wpB = wpmax
NpAs = 0
NpAe = Npmin
NpBs = Npmin
NpBe = Np

#############

sorted_px_sum = np.abs(sorted_px).sum();
sorted_py_sum = np.abs(sorted_py).sum();
sorted_pz_sum = np.abs(sorted_pz).sum();
sorted_wp_sum = np.abs(sorted_wp).sum();
sorted_px_sum = np.abs(sorted_px).sum()
sorted_py_sum = np.abs(sorted_py).sum()
sorted_pz_sum = np.abs(sorted_pz).sum()
sorted_wp_sum = np.abs(sorted_wp).sum()

# compute mean velocities
wAtot = wpA*NpA
Expand Down Expand Up @@ -118,8 +118,8 @@
TApar_30ps_soln = 6.15e3 # TA parallel solution at t = 30 ps
error = np.abs(TApar-TApar_30ps_soln)/TApar_30ps_soln
tolerance = 0.02
print('TApar at 30ps error = ', error);
print('tolerance = ', tolerance);
print('TApar at 30ps error = ', error)
print('tolerance = ', tolerance)
assert error < tolerance

test_name = os.path.split(os.getcwd())[1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@
# r(0) = r_0, r'(0) = 0, and a = q_e*q_tot/(4*pi*eps_0*m_e)
#
# The E was calculated at the end of the last time step
v_exact = lambda r: np.sqrt((q_e*q_tot)/(2*pi*m_e*eps_0)*(1/r_0-1/r))
t_exact = lambda r: np.sqrt(r_0**3*2*pi*m_e*eps_0/(q_e*q_tot)) \
* (np.sqrt(r/r_0-1)*np.sqrt(r/r_0) \
+ np.log(np.sqrt(r/r_0-1)+np.sqrt(r/r_0)))
func = lambda rho: t_exact(rho) - t_max #Objective function to find r(t_max)
def v_exact(r):
return np.sqrt(q_e * q_tot / (2 * pi * m_e * eps_0) * (1 / r_0 - 1 / r))
def t_exact(r):
return np.sqrt(r_0 ** 3 * 2 * pi * m_e * eps_0 / (q_e * q_tot)) * (np.sqrt(r / r_0 - 1) * np.sqrt(r / r_0) + np.log(np.sqrt(r / r_0 - 1) + np.sqrt(r / r_0)))
def func(rho):
return t_exact(rho) - t_max #Objective function to find r(t_max)
r_end = fsolve(func,r_0)[0] #Numerically solve for r(t_max)
E_exact = lambda r: np.sign(r)*(q_tot/(4*pi*eps_0*r**2)*(abs(r)>=r_end) \
+ q_tot*abs(r)/(4*pi*eps_0*r_end**3)*(abs(r)<r_end))
def E_exact(r):
return np.sign(r) * (q_tot / (4 * pi * eps_0 * r ** 2) * (abs(r) >= r_end) + q_tot * abs(r) / (4 * pi * eps_0 * r_end ** 3) * (abs(r) < r_end))

# Load data pertaining to fields
data = ds.covering_grid(level=0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def launch_analysis(executable):
def main() :

from lasy.laser import Laser
from lasy.profiles import CombinedLongitudinalTransverseProfile, GaussianProfile
from lasy.profiles import CombinedLongitudinalTransverseProfile
from lasy.profiles.longitudinal import GaussianLongitudinalProfile
from lasy.profiles.transverse import LaguerreGaussianTransverseProfile

Expand Down
2 changes: 1 addition & 1 deletion Examples/Tests/nci_fdtd_stability/analysis_ncicorr.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import checksumAPI

fn = sys.argv[1]
use_MR = re.search( 'nci_correctorMR', fn ) != None
use_MR = re.search( 'nci_correctorMR', fn ) is not None

if use_MR:
energy_corrector_off = 5.e32
Expand Down
48 changes: 25 additions & 23 deletions Examples/Tests/nuclear_fusion/analysis_two_product_fusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,30 +243,32 @@ def cross_section( E_keV ):
## Returns cross section in b, using the analytical fits given
## in H.-S. Bosch and G.M. Hale 1992 Nucl. Fusion 32 611
joule_to_keV = 1.e-3/scc.e
B_G = scc.pi * scc.alpha * np.sqrt( 2.*m_reduced * scc.c**2 * joule_to_keV );
B_G = scc.pi * scc.alpha * np.sqrt( 2.*m_reduced * scc.c**2 * joule_to_keV )
if reaction_type == 'DT':
A1 = 6.927e4;
A2 = 7.454e8;
A3 = 2.050e6;
A4 = 5.2002e4;
A5 = 0;
B1 = 6.38e1;
B2 = -9.95e-1;
B3 = 6.981e-5;
B4 = 1.728e-4;
A1 = 6.927e4
A2 = 7.454e8
A3 = 2.050e6
A4 = 5.2002e4
A5 = 0
B1 = 6.38e1
B2 = -9.95e-1
B3 = 6.981e-5
B4 = 1.728e-4
elif reaction_type == 'DD':
A1 = 5.3701e4;
A2 = 3.3027e2;
A3 = -1.2706e-1;
A4 = 2.9327e-5;
A5 = -2.5151e-9;
B1 = 0;
B2 = 0;
B3 = 0;
B4 = 0;

astrophysical_factor = (A1 + E_keV*(A2 + E_keV*(A3 + E_keV*(A4 + E_keV*A5)))) / (1 + E_keV*(B1 + E_keV*(B2 + E_keV*(B3 + E_keV*B4))));
millibarn_to_barn = 1.e-3;
A1 = 5.3701e4
A2 = 3.3027e2
A3 = -1.2706e-1
A4 = 2.9327e-5
A5 = -2.5151e-9
B1 = 0
B2 = 0
B3 = 0
B4 = 0
else:
raise RuntimeError(f"Reaction type '{reaction_type}' not implemented.")

astrophysical_factor = (A1 + E_keV*(A2 + E_keV*(A3 + E_keV*(A4 + E_keV*A5)))) / (1 + E_keV*(B1 + E_keV*(B2 + E_keV*(B3 + E_keV*B4))))
millibarn_to_barn = 1.e-3
return millibarn_to_barn * astrophysical_factor/E_keV * np.exp(-B_G/np.sqrt(E_keV))

def E_com_to_p_sq_com(m1, m2, E):
Expand Down Expand Up @@ -405,7 +407,7 @@ def main():
ds_start = yt.load(filename_start)
ad_end = ds_end.all_data()
ad_start = ds_start.all_data()
dt = float(ds_end.current_time - ds_start.current_time)
dt = float(ds_end.current_time - ds_start.current_time) # noqa

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable dt is not used.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dpgrote wrote in #5123 (comment)

The eval below needs dt. The eval is kind of hacky anyway. Something like this would be better

if i == 1:
    specific_check1(data, dt)
elif i == 2:
    specific_check2(data, dt)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NeilZaim @RemiLehe can you quickly see how to remove the eval with a more explicit or handling in this function?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simple fix would be to do

eval("specific_check"+str(i))(data, dt)

But it would be better to remove the eval altogether.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that looks very similar to me 😅

field_data_end = ds_end.covering_grid(level=0, left_edge=ds_end.domain_left_edge,
dims=ds_end.domain_dimensions)
field_data_start = ds_start.covering_grid(level=0, left_edge=ds_start.domain_left_edge,
Expand Down
2 changes: 1 addition & 1 deletion Examples/Tests/ohm_solver_EM_modes/PICMI_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(self, test, dim, B_dir, verbose):

# dump all the current attributes to a dill pickle file
if comm.rank == 0:
with open(f'sim_parameters.dpkl', 'wb') as f:
with open('sim_parameters.dpkl', 'wb') as f:
dill.dump(self, f)

# print out plasma parameters
Expand Down
2 changes: 1 addition & 1 deletion Examples/Tests/ohm_solver_EM_modes/PICMI_inputs_rz.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __init__(self, test, verbose):

# dump all the current attributes to a dill pickle file
if comm.rank == 0:
with open(f'sim_parameters.dpkl', 'wb') as f:
with open('sim_parameters.dpkl', 'wb') as f:
dill.dump(self, f)

# print out plasma parameters
Expand Down
2 changes: 1 addition & 1 deletion Examples/Tests/ohm_solver_EM_modes/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
matplotlib.rcParams.update({'font.size': 20})

# load simulation parameters
with open(f'sim_parameters.dpkl', 'rb') as f:
with open('sim_parameters.dpkl', 'rb') as f:
sim = dill.load(f)

if sim.B_dir == 'z':
Expand Down
6 changes: 3 additions & 3 deletions Examples/Tests/ohm_solver_EM_modes/analysis_rz.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
constants = picmi.constants

# load simulation parameters
with open(f'sim_parameters.dpkl', 'rb') as f:
with open('sim_parameters.dpkl', 'rb') as f:
sim = dill.load(f)

diag_dir = "diags/field_diags"
Expand Down Expand Up @@ -124,8 +124,8 @@ def process(it):
omega_fast = sim.vA * np.sqrt(R2 + np.sqrt(R2**2 - P4))
omega_slow = sim.vA * np.sqrt(R2 - np.sqrt(R2**2 - P4))
# Upper right corner
ax.plot(k*sim.l_i, omega_fast/sim.w_ci, 'w--', label = f"$\omega_{{fast}}$")
ax.plot(k*sim.l_i, omega_slow/sim.w_ci, color='white', linestyle='--', label = f"$\omega_{{slow}}$")
ax.plot(k*sim.l_i, omega_fast/sim.w_ci, 'w--', label = "$\omega_{fast}$")
ax.plot(k*sim.l_i, omega_slow/sim.w_ci, color='white', linestyle='--', label = "$\omega_{slow}$")
# Thermal resonance
thermal_res = sim.w_ci + 3*sim.v_ti*k
ax.plot(k*sim.l_i, thermal_res/sim.w_ci, color='magenta', linestyle='--', label = "$\omega = \Omega_i + 3v_{th,i}k$")
Expand Down
2 changes: 1 addition & 1 deletion Examples/Tests/ohm_solver_ion_Landau_damping/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
matplotlib.rcParams.update({'font.size': 20})

# load simulation parameters
with open(f'sim_parameters.dpkl', 'rb') as f:
with open('sim_parameters.dpkl', 'rb') as f:
sim = dill.load(f)

# theoretical damping rates were taken from Fig. 14b of Munoz et al.
Expand Down
Loading
Loading