0

My code is as follows (there's about 100 lines before of setting values for the loop which seem to be working so I've just included necessary values):

fraction=np.array([0.5, 0.3, 0.2])
tauC=np.array([30.,300.,100000.])

dC_memory=np.zeros((1,3))
dC_frac=np.zeros((1,3))    

for j in range(0,ens_num):

    dC_memory=np.zeros((1,3))
    for n in range(0,N-1):


                # C02 Concentration

                for m in range(0,3):

                    dC_frac[m]=fraction[m]*E[j,n+1]-dC_memory[m]/(tauC[m])
                    dC_memory[m]=dC_memory[m]+dC_frac[m]*dt
                    dC[j,n]=dC[j,n]+dC_frac[m]*dt



                C[j,n+1]=C[j,n]+dC[j,n]

                # Temperature

                dT[j,n]=((T2eq*math.log(C[j,n+1]/Cpi)/math.log(2))-T[j,n])*(dt/tauT)
                T[j,n+1]=T[j,n]+dT[j,n]

                # Adaptation

                dTadp[j,n]=(T[j,n]-Tadp[j,n])*dt/tauA
                Tadp[j,n+1]=Tadp[j,n]+dTadp[j,n]
                Tdiff[j,n+1]=0.5*(abs(T[j,n]-Tadp[j,n+1])+T[j,n]-Tadp[j,n+1])



                if yi[j,n+1]+xi0[k]<=mu:


                    count[j]=count[j]+1/N

When I run this I get the error on the line dC[j,n]=dC[j,n]+dC_frac[m]*dt saying

ValueError: setting an array element with a sequence.

I'm new to python but I know that python indexing starts from 0, but I cant understand why this code stops here.

2
  • What exactly is dC? You never initialize it in the code you posted. I suspect it's an ordinary Python list (instead of a numpy array), in which case dC[j,n] is not valid (you'd have to use dC[j][n] for multidimensional indexing). Commented Feb 3, 2018 at 15:40
  • Printing data, (conditional) expression results, anything relevant, is an effective debugging technique. You can print stuff just before the offending line or catch the error and print in the except suite. It will give you more clues to what is happening. Commented Feb 3, 2018 at 16:15

1 Answer 1

2

Your example code is not complete. But I think the bug is clear.

By defining

 dC_frac=np.zeros((1,3))

You dC_frac is a multidimensional array of shape (1, 3). Use dC_frac.shape you will find it's (1, 3), not, (3,).

Thus in

for m in range(0,3):
    dC_frac[m]=fraction[m]*E[j,n+1]-dC_memory[m]/(tauC[m])
    ...

Your dC_frac[m] is an array of 3 elements, not a scalar.

If your dC[j, n] and dt are scalars,

dC[j,n]=dC[j,n]+dC_frac[m]*dt

This will assign an array of 3 elements to an entry. Thus the error.

To fix, just use

dC_memory=np.zeros(3)
dC_frac=np.zeros(3) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.