Re: [Tutor] Do loop in Python
Yes. Actually, I have changed it to this kine od script: # == model loop == #Optione1 if True: z=zeros( (numlayers,) ) thickness= (thickness*1.0) for l in layers: z = arange ((-thickness - h * l),0,dz) ##z= t -h * l nu = num+ (0.001*exp(-0.005*(z+200.))*dz) #Option2 if False: thickness = range(-200 , 0, 10) # a list from -200 to 0 with step 10 (0, 10, 20, ..., 190, 200) layers = range(1,11) # a list from 1 to 10 for t in thickness: for l in layers: z = arange(( t + h * l ), 0, dz ) #zvalues = arange(-200.,0,dz) nu = num+ (0.001*exp(-0.005*(z+200.))) plot(nu,z) Then it seems it works. it should have a trend to reducing values... - Show quoted text - On Tue, Nov 29, 2011 at 2:00 PM, Steven D'Aprano st...@pearwood.info wrote: stm atoc wrote: Thank you so much for your reply. It was very helpful information and I used it in order to improve the program Here is the new version of the program: zvalues = [-200] # starting value hvalues = [10] # starting value increments = [1, 1, 1, 1, 1, 1, 1, 1] for N in increments: h = hvalues[-1] - N hvalues.append(h) z = zvalues[-1] + h zvalues.append(z) height = arange((z)*dz,0,dz) for z,when in enumerate(height): nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) I'm afraid I still don't know what the arange function is. Is that a function you have written yourself? However, I can see that it doesn't actually get used! You create an arange object, and call it height. height = arange((z)*dz,0,dz) You should insert a print statement after this line to see what value height is given, and check that it is what you expect it to be. Presumably height is some sort of list or sequence of values, because you next use it in a for-loop: for z,when in enumerate(height): ... So now we know that z takes on the values 0, 1, 2, 3, ... and when takes on the values from height, whatever they are. But in the rest of your code, you don't use when at all: nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) nu.append(num + nuh[z]) No when, hence the values from height aren't actually used. Strange. Also, what are dz and num? You use them both, but I can't see where they are defined or what value they have. Likewise nuh and nu, although I can guess they are probably lists because you append to them. Because I don't know what values to use, and I don't know what arange is, I can't run your code to see what it does. So I'm reduced to guessing. If I take a wild stab in the dark that dz is a small number, say, 0.01, I can see what values nuh gets: py from math import exp py dz = 0.01 py nuh = [] py for z in range(10): ... nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) ... py from pprint import pprint py pprint(nuh) [3.6787944117144236e-06, 3.6604463480401533e-06, 3.6421897957152333e-06, 3.624024298324903e-06, 3.6059494017307832e-06, 3.587964654059516e-06, 3.5700696056914737e-06, 3.5522638092495153e-06, 3.5345468195878014e-06, 3.5169181937806692e-06] Is that the sort of behaviour you expect for nuh? Since the nuh values are changing, num+nuh[z] should also be changing, which implies nu should be changing. Unless num is so large that rounding error wipes out the nuh values. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do loop in Python
Thank you so much. This script and all information was totally helpful and actually helped me for the next step of my work as well. Have a great time. Sue On Fri, Nov 25, 2011 at 10:44 PM, Andreas Perstinger andreas.perstin...@gmx.net wrote: On 2011-11-25 14:46, stm atoc wrote: Here is the new version of the program: zvalues = [-200] # starting value hvalues = [10] # starting value increments = [1, 1, 1, 1, 1, 1, 1, 1] for N in increments: h = hvalues[-1] - N hvalues.append(h) z = zvalues[-1] + h zvalues.append(z) height = arange((z)*dz,0,dz) There is no arange in python. Could it be that you use numpy and import it with from numpy import *? for z,when in enumerate(height): I'm pretty sure this line doesn't do what you expect it to do. You have a sequence (a numpy array) named height and after calling enumerate you get a list of tuples in the form of [(0, height[0]), (1, height[1]), ...]. Now the for-loop iterates over this list and assigns z to the first value of the tuple (the index-values) and when to the second (the values from height). You later never use when but just use z. If you really want that, the enumerate is completly unnecessary and you could just use for z in range(len(height)). But I'm not sure if numpy arrays work with len(). nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) The story is like this: I should define layers and thickness and see how the diffusion profile changes over the z. height (or depth) of the total thickness or 'z'. I basically, define 'z' in 10 layers and each layer is called ' N' . Difference between each layer is 'h', which is equal 10 micrometer. Now, what I like to do is the modification of nu based on each zvalue In fact, for each 'zvalue' o'z' step, I need to calculate a different value for 'nu' based on the available equation in the program. BUT, I am not sure, exactly, how to add the new do loop of z inside another loop of nu. For me your explanations are still too confusing. Could it be that you are thinking way too complicated? My guess is you want to have a range of material thicknesses (from 1 to 200 micrometers in 10 micrometer-steps) and then you want from each thickness 10 different layers, right? import math # you should always tell us which modules you import num = 0.05 # some constant nu = [] # list of resulting values h = 10.0 # height of one layer thickness = range(0, 210, 10) # a list from 0 to 200 with step 10 (0, 10, 20, ..., 190, 200) layers = range(1,11) # a list from 1 to 10 for t in thickness: for l in layers: z = t + h * l # I'm not sure if you want to add or subtract the layer thickness nu = num + (0.01 * math.exp(-0.05 * (z + 200.0))) This will result in a big one-dimensional list where you calculate for each thickness the nu-value for 10 layers. Am I close? I'm still not sure about the steps and the height of the layers. I also wonder if it wouldn't be better to use a two-dimensional list. I have done this way as well (the other way around): height = arange((z)*dz,0,dz) for z,when in enumerate(height): for N in increments: h = hvalues[-1] - N hvalues.append(h) z = zvalues[-1] + h zvalues.append(z) nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) but still no sign of 'nu changes' over 'z'! As Charles has already mentioned, the values for nu are very similar (they start beginning to differ just at the seventh digit after the comma). How do you further process this values? If you plot them what's your scale? Bye, Andreas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do loop in Python
stm atoc wrote: Thank you so much for your reply. It was very helpful information and I used it in order to improve the program Here is the new version of the program: zvalues = [-200] # starting value hvalues = [10] # starting value increments = [1, 1, 1, 1, 1, 1, 1, 1] for N in increments: h = hvalues[-1] - N hvalues.append(h) z = zvalues[-1] + h zvalues.append(z) height = arange((z)*dz,0,dz) for z,when in enumerate(height): nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) I'm afraid I still don't know what the arange function is. Is that a function you have written yourself? However, I can see that it doesn't actually get used! You create an arange object, and call it height. height = arange((z)*dz,0,dz) You should insert a print statement after this line to see what value height is given, and check that it is what you expect it to be. Presumably height is some sort of list or sequence of values, because you next use it in a for-loop: for z,when in enumerate(height): ... So now we know that z takes on the values 0, 1, 2, 3, ... and when takes on the values from height, whatever they are. But in the rest of your code, you don't use when at all: nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) nu.append(num + nuh[z]) No when, hence the values from height aren't actually used. Strange. Also, what are dz and num? You use them both, but I can't see where they are defined or what value they have. Likewise nuh and nu, although I can guess they are probably lists because you append to them. Because I don't know what values to use, and I don't know what arange is, I can't run your code to see what it does. So I'm reduced to guessing. If I take a wild stab in the dark that dz is a small number, say, 0.01, I can see what values nuh gets: py from math import exp py dz = 0.01 py nuh = [] py for z in range(10): ... nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) ... py from pprint import pprint py pprint(nuh) [3.6787944117144236e-06, 3.6604463480401533e-06, 3.6421897957152333e-06, 3.624024298324903e-06, 3.6059494017307832e-06, 3.587964654059516e-06, 3.5700696056914737e-06, 3.5522638092495153e-06, 3.5345468195878014e-06, 3.5169181937806692e-06] Is that the sort of behaviour you expect for nuh? Since the nuh values are changing, num+nuh[z] should also be changing, which implies nu should be changing. Unless num is so large that rounding error wipes out the nuh values. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Do loop in Python
Hi there, I am a new python user. I have a question regarding do loop. This is a simple program that I have written: - N=10 h=10.0 # [micrometer] z=-200.0 # [micrometer] num = 0.05 #m**2/s dz = 1.0 nuh=[] tmax=3600 dt=20. nu=[]height = arange(z*dz,0,dz) outfile=open('nu.dat','w') outfile.write('height, nu, nuh') for z,when in enumerate(height): for h in range(10): for N in range(10): for z in range((N-z)+(N-h)): nuh.append(0.01 * exp(-0.05*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) --- What I like to do with this program is do loop like the fortran version of as follows: do i = 2, N z(i) = z(i-1) +h(i-1) end do write(0,*) 'z ', z(1:N) write(0,*) 'when ', 'nu ','Conc ' do i= 1, N nuh(i)= 0.01d0*exp(-0.005d2*(z(i)+200)) ! turbulence diffusivity m**2/s nu(i)= num(1) + nuh(i) end do -- My problem is I am notable have the curve in the output plot as I have as a result of FORTRAN program. What happens is just having a straight line! the whole problem is in z part, which is supposed to be changed and i do not see it! So, would it be possible to take a look at it please. any suggestion would greatly appreciated. Thank you, Sue ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do loop in Python
Sue, I'm not familiar with FORTRAN, and since I'm not completely sure what you're trying to accomplish please take this simply as an 'educated guess'. My guess is that you're not getting the curve because Z is only defined to one decimal location (1/10th) precision and probably needs higher precision (and is likely getting it) in the FORTRAN version. http://docs.python.org/tutorial/floatingpoint.html this should help Charles Sent from my iPhone On Nov 25, 2011, at 2:16 AM, stm atoc stm.at...@googlemail.com wrote: Hi there, I am a new python user. I have a question regarding do loop. This is a simple program that I have written: - N=10 h=10.0 # [micrometer] z=-200.0 # [micrometer] num = 0.05 #m**2/s dz = 1.0 nuh=[] tmax=3600 dt=20. nu=[]height = arange(z*dz,0,dz) outfile=open('nu.dat','w') outfile.write('height, nu, nuh') for z,when in enumerate(height): for h in range(10): for N in range(10): for z in range((N-z)+(N-h)): nuh.append(0.01 * exp(-0.05*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) --- What I like to do with this program is do loop like the fortran version of as follows: do i = 2, N z(i) = z(i-1) +h(i-1) end do write(0,*) 'z ', z(1:N) write(0,*) 'when ', 'nu ','Conc ' do i= 1, N nuh(i)= 0.01d0*exp(-0.005d2*(z(i)+200)) ! turbulence diffusivity m**2/s nu(i)= num(1) + nuh(i) end do -- My problem is I am notable have the curve in the output plot as I have as a result of FORTRAN program. What happens is just having a straight line! the whole problem is in z part, which is supposed to be changed and i do not see it! So, would it be possible to take a look at it please. any suggestion would greatly appreciated. Thank you, Sue ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do loop in Python
regarding to the last email: what I am trying to do is seeing the variation of 'nu' over (changes of) 'z'. My concern is how to arrange this! Basically, I am not able to define the variation of nu by z ( 1 to 200). I am looking for a statement to show the changes of 'nu' for each step of z (as height). On the other hand, for each step, 'h' is supposed to be subtracted from 'z' (like: 200-10, 190-10...) as well, at least 10 times (which was trying to be defined as N)! I hope this is somehow clear Thanks in advance, Sue - Show quoted text - On Fri, Nov 25, 2011 at 10:16 AM, stm atoc stm.at...@googlemail.com wrote: Hi there, I am a new python user. I have a question regarding do loop. This is a simple program that I have written: - N=10 h=10.0 # [micrometer] z=-200.0 # [micrometer] num = 0.05 #m**2/s dz = 1.0 nuh=[] tmax=3600 dt=20. nu=[]height = arange(z*dz,0,dz) outfile=open('nu.dat','w') outfile.write('height, nu, nuh') for z,when in enumerate(height): for h in range(10): for N in range(10): for z in range((N-z)+(N-h)): nuh.append(0.01 * exp(-0.05*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) --- What I like to do with this program is do loop like the fortran version of as follows: do i = 2, N z(i) = z(i-1) +h(i-1) end do write(0,*) 'z ', z(1:N) write(0,*) 'when ', 'nu ','Conc ' do i= 1, N nuh(i)= 0.01d0*exp(-0.005d2*(z(i)+200)) ! turbulence diffusivity m**2/s nu(i)= num(1) + nuh(i) end do -- My problem is I am notable have the curve in the output plot as I have as a result of FORTRAN program. What happens is just having a straight line! the whole problem is in z part, which is supposed to be changed and i do not see it! So, would it be possible to take a look at it please. any suggestion would greatly appreciated. Thank you, Sue ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do loop in Python
stm atoc wrote: Hi there, I am a new python user. I have a question regarding do loop. This is a simple program that I have written: - N=10 h=10.0 # [micrometer] z=-200.0 # [micrometer] You define N, h and z here, but later on you use them as loop variables. So these three values never get used: they are thrown away, and replaced by the values of the loops: h - 0, 1, 2, ... 9 N - 0, 1, 2, ... 9 z is especially troublesome, because it gets used for TWO loop variables, one inside the other. The inner z loop depends on the outer z loop, which makes it tricky to predict what values z will take. num = 0.05 #m**2/s dz = 1.0 nuh=[] tmax=3600 dt=20. nu=[]height = arange(z*dz,0,dz) What is arange? In physics, height is a scalar. But later on, you seen to use height as if it were a collection of values. outfile=open('nu.dat','w') outfile.write('height, nu, nuh') for z,when in enumerate(height): for h in range(10): for N in range(10): for z in range((N-z)+(N-h)): nuh.append(0.01 * exp(-0.05*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) --- What I like to do with this program is do loop like the fortran version of as follows: do i = 2, N z(i) = z(i-1) +h(i-1) end do How is z initialised? What is h? I *think* you are trying to add a small increment to each value, based on the previous value. Am I close? Does this example help? zvalues = [1] # starting value increments = [0.01, 0.01, 0.02, 0.01, 0.01, 0.02, 0.01, 0.01] for h in increments: z = zvalues[-1] + h zvalues.append(z) print(zvalues) (Note: beware of floating point rounding.) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do loop in Python
Thank you so much for your reply. It was very helpful information and I used it in order to improve the program Here is the new version of the program: zvalues = [-200] # starting value hvalues = [10] # starting value increments = [1, 1, 1, 1, 1, 1, 1, 1] for N in increments: h = hvalues[-1] - N hvalues.append(h) z = zvalues[-1] + h zvalues.append(z) height = arange((z)*dz,0,dz) for z,when in enumerate(height): nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) The story is like this: I should define layers and thickness and see how the diffusion profile changes over the z. height (or depth) of the total thickness or 'z'. I basically, define 'z' in 10 layers and each layer is called ' N' . Difference between each layer is 'h', which is equal 10 micrometer. Now, what I like to do is the modification of nu based on each zvalue In fact, for each 'zvalue' o'z' step, I need to calculate a different value for 'nu' based on the available equation in the program. BUT, I am not sure, exactly, how to add the new do loop of z inside another loop of nu. I have done this way as well (the other way around): height = arange((z)*dz,0,dz) for z,when in enumerate(height): for N in increments: h = hvalues[-1] - N hvalues.append(h) z = zvalues[-1] + h zvalues.append(z) nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) but still no sign of 'nu changes' over 'z'! So, would it be possible to check that again? Thanks, Sue On Fri, Nov 25, 2011 at 12:36 PM, Steven D'Aprano st...@pearwood.info wrote: stm atoc wrote: Hi there, I am a new python user. I have a question regarding do loop. This is a simple program that I have written: - N=10 h=10.0 # [micrometer] z=-200.0 # [micrometer] You define N, h and z here, but later on you use them as loop variables. So these three values never get used: they are thrown away, and replaced by the values of the loops: h - 0, 1, 2, ... 9 N - 0, 1, 2, ... 9 z is especially troublesome, because it gets used for TWO loop variables, one inside the other. The inner z loop depends on the outer z loop, which makes it tricky to predict what values z will take. num = 0.05 #m**2/s dz = 1.0 nuh=[] tmax=3600 dt=20. nu=[]height = arange(z*dz,0,dz) What is arange? In physics, height is a scalar. But later on, you seen to use height as if it were a collection of values. outfile=open('nu.dat','w') outfile.write('height, nu, nuh') for z,when in enumerate(height): for h in range(10): for N in range(10): for z in range((N-z)+(N-h)): nuh.append(0.01 * exp(-0.05*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) --- What I like to do with this program is do loop like the fortran version of as follows: do i = 2, N z(i) = z(i-1) +h(i-1) end do How is z initialised? What is h? I *think* you are trying to add a small increment to each value, based on the previous value. Am I close? Does this example help? zvalues = [1] # starting value increments = [0.01, 0.01, 0.02, 0.01, 0.01, 0.02, 0.01, 0.01] for h in increments: z = zvalues[-1] + h zvalues.append(z) print(zvalues) (Note: beware of floating point rounding.) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do loop in Python
On 2011-11-25 14:46, stm atoc wrote: Here is the new version of the program: zvalues = [-200] # starting value hvalues = [10] # starting value increments = [1, 1, 1, 1, 1, 1, 1, 1] for N in increments: h = hvalues[-1] - N hvalues.append(h) z = zvalues[-1] + h zvalues.append(z) height = arange((z)*dz,0,dz) There is no arange in python. Could it be that you use numpy and import it with from numpy import *? for z,when in enumerate(height): I'm pretty sure this line doesn't do what you expect it to do. You have a sequence (a numpy array) named height and after calling enumerate you get a list of tuples in the form of [(0, height[0]), (1, height[1]), ...]. Now the for-loop iterates over this list and assigns z to the first value of the tuple (the index-values) and when to the second (the values from height). You later never use when but just use z. If you really want that, the enumerate is completly unnecessary and you could just use for z in range(len(height)). But I'm not sure if numpy arrays work with len(). nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) The story is like this: I should define layers and thickness and see how the diffusion profile changes over the z. height (or depth) of the total thickness or 'z'. I basically, define 'z' in 10 layers and each layer is called ' N' . Difference between each layer is 'h', which is equal 10 micrometer. Now, what I like to do is the modification of nu based on each zvalue In fact, for each 'zvalue' o'z' step, I need to calculate a different value for 'nu' based on the available equation in the program. BUT, I am not sure, exactly, how to add the new do loop of z inside another loop of nu. For me your explanations are still too confusing. Could it be that you are thinking way too complicated? My guess is you want to have a range of material thicknesses (from 1 to 200 micrometers in 10 micrometer-steps) and then you want from each thickness 10 different layers, right? import math # you should always tell us which modules you import num = 0.05 # some constant nu = [] # list of resulting values h = 10.0 # height of one layer thickness = range(0, 210, 10) # a list from 0 to 200 with step 10 (0, 10, 20, ..., 190, 200) layers = range(1,11) # a list from 1 to 10 for t in thickness: for l in layers: z = t + h * l # I'm not sure if you want to add or subtract the layer thickness nu = num + (0.01 * math.exp(-0.05 * (z + 200.0))) This will result in a big one-dimensional list where you calculate for each thickness the nu-value for 10 layers. Am I close? I'm still not sure about the steps and the height of the layers. I also wonder if it wouldn't be better to use a two-dimensional list. I have done this way as well (the other way around): height = arange((z)*dz,0,dz) for z,when in enumerate(height): for N in increments: h = hvalues[-1] - N hvalues.append(h) z = zvalues[-1] + h zvalues.append(z) nuh.append(0.001 * exp(-0.005*(z+200.0))*dz) #turbulence diffusivity m**2/s nu.append(num + nuh[z]) but still no sign of 'nu changes' over 'z'! As Charles has already mentioned, the values for nu are very similar (they start beginning to differ just at the seventh digit after the comma). How do you further process this values? If you plot them what's your scale? Bye, Andreas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor