Skip to content

Commit 360cf79

Browse files
committed
Fixed bug in changeZeff
1 parent 833296e commit 360cf79

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

src/mitim_tools/gacode_tools/PROFILEStools.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,21 +1682,32 @@ def lumpDT(self):
16821682

16831683
self.moveSpecie(pos=len(self.Species), pos_new=1)
16841684

1685-
def changeZeff(self, Zeff, ion_pos=2, keep_fmain=False, quasineutral_ions=None, enforceSameGradients=False):
1686-
"""
1687-
if (D,Z1,Z2), pos 1 -> change Z1
1688-
"""
1685+
def changeZeff(
1686+
self,
1687+
Zeff,
1688+
ion_pos = 2, # Position of ion to change (if (D,Z1,Z2), pos 1 -> change Z1)
1689+
keep_fmain = False, # If True, it will keep fmain and change Z of ion in position ion_pos. If False, it will change the content of ion in position ion_pos and the content of quasineutral ions to achieve Zeff
1690+
fmain_force = None, # If keep_fmain is True, it will force fmain to this value. If None, it will use the current fmain
1691+
enforceSameGradients = False # If True, it will scale all thermal densities to have the same gradients after changing Zeff
1692+
):
16891693

1690-
if quasineutral_ions is None:
1691-
if self.DTplasmaBool:
1692-
quasineutral_ions = [self.Dion, self.Tion]
1693-
else:
1694-
quasineutral_ions = [self.Mion]
1694+
if not keep_fmain and fmain_force is not None:
1695+
raise ValueError("[MITIM] fmain_force can only be used if keep_fmain is True")
1696+
1697+
if fmain_force is not None:
1698+
fmain_factor = fmain_force / self.derived["fmain"]
1699+
else:
1700+
fmain_factor = 1.0
1701+
1702+
if self.DTplasmaBool:
1703+
quasineutral_ions = [self.Dion, self.Tion]
1704+
else:
1705+
quasineutral_ions = [self.Mion]
16951706

16961707
if not keep_fmain:
16971708
print(f'\t\t- Changing Zeff (from {self.derived["Zeff_vol"]:.3f} to {Zeff=:.3f}) by changing content of ion in position {ion_pos} {self.Species[ion_pos]["N"],self.Species[ion_pos]["Z"]}, quasineutralized by ions {quasineutral_ions}',typeMsg="i")
16981709
else:
1699-
print(f'\t\t- Changing Zeff (from {self.derived["Zeff_vol"]:.3f} to {Zeff=:.3f}) by changing content and Z of ion in position {ion_pos} {self.Species[ion_pos]["N"],self.Species[ion_pos]["Z"]}, quasineutralized by ions {quasineutral_ions} and keeping fmain={self.derived["fmain"]:.3f}',typeMsg="i")
1710+
print(f'\t\t- Changing Zeff (from {self.derived["Zeff_vol"]:.3f} to {Zeff=:.3f}) by changing content and Z of ion in position {ion_pos} {self.Species[ion_pos]["N"],self.Species[ion_pos]["Z"]}, quasineutralized by ions {quasineutral_ions} and keeping fmain={self.derived["fmain"]*fmain_factor:.3f}',typeMsg="i")
17001711

17011712
# Plasma needs to be in quasineutrality to start with
17021713
self.enforceQuasineutrality()
@@ -1711,15 +1722,19 @@ def changeZeff(self, Zeff, ion_pos=2, keep_fmain=False, quasineutral_ions=None,
17111722
fZj = np.zeros(self.derived["fi"].shape[0])
17121723
fZj2 = np.zeros(self.derived["fi"].shape[0])
17131724
for i in range(len(self.Species)):
1725+
1726+
# Ions for quasineutrality (main ones)
17141727
if i in quasineutral_ions:
17151728
Zq += self.Species[i]["Z"]
17161729
Zq2 += self.Species[i]["Z"] ** 2
17171730

1718-
fZq += self.Species[i]["Z"] * self.derived["fi"][:, i]
1719-
fZq2 += self.Species[i]["Z"] ** 2 * self.derived["fi"][:, i]
1731+
fZq += self.Species[i]["Z"] * self.derived["fi"][:, i] * fmain_factor
1732+
fZq2 += self.Species[i]["Z"] ** 2 * self.derived["fi"][:, i] * fmain_factor
1733+
# Non-quasineutral and not the ion to change
17201734
elif i != ion_pos:
17211735
fZj += self.Species[i]["Z"] * self.derived["fi"][:, i]
17221736
fZj2 += self.Species[i]["Z"] ** 2 * self.derived["fi"][:, i]
1737+
# Ion to change
17231738
else:
17241739
Zk = self.Species[i]["Z"]
17251740

@@ -1749,15 +1764,23 @@ def changeZeff(self, Zeff, ion_pos=2, keep_fmain=False, quasineutral_ions=None,
17491764
# Find free parameters (fk and Zk)
17501765
# ------------------------------------------------------
17511766

1752-
Zk = (Zeff - fZq2 + fZj2) / (1 - fZq - fZj)
1753-
fk = (1 - fZq - fZj) / Zk
1767+
Zk = (Zeff - fZq2 - fZj2) / (1 - fZq - fZj)
17541768

1769+
# I need a single value
1770+
Zk_ave = CALCtools.integrateFS(Zk, self.profiles["rmin(m)"], self.derived["volp_miller"])[-1] / self.derived["volume"]
1771+
1772+
fk = (1 - fZq - fZj) / Zk_ave
1773+
17551774
# ------------------------------------------------------
17561775
# Insert
17571776
# ------------------------------------------------------
17581777

1759-
self.profiles['z'][ion_pos] = CALCtools.integrateFS(Zk, self.profiles["rmin(m)"], self.derived["volp_miller"])[-1] / self.derived["volume"]
1778+
self.profiles['z'][ion_pos] = Zk_ave
17601779
self.profiles["ni(10^19/m^3)"][:, ion_pos] = fk * self.profiles["ne(10^19/m^3)"]
1780+
1781+
if fmain_force is not None:
1782+
for i in quasineutral_ions:
1783+
self.profiles["ni(10^19/m^3)"][:, i] *= fmain_factor
17611784

17621785
self.readSpecies()
17631786

0 commit comments

Comments
 (0)