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

Make linting more rigorous. #359

Merged
merged 20 commits into from
Nov 1, 2023
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
11 changes: 2 additions & 9 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

- name: Install python dependencies
run: |
pip install flake8
- name: Run ruff
uses: chartboost/ruff-action@v1

- name: Run linting tests
run: |
export EXCLUDE="E226,E265,E266,E4,E5,E7,W,F"
flake8 --ignore $EXCLUDE src/
flake8 --ignore $EXCLUDE tests/
flake8 --ignore $EXCLUDE examples/
1 change: 0 additions & 1 deletion examples/1_Simple/just_a_quadratic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3

import logging
from simsopt.objectives.functions import Identity
from simsopt.objectives import LeastSquaresProblem
from simsopt.solve import least_squares_serial_solve
Expand Down
1 change: 0 additions & 1 deletion examples/1_Simple/permanent_magnet_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from matplotlib import pyplot as plt
from simsopt.field import DipoleField, ToroidalField
from simsopt.geo import PermanentMagnetGrid, SurfaceRZFourier
from simsopt.objectives import SquaredFlux
from simsopt.solve import GPMO
from simsopt.util.permanent_magnet_helper_functions import *

Expand Down
2 changes: 1 addition & 1 deletion examples/1_Simple/stage_two_optimization_minimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def fun(dofs):
""")
res = minimize(fun, dofs, jac=True, method='L-BFGS-B',
options={'maxiter': MAXITER, 'maxcor': 300, 'iprint': 5}, tol=1e-15)
curves_to_vtk(curves, OUT_DIR + f"curves_opt", close=True)
curves_to_vtk(curves, OUT_DIR + "curves_opt", close=True)
pointData = {"B_N": np.sum(bs.B().reshape((nphi, ntheta, 3)) * s.unitnormal(), axis=2)[:, :, None]}
s.to_vtk(OUT_DIR + "surf_opt", extra_data=pointData)

Expand Down
1 change: 0 additions & 1 deletion examples/1_Simple/tracing_fieldlines_QA.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import time
import os
import logging
import sys
from pathlib import Path
import numpy as np

Expand Down
2 changes: 0 additions & 2 deletions examples/2_Intermediate/QSC.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env python3

import logging
import numpy as np
from qsc import Qsc

from simsopt._core import Optimizable
Expand Down
6 changes: 3 additions & 3 deletions examples/2_Intermediate/boozerQA.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from simsopt.configs import get_ncsx_data
from simsopt.field import BiotSavart, coils_via_symmetries
from simsopt.geo import SurfaceXYZTensorFourier, BoozerSurface, curves_to_vtk, boozer_surface_residual, \
ToroidalFlux, Volume, MajorRadius, CurveLength, CurveCurveDistance, NonQuasiSymmetricRatio, Iotas
Volume, MajorRadius, CurveLength, NonQuasiSymmetricRatio, Iotas
from simsopt.objectives import QuadraticPenalty
from simsopt.util import in_github_actions

Expand Down Expand Up @@ -83,7 +83,7 @@
# sum the objectives together
JF = J_nonQSRatio + J_iotas + J_major_radius + Jls

curves_to_vtk(curves, OUT_DIR + f"curves_init")
curves_to_vtk(curves, OUT_DIR + "curves_init")
boozer_surface.surface.to_vtk(OUT_DIR + "surf_init")

# let's fix the coil current
Expand Down Expand Up @@ -144,7 +144,7 @@ def fun(dofs):
MAXITER = 50 if in_github_actions else 1e3

res = minimize(fun, dofs, jac=True, method='BFGS', options={'maxiter': MAXITER}, tol=1e-15)
curves_to_vtk(curves, OUT_DIR + f"curves_opt")
curves_to_vtk(curves, OUT_DIR + "curves_opt")
boozer_surface.surface.to_vtk(OUT_DIR + "surf_opt")

print("End of 2_Intermediate/boozerQA.py")
Expand Down
3 changes: 0 additions & 3 deletions examples/2_Intermediate/permanent_magnet_MUSE.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
https://github.com/akaptano/simsopt_permanent_magnet_advanced_scripts.git
"""

import os
import pickle
import time
from pathlib import Path

Expand Down Expand Up @@ -257,7 +255,6 @@
# surface is at least 64 x 64 resolution.
vmec_flag = False
if vmec_flag:
from mpi4py import MPI
from simsopt.mhd.vmec import Vmec
from simsopt.util.mpi import MpiPartition
mpi = MpiPartition(ngroups=1)
Expand Down
1 change: 0 additions & 1 deletion examples/2_Intermediate/permanent_magnet_PM4Stell.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
Ken Hammond and the PM4Stell + MAGPIE teams.
'''

import os
from pathlib import Path
import time

Expand Down
3 changes: 0 additions & 3 deletions examples/2_Intermediate/permanent_magnet_QA.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@
are available to OpenMP, e.g. through setting OMP_NUM_THREADS).
"""

import os
import time
from pathlib import Path

import numpy as np
from matplotlib import pyplot as plt

from simsopt.field import BiotSavart, DipoleField
from simsopt.geo import PermanentMagnetGrid, SurfaceRZFourier
Expand Down Expand Up @@ -242,7 +240,6 @@
# surface is at least 64 x 64 resolution.
vmec_flag = False
if vmec_flag:
from mpi4py import MPI
from simsopt.mhd.vmec import Vmec
from simsopt.util.mpi import MpiPartition
mpi = MpiPartition(ngroups=1)
Expand Down
4 changes: 2 additions & 2 deletions examples/2_Intermediate/stage_two_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def fun(dofs):
################################################################################
""")
res = minimize(fun, dofs, jac=True, method='L-BFGS-B', options={'maxiter': MAXITER, 'maxcor': 300}, tol=1e-15)
curves_to_vtk(curves, OUT_DIR + f"curves_opt_short")
curves_to_vtk(curves, OUT_DIR + "curves_opt_short")
pointData = {"B_N": np.sum(bs.B().reshape((nphi, ntheta, 3)) * s.unitnormal(), axis=2)[:, :, None]}
s.to_vtk(OUT_DIR + "surf_opt_short", extra_data=pointData)

Expand All @@ -177,7 +177,7 @@ def fun(dofs):
dofs = res.x
LENGTH_WEIGHT *= 0.1
res = minimize(fun, dofs, jac=True, method='L-BFGS-B', options={'maxiter': MAXITER, 'maxcor': 300}, tol=1e-15)
curves_to_vtk(curves, OUT_DIR + f"curves_opt_long")
curves_to_vtk(curves, OUT_DIR + "curves_opt_long")
pointData = {"B_N": np.sum(bs.B().reshape((nphi, ntheta, 3)) * s.unitnormal(), axis=2)[:, :, None]}
s.to_vtk(OUT_DIR + "surf_opt_long", extra_data=pointData)

Expand Down
3 changes: 0 additions & 3 deletions examples/2_Intermediate/stage_two_optimization_stochastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,7 @@ def fun(dofs):
JF.x = dofs
J = JF.J()
grad = JF.dJ()
cl_string = ", ".join([f"{J.J():.3f}" for J in Jls])
mean_AbsB = np.mean(bs.AbsB())
jf = Jmpi.J()
BdotN = np.mean(np.abs(np.sum(bs.B().reshape((nphi, ntheta, 3)) * s.unitnormal(), axis=2)))
outstr = f"J={J:.1e}, ⟨Jf⟩={jf:.1e}"
cl_string = ", ".join([f"{J.J():.1f}" for J in Jls])
kap_string = ", ".join(f"{np.max(c.kappa()):.1f}" for c in base_curves)
Expand Down
7 changes: 3 additions & 4 deletions examples/3_Advanced/single_stage_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"""
import os
import numpy as np
from mpi4py import MPI
from pathlib import Path
from scipy.optimize import minimize
from simsopt.util import MpiPartition, proc0_print, comm_world
Expand Down Expand Up @@ -57,7 +56,7 @@
ARCLENGTH_WEIGHT = 1e-9 # Weight for the arclength variation penalty in the objective function
##########################################################################################
##########################################################################################
directory = f'optimization_QH'
directory = 'optimization_QH'
vmec_verbose = False
# Create output directories
this_path = os.path.join(parent_path, directory)
Expand Down Expand Up @@ -109,7 +108,7 @@
J_LENGTH_PENALTY = LENGTH_CON_WEIGHT * sum([QuadraticPenalty(Jls[i], LENGTH_THRESHOLD) for i in range(len(base_curves))])
JF = Jf + J_CC + J_LENGTH + J_LENGTH_PENALTY + J_CURVATURE + J_MSC
##########################################################################################
proc0_print(f' Starting optimization')
proc0_print(' Starting optimization')
##########################################################################################
# Initial stage 2 optimization
##########################################################################################
Expand Down Expand Up @@ -229,7 +228,7 @@ def fun(dofs, prob_jacobian=None, info={'Nfeval': 0}):
pointData = {"B_N": BdotN_surf[:, :, None]}
surf.to_vtk(os.path.join(coils_results_path, "surf_opt"), extra_data=pointData)
bs.save(os.path.join(coils_results_path, "biot_savart_opt.json"))
vmec.write_input(os.path.join(this_path, f'input.final'))
vmec.write_input(os.path.join(this_path, 'input.final'))
proc0_print(f"Aspect ratio after optimization: {vmec.aspect()}")
proc0_print(f"Mean iota after optimization: {vmec.mean_iota()}")
proc0_print(f"Quasisymmetry objective after optimization: {qs.total()}")
Expand Down
9 changes: 4 additions & 5 deletions examples/3_Advanced/single_stage_optimization_finite_beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"""
import os
import numpy as np
from mpi4py import MPI
from math import isnan
from pathlib import Path
from scipy.optimize import minimize
Expand Down Expand Up @@ -62,7 +61,7 @@
ARCLENGTH_WEIGHT = 1e-9 # Weight for the arclength variation penalty in the objective function
##########################################################################################
##########################################################################################
directory = f'optimization_QH_finitebeta'
directory = 'optimization_QH_finitebeta'
vmec_verbose = False
# Create output directories
this_path = os.path.join(parent_path, directory)
Expand Down Expand Up @@ -122,7 +121,7 @@
JF = Jf + J_CC + J_LENGTH + J_LENGTH_PENALTY + J_CURVATURE + J_MSC

##########################################################################################
proc0_print(f' Starting optimization')
proc0_print(' Starting optimization')
##########################################################################################
# Initial stage 2 optimization
##########################################################################################
Expand Down Expand Up @@ -169,7 +168,7 @@ def fun_J(prob, coils_prob):
try:
vc = VirtualCasing.from_vmec(vmec, src_nphi=vc_src_nphi, trgt_nphi=nphi_VMEC, trgt_ntheta=ntheta_VMEC, filename=None)
Jf.target = vc.B_external_normal
except ObjectiveFailure as e:
except ObjectiveFailure:
pass

bs.set_points(surf.gamma().reshape((-1, 3)))
Expand Down Expand Up @@ -265,7 +264,7 @@ def fun(dofss, prob_jacobian, info={'Nfeval': 0}):
pointData = {"B_N": BdotN_surf[:, :, None]}
surf.to_vtk(os.path.join(coils_results_path, "surf_opt"), extra_data=pointData)
bs.save(os.path.join(coils_results_path, "biot_savart_opt.json"))
vmec.write_input(os.path.join(this_path, f'input.final'))
vmec.write_input(os.path.join(this_path, 'input.final'))
proc0_print(f"Aspect ratio after optimization: {vmec.aspect()}")
proc0_print(f"Mean iota after optimization: {vmec.mean_iota()}")
proc0_print(f"Quasisymmetry objective after optimization: {qs.total()}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3

import os
from mpi4py import MPI
import numpy as np

from simsopt.mhd import Vmec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3

import numpy as np
from mpi4py import MPI

from simsopt.objectives import LeastSquaresProblem
from simsopt.mhd import Vmec
Expand Down
15 changes: 15 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,18 @@ requires = ["setuptools>=45", "wheel", "oldest-supported-numpy", "cmake", "ninja
build-backend = "setuptools.build_meta"

[tools.setuptools_scm]

[tool.ruff]
src = ["src/simsopt"]
ignore = ["E226","E265","E266","E4","E501","E701","E702","E712","E722","E731","E741","F403","F405","F406","W5"]
extend-exclude = ["thirdparty"]
[tool.ruff.per-file-ignores]
"__init__.py" = ["E402","F401"]
"src/simsopt/field/magneticfieldclasses.py" = ["E743"]
"src/simsopt/mhd/bootstrap.py" = ["F841"]
"src/simsopt/mhd/vmec_diagnostics.py" = ["F841"]
"tests/verify_MPI_not_initialized.py" = ["F401"]
"tests/geo/test_plot.py" = ["F401"]
"tests/geo/test_curve.py" = ["F401"]
"tests/geo/test_surface.py" = ["F401"]
"tests/mhd/test_virtual_casing.py" = ["F841"]
3 changes: 2 additions & 1 deletion run_autopep
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# https://pycodestyle.pycqa.org/en/latest/intro.html#error-codes
set -ex

EXCLUDE="E226,E265,E266,E4,E5,E7,W,F"
# EXCLUDE="E226,E265,E266,E4,E5,E7,W,F"
EXCLUDE="E226,E265,E266,E4,E501,E722,F403,F405,F406,W5"
flake8 --ignore $EXCLUDE src/ || (exit 0)
flake8 --ignore $EXCLUDE tests/ || (exit 0)
flake8 --ignore $EXCLUDE examples/ || (exit 0)
Expand Down
7 changes: 7 additions & 0 deletions run_ruff
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
# https://pycodestyle.pycqa.org/en/latest/intro.html#error-codes
set -ex

ruff check . || (exit 0)
ruff check --fix .
ruff check .
1 change: 0 additions & 1 deletion src/simsopt/_core/optimizable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,6 @@ def upper_bounds(self) -> RealArray:
Upper bounds of the free DOFs associated with the current
Optimizable object and those of its ancestors
"""
opts = self.ancestors + [self]
return np.concatenate([opt._dofs.free_upper_bounds for opt in self.unique_dof_lineage])

@upper_bounds.setter
Expand Down
2 changes: 1 addition & 1 deletion src/simsopt/field/coil.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from simsopt._core.optimizable import Optimizable
from simsopt._core.derivative import Derivative
from simsopt.geo.curvexyzfourier import CurveXYZFourier
from simsopt.geo.curve import RotatedCurve, Curve
from simsopt.geo.curve import RotatedCurve
import simsoptpp as sopp


Expand Down
3 changes: 0 additions & 3 deletions src/simsopt/field/magneticfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@ def to_mgrid(self, filename, nr=10, nphi=4, nz=12, rmin=1.0, rmax=2.0, zmin=-0.5
zs = np.linspace(zmin, zmax, nz, endpoint=True)

Phi, Z, R = np.meshgrid(phis, zs, rs, indexing='ij')
X = R * np.cos(Phi)
Y = R * np.sin(Phi)
Z = Z

RPhiZ = np.zeros((R.size, 3))
RPhiZ[:, 0] = R.flatten()
Expand Down
14 changes: 5 additions & 9 deletions src/simsopt/field/magneticfieldclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def _B_impl(self, B):

def _dB_by_dX_impl(self, dB):
points = self.get_points_cart_ref()
phi = np.arctan2(points[:, 1], points[:, 0])
R = np.sqrt(np.square(points[:, 0]) + np.square(points[:, 1]))

x = points[:, 0]
Expand All @@ -66,8 +65,6 @@ def _dB_by_dX_impl(self, dB):

def _d2B_by_dXdX_impl(self, ddB):
points = self.get_points_cart_ref()
x = points[:, 0]
y = points[:, 1]
ddB[:] = 2*self.B0*self.R0*np.multiply(
1/(points[:, 0]**2+points[:, 1]**2)**3, np.array([
[[3*points[:, 0]**2+points[:, 1]**3, points[:, 0]**3-3*points[:, 0]*points[:, 1]**2, np.zeros((len(points)))], [
Expand Down Expand Up @@ -397,7 +394,6 @@ def _B_impl(self, B):
k = np.sqrt(1-np.divide(np.square(alpha), np.square(beta)))
ellipek2 = ellipe(k**2)
ellipkk2 = ellipk(k**2)
gamma = np.square(points[:, 0]) - np.square(points[:, 1])
B[:] = np.dot(self.rotMatrix, np.array(
[self.Inorm*points[:, 0]*points[:, 2]/(2*alpha**2*beta*rho**2+1e-31)*((self.r0**2+r**2)*ellipek2-alpha**2*ellipkk2),
self.Inorm*points[:, 1]*points[:, 2]/(2*alpha**2*beta*rho**2+1e-31)*((self.r0**2+r**2)*ellipek2-alpha**2*ellipkk2),
Expand Down Expand Up @@ -666,7 +662,6 @@ def _toVTK(self, vtkname):
mx = np.ascontiguousarray(self.m_vec[:, 0])
my = np.ascontiguousarray(self.m_vec[:, 1])
mz = np.ascontiguousarray(self.m_vec[:, 2])
mmag = np.sqrt(mx ** 2 + my ** 2 + mz ** 2)
mx_normalized = np.ascontiguousarray(mx / self.m_maxima)
my_normalized = np.ascontiguousarray(my / self.m_maxima)
mz_normalized = np.ascontiguousarray(mz / self.m_maxima)
Expand All @@ -680,11 +675,12 @@ def _toVTK(self, vtkname):
mtheta_normalized = np.ascontiguousarray(mtheta / self.m_maxima)

# Save all the data to a vtk file which can be visualized nicely with ParaView
data = {"m": (mx, my, mz), "m_normalized": (mx_normalized, my_normalized, mz_normalized), "m_rphiz": (mr, mphi, mz), "m_rphiz_normalized": (mr_normalized, mphi_normalized, mz_normalized), "m_rphitheta": (mrminor, mphi, mtheta), "m_rphitheta_normalized": (mrminor_normalized, mphi_normalized, mtheta_normalized)}
data = {"m": (mx, my, mz), "m_normalized": (mx_normalized, my_normalized, mz_normalized),
"m_rphiz": (mr, mphi, mz), "m_rphiz_normalized": (mr_normalized, mphi_normalized, mz_normalized),
"m_rphitheta": (mrminor, mphi, mtheta),
"m_rphitheta_normalized": (mrminor_normalized, mphi_normalized, mtheta_normalized)}
from pyevtk.hl import pointsToVTK
pointsToVTK(
str(vtkname), ox, oy, oz, data=data
)
pointsToVTK(str(vtkname), ox, oy, oz, data=data)


class Dommaschk(MagneticField):
Expand Down
1 change: 0 additions & 1 deletion src/simsopt/field/mgrid.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import numpy as np
from scipy.io import netcdf_file

Expand Down
7 changes: 3 additions & 4 deletions src/simsopt/field/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,6 @@ def compute_resonances(res_tys, res_phi_hits, ma=None, delta=1e-2):
theta0 = res_tys[ip][0, 2]
zeta0 = res_tys[ip][0, 3]
theta0_mod = theta0 % (2*np.pi)
zeta0_mod = zeta0 % (2*np.pi)
x0 = s0 * np.cos(theta0)
y0 = s0 * np.sin(theta0)
else:
Expand Down Expand Up @@ -550,10 +549,10 @@ def compute_resonances(res_tys, res_phi_hits, ma=None, delta=1e-2):
logger.debug(f'(R,Z)r = {np.sqrt(res_tys[ip][indexr,1]**2 + res_tys[ip][indexr,2]**2),res_tys[ip][indexr,3]}')

mpol = np.amax([mpoll, mpolm, mpolr])
index_mpol = np.argmax([mpoll, mpolm, mpolr])
# index_mpol = np.argmax([mpoll, mpolm, mpolr])
ntor = np.amax([ntorl, ntorm, ntorr])
index_ntor = np.argmax([ntorl, ntorm, ntorr])
index = np.amax([index_mpol, index_ntor])
# index_ntor = np.argmax([ntorl, ntorm, ntorr])
# index = np.amax([index_mpol, index_ntor])
resonances.append(np.asarray([R0, Z0, phi0, vpar0, t, mpol, ntor]))
return resonances

Expand Down
Loading
Loading