IDEA Cleaning: I won't do that but that is ok.
In this function you define a function in a function that is ok, but in this case not necessary.
see https://gitlab.aip.de/lketzer/platypos/-/blob/master/platypos_package/Planet_class_LoFo14.py#L133
def Solve_for_fenv(self):
""" For known core and planet radius, core mass, age and flux,
solve for envelope mass fraction."""
if self.radius == self.core_radius:
self.fenv = 0.0
else:
def Calculate_fenv(fenv):
age_exponent = {"solarZ": -0.11, "enhZ": -0.18}
return -self.radius + self.core_radius + (2.06\
* (self.core_mass/(1-(fenv/100)))**(-0.21)\
* (fenv/5)**0.59 * (self.flux)**0.044\
* ((self.age/1e3)/5)**(age_exponent[self.metallicity]))
f_guess = 0.1
fenv = optimize.fsolve(Calculate_fenv, x0=f_guess)[0]
self.fenv = fenv
# if fenv1 != fenv:
# print("Sth went wrong in solving for\
# the envelope mass fraction! Check!")
defining a function inside a function is totally fine in python. Even a class in a class...
However, it might be a bit difficult to debug. Because the function exists only in the namespace of the other function.
So even some time it is necessary to define a function into another function (a decorator for instance) here it is not.
And what is not necessary should be avoided.
@staticmethod
def calculate_fenv(fenv):
age_exponent = {"solarZ": -0.11, "enhZ": -0.18}
return -1. * self.radius + self.core_radius \
+ (2.06 * (self.core_mass / (1 - (fenv / 100) ) )**(-0.21) \
* (fenv / 5)**0.59 * (self.flux)**0.044 \
* ( (self.age / 1.e3) / 5)**(age_exponent[self.metallicity]))
def Solve_for_fenv(self):
""" For known core and planet radius, core mass, age and flux,
solve for envelope mass fraction."""
if self.radius == self.core_radius:
self.fenv = 0.0
else:
f_guess = 0.1
fenv = optimize.fsolve(self.calculate_fenv, x0=f_guess)[0]
self.fenv = fenv
# if fenv1 != fenv:
# print("Sth went wrong in solving for\
# the envelope mass fraction! Check!")
The funny part here is that the @staticmethod
decorator (decorator?? read the docs that is one of the most crazy part of python) actually defines calculate_fenv
inside another method. X) but it does properly and debugging won't be an issue.