Skip to content

Commit 96390f2

Browse files
committed
Use tensordot to calculate boundary conditions in NKGSsolver.freeboundary
Using tensordot reduces the copying of data which increases computational and memory efficiency. Original runtime of freeboundary: 37.74% (22.36%) New runtime of freeboundary: 16.76% (0.27%) (tensordot takes 4.73%)
1 parent ca47ed9 commit 96390f2

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

freegsnke/GSstaticsolver.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@
1515
it under the terms of the GNU Lesser General Public License as published by
1616
the Free Software Foundation, either version 3 of the License, or
1717
(at your option) any later version.
18-
18+
1919
You should have received a copy of the GNU Lesser General Public License
20-
along with FreeGSNKE. If not, see <http://www.gnu.org/licenses/>.
20+
along with FreeGSNKE. If not, see <http://www.gnu.org/licenses/>.
2121
"""
2222

23-
import warnings
2423
from copy import deepcopy
2524

2625
import freegs4e
27-
import matplotlib.pyplot as plt
2826
import numpy as np
29-
import scipy as sp
3027
from freegs4e.gradshafranov import Greens
3128

3229
from . import nk_solver_H as nk_solver
@@ -168,7 +165,10 @@ def freeboundary(self, plasma_psi, tokamak_psi, profiles):
168165

169166
# calculates and imposes the boundary conditions
170167
self.psi_boundary = np.zeros_like(self.R)
171-
psi_bnd = np.sum(self.greenfunc * self.jtor[np.newaxis, :, :], axis=(-1, -2))
168+
# weighted sum over the last two axes.
169+
# "contract" axis 1 of greenfunc with axis 0 of jtor
170+
# contract axis 2 of greenfunc with axis 1 of jtor
171+
psi_bnd = np.tensordot(self.greenfunc, self.jtor, axes=([1, 2], [0, 1]))
172172

173173
self.psi_boundary[:, 0] = psi_bnd[: self.nx]
174174
self.psi_boundary[:, -1] = psi_bnd[self.nx : 2 * self.nx]
@@ -418,7 +418,6 @@ def forward_solve(
418418
while (rel_change > target_relative_tolerance) * (
419419
iterations < max_solving_iterations
420420
):
421-
422421
if rel_change > Picard_handover:
423422
log.append("Picard iteration: " + str(iterations))
424423
# using Picard instead of NK
@@ -762,10 +761,9 @@ def optimize_currents(
762761
# print(delta_current)
763762

764763
for i in range(constrain.n_control_coils):
765-
766764
if verbose:
767765
print(
768-
f" - calculating derivatives for coil {i+1}/{constrain.n_control_coils}"
766+
f" - calculating derivatives for coil {i + 1}/{constrain.n_control_coils}"
769767
)
770768

771769
currents = np.copy(self.dummy_current)
@@ -963,12 +961,10 @@ def inverse_solve(
963961
(rel_change_full > target_relative_tolerance)
964962
+ (previous_rel_delta_psit > target_relative_psit_update)
965963
) * (iterations < max_solving_iterations):
966-
967964
if verbose:
968965
print("Iteration: " + str(iterations))
969966

970967
if check_equilibrium:
971-
972968
# this_max_rel_psit = min(max_rel_psit, np.mean(self.rel_psit_updates[-6:]))
973969
this_max_rel_psit = np.mean(self.rel_psit_updates[-6:])
974970
this_max_rel_update_size = 1.0 * max_rel_update_size

0 commit comments

Comments
 (0)