def fib2meshalyzer(filename): """ Given a filename for a CARP mesh (.pts, .elem and .lon) this functions generates the .vec and .vpts files necessary to visualize vector data in meshalyzer. """ ptsFile = "%s.pts" % filename lonFile = "%s.lon" % filename vecFile = "%s.vec" % filename elemFile = "%s.elem" % filename vptsFile = "%s.vpts" % filename np_xyz = read_array_pts(ptsFile) elemList = read_array_elem(elemFile) # copy filename.lon to filename.vec shutil.copy(lonFile, vecFile) # create .vpts file f = open(vptsFile, "w") f.write("%d\n" % len(elemList)) for e in elemList: numNode = getNumNode[e[0]] nodeList = [e[j + 1] for j in xrange(numNode)] centroid = get_element_center(nodeList, np_xyz) f.write("%f %f %f\n" % (centroid[0], centroid[1], centroid[2])) f.close() print ".vec and .vpts files were generated!"
def fit_lambda(igb_file, pts_file, resolution=100): """ Function to fit the space constant lambda Input: igb_file - with Vm at each time slice pts_file - file describing the mesh resolution - of the mesh """ [vm, hd] = read_igb_slice(igb_file) Vm = vm.squeeze() xyz = read_array_pts(pts_file) ytmp = xyz[:,1] ztmp = xyz[:,2] size = len(xyz[:,1]) # list with the nodes' indexes of the line aux = -resolution/2 lindx = [i for i in xrange(size) if ytmp[i] == aux and ztmp[i] == aux] value1 = Vm[100,:] x = zeros((len(lindx))) # space data = zeros((len(lindx))) # vm at t=100ms ### ### improve the loop and assignments below ### for i in lindx: x[i] = xyz[i,0] data[i] = value1[i] def dbexpl(x,p): """ Example of curve fitting for a*exp(-x/p1) """ return( p[0]*exp(-(x+25000)/p[1]) + data[-1] ) def residuals(p,data,x): err = data - dbexpl(x,p) return err ### vm curve fitting ### p0 = [10,1000] # initial guesses guessfit = dbexpl(x,p0) # data evaluated with p0 pbest = leastsq(residuals,p0,args=(data,x),full_output=1) # minimize the sum of squares of a set of equations bestparams = pbest[0] # solution (estimated parameters) cov_x = pbest[1] # estimate of the covariance matrix of the solution print 'Best fit parameters: ' , bestparams #print cov_x datafit = dbexpl(x,bestparams) plot(x,data,'b',x,datafit,'r') legend(['Vm(x,t=100ms)','fitting'], 'upper right') xlabel('Time') title('Space constant ''$\lambda$'' fitting') grid(True) show()
def condVelocity(ptsFile, actFile, output=True): """ Function to calculate the conduction velocity in a cable slab Input: ptsFile - description of the nodes actFile - file with activation time of the nodes Example: 0 1 2 3 4 5 6 7 8 A B Bernardo Martins Rocha, 2008 """ xyz = read_array_pts(ptsFile) #act = read_array(actFile) # deprecated under new SciPy/Numpy act = loadtxt(actFile) actTeste = act distA = max(xyz[:,0]/4) distB = 3*max(xyz[:,0]/4) # node A tmp = abs(xyz[:,0] - distA) tmpID = where(tmp == tmp.min()) # where return tuple aux = tmpID[0][0] + 1 nodeA = xyz[aux] actTI = where(act[:,0] == aux) # activation time index if size(actTI) == 0: if output: print " NodeA was not activated. CV is unknown!" return nan else: actTI = actTI[0][0] # from tuple to scalar actTA = act[actTI , 1] # activation time for node A # node B tmp = abs(xyz[:,0] - distB) tmpID = where(tmp == tmp.min()) # where return tuple aux = tmpID[0][0] + 1 nodeB = xyz[aux] actTI = where(act[:,0] == aux) # activation time index if size(actTI) == 0: if output: print " NodeB was not activated. CV is unknown!" return nan else: actTI = actTI[0][0] # from tuple to scalar actTB = act[actTI , 1] # activation time for node B distAB = sqrt(sum(pow(nodeB - nodeA,2))) condve = distAB / (actTB - actTA) if output: print " CV = %.3f m/s" % (condve/1000) return (condve/1000)
def tag_elem_file (elem_file, node_file, region_list): """ Function to tag rectangular areas in structured meshes in CARP Bernardo M. Rocha, 2009 """ new_efile = open('element.elem', 'w') elem = read_array_elem (elem_file) pts = read_array_pts (node_file) for r in region_list: for e in elem: elem_type = e[0] if elem_type == 'Qd': node_range = xrange(1,4+1) node_tag = e[5] for n in node_range: g = int( e[n] ) node = Point ( pts[g][0], pts[g][1], pts[g][2] ) # check if node is inside region if ( (node.x >= r.bottom.x) and (node.x <= r.bottom.x + r.width) and (node.y >= r.top.y - r.height) and (node.y <= r.top.y)): node_tag = r.tag # end if # end for n e[5] = node_tag # end if Qd # end for e # end for r # write to file new_efile.write('%d\n' % len(elem)) for e in elem: new_efile.write('Qd %d %d %d %d %d \n' % (int(e[1]), int(e[2]),int(e[3]),int(e[4]),int(e[5]))) new_efile.close() new_efile.close()