From 556be01b4af06bc758c601f8594420d9597de9bb Mon Sep 17 00:00:00 2001 From: Chris Hansen Date: Tue, 17 Sep 2024 17:25:12 -0400 Subject: [PATCH] ThinCurr: Check size of matrix in call to `ThinCurr.cross_eval()` - 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 --- src/physics/thin_wall.F90 | 35 ++++++++++--------- .../OpenFUSIONToolkit/ThinCurr/_core.py | 6 ++-- .../OpenFUSIONToolkit/TokaMaker/_core.py | 20 +++++------ 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/physics/thin_wall.F90 b/src/physics/thin_wall.F90 index 1f21b68..c4e1457 100644 --- a/src/physics/thin_wall.F90 +++ b/src/physics/thin_wall.F90 @@ -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() diff --git a/src/python/OpenFUSIONToolkit/ThinCurr/_core.py b/src/python/OpenFUSIONToolkit/ThinCurr/_core.py index 4384ce7..2eb5607 100644 --- a/src/python/OpenFUSIONToolkit/ThinCurr/_core.py +++ b/src/python/OpenFUSIONToolkit/ThinCurr/_core.py @@ -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) @@ -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) @@ -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) diff --git a/src/python/OpenFUSIONToolkit/TokaMaker/_core.py b/src/python/OpenFUSIONToolkit/TokaMaker/_core.py index 36d9099..e174b2e 100644 --- a/src/python/OpenFUSIONToolkit/TokaMaker/_core.py +++ b/src/python/OpenFUSIONToolkit/TokaMaker/_core.py @@ -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) @@ -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): @@ -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) @@ -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)) @@ -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) @@ -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)) @@ -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)