arrays - TypeError: 'numpy.ndarray' object is not callable -


i have seen other people asking similar can not figure out problem anyway. trying translate matlab code python , have problem after following line in loop:

dx = abs(np.diff(g_coord(num)))

below have code loop. appreciated. tried fix myself unsuccessfully. sorry if stupid mistake. matlab lines kept python comments in case helps.

import numpy np scipy.sparse import lil_matrix   # physical parameters seconds_per_yr = 60*60*24*365; # number of seconds in 1 year lx = 10000 ; #length of spatial domain (m) cp = 1e3 ; # rock heat capacity (j/kg/k) rho = 2700 ; # rock density (kg/mˆ3) k = 3.3 ; # bulk thermal conductivity (w/m/k) kappa = k/(cp*rho); # thermal diffusivity (mˆ2/s) tb = 0 ; # temperatures @ boundaries (o c) = 2.6e-6 ; # heat production (w/mˆ3) h = a/(rho*cp); # heat source term (o k/s) % numerical parameters dt = 1000*seconds_per_yr ; # time step (s) ntime = 5000 ; # number of time steps nels = 40 ; # total number of elements nod = 2 ; # number of nodes per element nn = nels+1 # total number of nodes dx = lx/nels ; # element size  g_coord =  np.arange(0, lx+1, dx)#[0:dx:lx]  bcdof = np.array([1, nn]); #[ 1 nn ] ; boundary nodes bcval = np.array([tb, tb]); #[ tb tb ] ; # boudary values  g_num = np.zeros((nod, nels), float); #zeros(nod,nels) ; g_num[0,:]=np.arange(1, nn); #g_num(1,:) = [1:nn-1] ; g_num[1,:]=np.arange(2, nn+1); #g_num(2,:) = [2:nn] ;   # initialise matrices , vectors ff = np.zeros((nn,1), float); # system load vector b = np.zeros((nn,1), float); # system rhs vector lhs=lil_matrix((nn, nn)) #lhs = sparse(nn,nn); system lhs matrix rhs=lil_matrix((nn, nn)) #rhs = sparse(nn,nn); system rhs matrix displ = np.zeros((nn,1), float); # initial temperature (o c)   #----------------------------------------------------- # matrix assembly #-----------------------------------------------------  # matlab version of loop #----------------------------------------------------- #for iel=1:nels # loop on elements #    num = g_num(:,iel) ; # retrieve equation number #    dx = abs(diff(g_coord(num))) ; # length of element #    mm = dx*[1/3 1/6 ; 1/6 1/3 ] ;# mass matrix #    km = [kappa/dx -kappa/dx ; -kappa/dx kappa/dx ]; #diffn matrix #    f = dx*h*[1/2 ; 1/2] ; # load vector #    lhs(num,num) = lhs(num,num) + mm/dt + km ; # assemble lhs #    rhs(num,num) = rhs(num,num) + mm/dt ; # assemble rhs #    ff(num) = ff(num) + f ; # assemble load #end # end of element loop  #python version of loop #----------------------------------------------------- iel in range(0, nels): # loop on elements     num = g_num[:,iel]  # retrieve equation number     #print(num)     dx = abs(np.diff(g_coord[num]))  # length of element     mm = dx*(np.array([[1/3, 1/6],[1/6, 1/3]]))  # mass matrix     km = np.array([[kappa/dx, -kappa/dx],[-kappa/dx, kappa/dx]])      f = dx*h*(np.array([1/2, 1/2])).reshape(-1,1) # load vector     lhs[num,num] = lhs[num,num] + mm/dt + km # assemble lhs     rhs[num,num] = rhs[num,num] + mm/dt # assemble rhs     ff[num] = ff[num] + f # assemble load 

the error seems because num float. do:

dx = abs(np.diff(g_coord[np.int32(num)]))

however raises error few lines later because num 2-element array. know code should not. if have more issues, can comment below or edit question first problem solved.

also, noticed left ; @ end of lines in matlab. not need in python. also, think there no need specify float when create matrices of zeros, naturally float.


Comments

Popular posts from this blog

Ansible warning on jinja2 braces on when -

Parsing a protocol message from Go by Java -

html - How to custom Bootstrap grid height? -