Skip to content

Commit

Permalink
ThinCurr: Check size of matrix in call to ThinCurr.cross_eval()
Browse files Browse the repository at this point in the history
 - TokaMaker: Fix typo to enable memory reordering of input if needed in `TokaMaker.set_coil_currents()`
 - Replace `ValueError` with `IndexError` on size checks throughout python interface
  • Loading branch information
hansec committed Sep 17, 2024
1 parent d3b5787 commit 556be01
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
35 changes: 18 additions & 17 deletions src/physics/thin_wall.F90
Original file line number Diff line number Diff line change
Expand Up @@ -1304,23 +1304,24 @@ SUBROUTINE tw_compute_Lmat_MF(row_obj,col_obj,nrhs,a,b)
END DO
END DO
!$omp end do nowait
!---Add passive coils to model
!$omp do
DO i=1,row_obj%np_active+row_obj%nholes
DO j=1,col_obj%n_vcoils
jj=col_obj%np_active+col_obj%nholes+j
b(jj,:) = row_obj%Ael2coil(i,j)*a(i,:)
END DO
END DO
!$omp end do nowait
!$omp do
DO i=1,row_obj%n_vcoils
ii=row_obj%np_active+row_obj%nholes+i
DO j=1,col_obj%n_vcoils
jj=col_obj%np_active+col_obj%nholes+j
b(jj,:) = b(jj,:) + row_obj%Acoil2coil(i,j)*a(ii,:)
END DO
END DO
IF((col_obj%n_vcoils>0).OR.(row_obj%n_vcoils>0))CALL oft_warn("V-coil contributions were not computed.")
! !---Add passive coils to model
! !$omp do
! DO i=1,col_obj%n_vcoils
! ii=col_obj%np_active+col_obj%nholes+i
! DO j=1,row_obj%np_active+row_obj%nholes
! b(ii,:) = b(ii,:) + row_obj%Ael2coil(j,i)*a(j,:)
! END DO
! END DO
! !$omp end do nowait
! !$omp do
! DO i=1,col_obj%n_vcoils
! ii=col_obj%np_active+col_obj%nholes+i
! DO j=1,row_obj%n_vcoils
! jj=row_obj%np_active+row_obj%nholes+j
! b(ii,:) = b(ii,:) + row_obj%Acoil2coil(j,i)*a(jj,:)
! END DO
! END DO
!$omp end parallel
b = b/(4.d0*pi)
CALL quad%delete()
Expand Down
6 changes: 4 additions & 2 deletions src/python/OpenFUSIONToolkit/ThinCurr/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def save_current(self,potential,tag):
@param tag Name of field in plot files
'''
if potential.shape[0] != self.nelems:
raise ValueError('Incorrect shape of "potential", should be [nelems]')
raise IndexError('Incorrect shape of "potential", should be [nelems]')
potential = numpy.ascontiguousarray(potential, dtype=numpy.float64)
cstring = c_char_p(tag.encode())
thincurr_save_field(self.tw_obj,potential,cstring)
Expand All @@ -231,7 +231,7 @@ def save_scalar(self,field,tag):
@param tag Name of field in plot files
'''
if field.shape[0] != self.np:
raise ValueError('Incorrect shape of "field", should be [np]')
raise IndexError('Incorrect shape of "field", should be [np]')
field = numpy.ascontiguousarray(field, dtype=numpy.float64)
ctag = c_char_p(tag.encode())
thincurr_save_scalar(self.tw_obj,field,ctag)
Expand Down Expand Up @@ -385,6 +385,8 @@ def cross_eval(self,model2,field):
@result Flux on `model2` from `field` on `self` `(field.shape[0],:)`
'''
nrhs = field.shape[0]
if field.shape[1] != self.nelems:
raise IndexError('Incorrect shape of "field", should be [nelems]')
vec_out = numpy.zeros((nrhs,model2.nelems), dtype=numpy.float64)
vec_in = numpy.ascontiguousarray(field.copy(), dtype=numpy.float64)
error_string = c_char_p(b""*200)
Expand Down
20 changes: 10 additions & 10 deletions src/python/OpenFUSIONToolkit/TokaMaker/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,16 +422,16 @@ def set_coil_reg(self,reg_mat,reg_targets=None,reg_weights=None):
@param reg_weights Weights for regularization terms [nregularize] (default: 1)
'''
if reg_mat.shape[1] != self.ncoils+1:
raise ValueError('Incorrect shape of "reg_mat", should be [nregularize,ncoils+1]')
raise IndexError('Incorrect shape of "reg_mat", should be [nregularize,ncoils+1]')
nregularize = reg_mat.shape[0]
if reg_targets is None:
reg_targets = numpy.zeros((nregularize,), dtype=numpy.float64)
if reg_weights is None:
reg_weights = numpy.ones((nregularize,), dtype=numpy.float64)
if reg_targets.shape[0] != nregularize:
raise ValueError('Incorrect shape of "reg_targets", should be [nregularize]')
raise IndexError('Incorrect shape of "reg_targets", should be [nregularize]')
if reg_weights.shape[0] != nregularize:
raise ValueError('Incorrect shape of "reg_weights", should be [nregularize]')
raise IndexError('Incorrect shape of "reg_weights", should be [nregularize]')
reg_mat = numpy.ascontiguousarray(reg_mat.transpose(), dtype=numpy.float64)
reg_targets = numpy.ascontiguousarray(reg_targets, dtype=numpy.float64)
reg_weights = numpy.ascontiguousarray(reg_weights, dtype=numpy.float64)
Expand All @@ -446,8 +446,8 @@ def set_coil_bounds(self,coil_bounds):
@param coil_bounds Minimum and maximum allowable coil currents [ncoils+1,2]
'''
if (coil_bounds.shape[0] != self.ncoils+1) or (coil_bounds.shape[1] != 2):
raise ValueError('Incorrect shape of "coil_bounds", should be [ncoils+1,2]')
boucoil_boundsnds = numpy.ascontiguousarray(coil_bounds, dtype=numpy.float64)
raise IndexError('Incorrect shape of "coil_bounds", should be [ncoils+1,2]')
coil_bounds = numpy.ascontiguousarray(coil_bounds, dtype=numpy.float64)
tokamaker_set_coil_bounds(coil_bounds)

def set_coil_vsc(self,coil_gains):
Expand All @@ -456,7 +456,7 @@ def set_coil_vsc(self,coil_gains):
@param coil_gains Gains for each coil (absolute scale is arbitrary)
'''
if coil_gains.shape[0] != self.ncoils:
raise ValueError('Incorrect shape of "coil_gains", should be [ncoils]')
raise IndexError('Incorrect shape of "coil_gains", should be [ncoils]')
coil_gains = numpy.ascontiguousarray(coil_gains, dtype=numpy.float64)
tokamaker_set_coil_vsc(coil_gains)

Expand Down Expand Up @@ -552,7 +552,7 @@ def vac_solve(self,psi=None):
psi = numpy.zeros((self.np,),dtype=numpy.float64)
else:
if psi.shape[0] != self.np:
raise ValueError('Incorrect shape of "psi", should be [np]')
raise IndexError('Incorrect shape of "psi", should be [np]')
psi = numpy.ascontiguousarray(psi, dtype=numpy.float64)
error_flag = c_int()
tokamaker_vac_solve(psi,ctypes.byref(error_flag))
Expand Down Expand Up @@ -778,7 +778,7 @@ def set_psi(self,psi):
@param psi Poloidal flux values (should not be normalized!)
'''
if psi.shape[0] != self.np:
raise ValueError('Incorrect shape of "psi", should be [np]')
raise IndexError('Incorrect shape of "psi", should be [np]')
psi = numpy.ascontiguousarray(psi, dtype=numpy.float64)
tokamaker_set_psi(psi)

Expand All @@ -789,7 +789,7 @@ def set_psi_dt(self,psi0,dt):
@param dt Time since reference poloidal flux
'''
if psi0.shape[0] != self.np:
raise ValueError('Incorrect shape of "psi0", should be [np]')
raise IndexError('Incorrect shape of "psi0", should be [np]')
psi0 = numpy.ascontiguousarray(psi0, dtype=numpy.float64)
tokamaker_set_psi_dt(psi0,c_double(dt))

Expand Down Expand Up @@ -990,7 +990,7 @@ def set_coil_currents(self, currents):
@param currents Current in each coil [A]
'''
if currents.shape[0] != self.ncoils:
raise ValueError('Incorrect shape of "currents", should be [ncoils]')
raise IndexError('Incorrect shape of "currents", should be [ncoils]')
currents = numpy.ascontiguousarray(currents, dtype=numpy.float64)
tokamaker_set_coil_currents(currents)

Expand Down

0 comments on commit 556be01

Please sign in to comment.