Python Forum
Nested for loop strange problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested for loop strange problem
#1
Thank you for taking some time to read this problem...
I have this nested for loop and I´m note sure its doing what I need.
I need to run a lake temperature model for N = 365 and for each day (i) the first loop calculates the values off b0,c0,d0,an,bn and dn. The second loop has to go trough 40 layers (j)and calculate the coefficients ai, bi, ci, and di. Then the code line:

TempTDMA=TDMAsolver(a[i-1], b[i-1], c[i-1], d[i-1])
returns a list of values after applying the TDMAsolver function for each day(i) with the following input: b0,c0,d0, an,bn and dn, a1...aNlayers-1, b1...bNlayers-1, c1...cNlayers-1 and d1...dNlayers-1

the strange thing is that results seem to be ok, but I can´t remove the following line that´s no used by the model:

CANT_REMOVE_THIS_LINE=fix_profile(n,TempTDMA,Areat0)
I get an error in a function thats note linked with this code line, I can change the name of the variable associated with the fix_profile funtion ("CANT_REMOVE_THIS_LINE2) and the model runs!


this are the neested loops:

N=365
Nlayers=40
for i in xrange(1,N+1):
    I=np.linspace(1,N,N)
    Radiation[i-1] = EquilibriumTemp(air[i-1], RelativeHumidity[i-1], wind[i-1], ClearSkyRadiation[i-1], TempLake[i-1][0])
    Thermocline_Depth[i-1] = ThermoTemp(TempLake[i-1], z, Smin=0.1, seasonal=True, index=False, mixed_cutoff=1)
    K[i-1]=Keddy(TempLake[i-1],z,wind[i-1],Areat0)
    K[i]=Keddy(TempLake[i-1],z,wind[i-1],Areat0)
    
    #surface
    #b0, c0, d0
    b[i-1][0]=1+alfa[0]*s*(0.0+2.0*K[i][0]+K[i][1])
    c[i-1][0]=-alfa[0]*s*(0.0+2.0*K[i][0]+K[i][1])
    d[i-1][0]=((1.0-alfa[0]*s*(0.0+2.0*K[i-1][0]+K[i-1][1]))*TempLake[i-1][0])+(alfa[0]*s*(0.0+2.0*K[i-1][0]+K[i-1][1])*TempLake[i-1][1])+alfa[0]*s*(0.0+K[i-1][0])*((2*Radiation[i-1]*DZ)/(Cp*DH2O*(K[i-1][0]/Areat0[0])))+alfa[0]*s*(0.0+K[i][0])*((2.0*Radiation[i-1]*DZ)/(DH2O*Cp*(K[i][0]/Areat0[0])))
  
    #Bottom
    #an, bn, dn
    a[i-1][-1]=-alfa[-1]*s*(K[i][-2]+2.0*K[i][-1]+0.0)
    b[i-1][-1]=1+alfa[-1]*s*(K[i][-2]+2.0*K[i][-1]+0.0)
    d[i-1][-1]=alfa[-1]*s*TempLake[i-1][-2]*(K[i-1][-2]+2.0*K[i-1][-1]+0.0)+(1-alfa[-1]*s*(K[i-1][-2]+2*K[i-1][-1]+0.0))*TempLake[i-1][-1] # 0,1,2,3,4,5,6,7,8,9 
    
    
    #ai, bi, ci, di
    for j in xrange(1,Nlayers-1):# 40
        a[i-1][j-1]=-alfa[j]*s*(K[i][j-1]+K[i][j]) # 40
        b[i-1][j]=1.0+alfa[j]*s*(K[i][j-1]+2.0*K[i][j]+K[i][j+1])
        c[i-1][j]=-alfa[j]*s*(K[i][j]+K[i][j+1])
        d[i-1][j]=alfa[j]*s*(K[i-1][j]+K[i-1][j-1])*TempLake[i-1][j-1]+alfa[j]*s*(K[i-1][j+1]+K[i-1][j])*TempLake[i-1][j+1]+(1-alfa[j]*s*(K[i-1][j-1]+2.0*K[i-1][j]+K[i-1][j+1]))*TempLake[i-1][j]

        
        TempTDMA=TDMAsolver(a[i-1], b[i-1], c[i-1], d[i-1])
        n=len(TempTDMA)
        CANT_REMOVE_THIS_LINE=fix_profile(n,TempTDMA,Areat0)
    TempLake[i]=TempTDMA
the error ocurs inside the function ThermoTemp:


Error:
Traceback (most recent call last): File "C:\Users\manue\Google Drive\Lake model\PythonLakeModel\ModCastelo do Bode\src\2_Temp_model_version2.py", line 384, in <module> Thermocline_Depth[i-1] = ThermoTemp(TempLake[i-1], z, Smin=0.1, seasonal=True, index=False, mixed_cutoff=1) File "C:\Users\manue\Google Drive\Lake model\PythonLakeModel\ModCastelo do Bode\src\2_Temp_model_version2.py", line 169, in ThermoTemp Sdn = -(depths[thermoInd+1] - depths[thermoInd])/(drho_dz[thermoInd+1] - drho_dz[thermoInd]) IndexError: index 40 is out of bounds for axis 0 with size 40
Reply


Messages In This Thread
Nested for loop strange problem - by mcva - Mar-16-2019, 12:22 PM
RE: Nested for loop strange problem - by Yoriz - Mar-16-2019, 12:51 PM
RE: Nested for loop strange problem - by mcva - Mar-16-2019, 12:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  While Loop Problem Benno2805 1 604 Sep-06-2023, 04:51 PM
Last Post: deanhystad
  Big O runtime nested for loop and append yarinsh 4 1,442 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,677 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  Loop reading csv file problem faustineaiden 1 1,601 Dec-11-2021, 08:40 AM
Last Post: ibreeden
  Problem with nested JSON Kalet 7 2,850 Dec-09-2021, 11:13 PM
Last Post: Gribouillis
  Strange problem related to "python service" Pavel_47 1 1,435 Dec-07-2021, 12:52 PM
Last Post: Pavel_47
  Nested dictionary acting strange Pedroski55 2 2,155 May-13-2021, 10:37 PM
Last Post: Pedroski55
  How do I add another loop to my nested loop greenpine 11 4,667 Jan-12-2021, 04:41 PM
Last Post: greenpine
  Error on nested loop : Invalid syntax dvazquezgu 3 3,302 Nov-25-2020, 10:04 AM
Last Post: palladium
  Infinite loop problem Zirconyl 5 3,055 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020