0

Here's my code:

# Libs
import numpy as np

# Isostatic model:

def airy(t,tref=35.,rhoM=3.2,rhoC=2.7,mode=None,rate=None,hlimit=None):
  # Equilibrium ratios:
  er = rhoC / (rhoM-rhoC)
  di = rhoC/rhoM
  # Static buoyancy equation:
  excess = t-tref
  hi = excess * (1-di)
  ri = excess - hi  
  # Mode results:
  if mode == 'Initial':
    print("Model parameters: Crustal density =",rhoC,"Mantle density =",rhoM)
    print("Mountain height (Km) =",np.round(hi, 3))
    print("Root thickness (Km) =",np.round(ri, 3))
    print("Ratio of height to root =",er)
    return ri, hi
  elif mode=='Erosive':
    # Initialise loop
    counter = 0
    ht = hi
    while ht >= hlimit:
      counter = counter+1
      excess = t-tref
      ht = excess * (1-di)
      rt = excess - ht
      ht = ht*np.exp(rate*counter)
      t = ht+rt+tref
      print(rt, ht, counter)
  elif mode==None:
    return ri, hi

tref = 35.  
it = tref*1.5
print("Initial thickness =",it)
ir, ih = airy(it, mode='Initial')
rt, ht, tstep = airy(it, mode='Erosive', rate=-0.025,hlimit=0.5)

It's taken me bl*ody ages to get even to this stage with the loop and its still not what I'm after!

I would like to get a list or array of values for each iteration of the loop for the variables rt and ht. At the moment this works and prints the correct values for the loop, but also results in a NoneTypeError. If I add a return rt, ht, counter then the result is a single value, not a list/array.

So I've seen this answer, but ideally want this inside one function and I can't see where I'm supposed to use a list in the loop.

Any help would be appreciated! Thanks

2 Answers 2

2

You forgot to return the three-valued tuple:

def airy(t,tref=35.,rhoM=3.2,rhoC=2.7,mode=None,rate=None,hlimit=None):
  # Equilibrium ratios:
  er = rhoC / (rhoM-rhoC)
  di = rhoC/rhoM
  # Static buoyancy equation:
  excess = t-tref
  hi = excess * (1-di)
  ri = excess - hi  
  # Mode results:
  if mode == 'Initial':
    print("Model parameters: Crustal density =",rhoC,"Mantle density =",rhoM)
    print("Mountain height (Km) =",np.round(hi, 3))
    print("Root thickness (Km) =",np.round(ri, 3))
    print("Ratio of height to root =",er)
    return ri, hi
  elif mode=='Erosive':
    # Initialise loop
    counter = 0
    ht = hi
    l_ht = []
    l_rt_ = []
    l_counter = []
    while ht >= hlimit:
      counter = counter+1
      excess = t-tref
      ht = excess * (1-di)
      rt = excess - ht
      ht = ht*np.exp(rate*counter)
      t = ht+rt+tref
      print(rt, ht, counter)
      l_rt.append(rt)
      l_ht.append(ht)
      l_counter .append(counter)
    return l_rt, l_ht, l_counter # HERE
  elif mode==None:
    return ri, hi

Would return None otherwise, and fail with the unpacking.

3
  • Hey I just noticed that pasting the code block - that does indeed get rid of the nonetypeerror, but now the results are a single value, when really I want the value of each iteration step (the counter variable)
    – 8556732
    Nov 16, 2018 at 13:31
  • 1
    Indeed. I just added a list to store the intermediate results and return them. Nov 16, 2018 at 13:33
  • Brilliant - now I've seen how this can be implemented I can see a ton of uses for it. Thanks
    – 8556732
    Nov 16, 2018 at 13:38
1
    elif mode=='Erosive':
        # Initialise loop
        counter = 0
        ht = hi
        while ht >= hlimit:
          counter = counter+1
          excess = t-tref
          ht = excess * (1-di)
          rt = excess - ht
          ht = ht*np.exp(rate*counter)
          t = ht+rt+tref
          print(rt, ht, counter)
#-------> where is a return statement?
2
  • Hey - thanks for the reply, Matthieu also noticed this. Cheers!
    – 8556732
    Nov 16, 2018 at 13:38
  • 1
    Yeah, I have not refreshed the site ;D
    – artona
    Nov 16, 2018 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.