#!/usr/bin/env python
"""
Created on Sat Feb 15 10:20:58 2014

Author: Oren Freifeld
Email: [email protected]
"""
import copy
from of.utils import Bunch

_params_flow_int = Bunch()
_params_flow_int.nTimeSteps = 10
_params_flow_int.dt = 0.0001
_params_flow_int.nStepsODEsolver = 10


def get_params_flow_int():
    return copy.deepcopy(_params_flow_int)


if __name__ == "__main__":
    print get_params_flow_int()
示例#2
0
def get_stuff_for_the_local_version(cpa_space):  
    if cpa_space.tess not in ['I','II']:
        raise ValueError(cpa_space.tess)
#    compute_maps = cpa_space.dim_domain > 1 or cpa_space.tess == 'I'

    compute_maps =  cpa_space.dim_domain==1 or cpa_space.tess == 'I'
    
    if not compute_maps:
        return None

    
            
    
    nC = cpa_space.nC
    nHomoCoo = cpa_space.nHomoCoo
    lengthAvee = cpa_space.lengthAvee
    dim_domain = cpa_space.dim_domain
    dim_range = cpa_space.dim_range

    b = Bunch()
    
    cells_verts_homo_coo = cpa_space.tessellation.cells_verts_homo_coo
    
    if compute_maps:       
        X = np.zeros((nC,lengthAvee,lengthAvee))
        Xinv = np.zeros_like(X)
    
        if dim_domain == 1:
            for (x,xinv,(vrt0,vrt1)) in zip(X,Xinv,cells_verts_homo_coo):
                x[0,:2]=vrt0
                x[1,:2]=vrt1                    
                xinv[:]=inv(x)             
        elif dim_domain == 2:
            for (x,xinv,(vrt0,vrt1,vrt2)) in zip(X,Xinv,cells_verts_homo_coo):
                x[0,:3]=x[1,3:]=vrt0
                x[2,:3]=x[3,3:]=vrt1
                x[4,:3]=x[5,3:]=vrt2           
                xinv[:]=inv(x)      
        elif dim_domain == 3:
            for (x,xinv,(vrt0,vrt1,vrt2,vrt3)) in zip(X,Xinv,cells_verts_homo_coo):             
                x[0,:4]=x[1,4:8]=x[2,8:]=vrt0
                x[3,:4]=x[4,4:8]=x[5,8:]=vrt1    
                x[6,:4]=x[7,4:8]=x[8,8:]=vrt2
                x[9,:4]=x[10,4:8]=x[11,8:]=vrt3            
                xinv[:]=inv(x)          
             
        else:
            raise NotImplementedError(dim_domain)
        
    
    vert_tess = []
    vert_tess_one_cell = []
    ind_into_vert_tess = np.zeros((nC,nHomoCoo),np.int)
    
    for c,cell_verts in enumerate(cells_verts_homo_coo):
        for j,v in enumerate(cell_verts):
            t = tuple(v.tolist())
            if t not in vert_tess:        
                vert_tess.append(t)
                # c is the cell index
                # j is the index of this vertex within that cell 
                vert_tess_one_cell.append((c,j))
            ind_into_vert_tess[c,j]=vert_tess.index(t)
            
    vert_tess = np.asarray(vert_tess)
    vert_tess_one_cell = np.asarray(vert_tess_one_cell)
     
    
    b.vert_tess = vert_tess
    b.ind_into_vert_tess = ind_into_vert_tess
    b.Xinv = Xinv
    b.X = X
    
    
    """
    Build a sparse matrix H such that    
    Avees = H times velTess    
    The values of H, which is sparse, are dictated by vertTess.
    H.shape = (lengthAvee*nC,len(vert_tess)*dim_range)
    """
    H = np.zeros((lengthAvee*nC,len(vert_tess)*dim_range))
    
    for c in range(nC):
        ind = ind_into_vert_tess[c]
        ind_all_coo = np.zeros((len(ind),dim_range),np.int)
        for coo in range(dim_range):
            ind_all_coo[:,coo]=ind*dim_range+coo  
        
        
        
        H[c*lengthAvee:(c+1)*lengthAvee,ind_all_coo.ravel()]=Xinv[c]
#    
 
    
    
    """
    Build a sparse matrix H such that    
    velTess  = G times Avees 
    G.shape = (len(vert_tess)*dim_range,lengthAvee*nC)
    """
    G = np.zeros((len(vert_tess)*dim_range,lengthAvee*nC))
    
    
    for i in range(vert_tess.shape[0]):
        # c is the cell index
        # j is the index of this vertex within this cell
        c,j = vert_tess_one_cell[i]
        for coo in range(dim_range):
            G[i*dim_range+coo,lengthAvee*c:lengthAvee*(c+1)]=X[c][j*dim_range+coo]
       
#    ipshell('hi')

     
    H = lil_matrix(H)     
    G = lil_matrix(G)
     
    b._mat_velTess2Avees = H
    b._mat_Avees2velTess = G   
#   
     
    if 1:
        def mv1(v):
            return H.dot(v)
        def mv2(v):
            return G.dot(v)
        def rmv1(v):
            return H.T.dot(v)
        def rmv2(v):
            return G.T.dot(v)
        def mm1(V):
            return H.dot(V)
        def mm2(V):
            return G.dot(V)            
        _H = ssl.LinearOperator(H.shape,matvec=mv1,
                                rmatvec=rmv1,
                                matmat=mm1)
        _G = ssl.LinearOperator(lil_matrix(G).shape,matvec=mv2,
                                rmatvec=rmv2,
                                matmat=mm2)
    
        b.linop_velTess2Avees = _H
        b.linop_Avees2velTess = _G            
    
    return b
示例#3
0
#!/usr/bin/env python
"""
Created on Tue Feb  4 11:04:35 2014

Author: Oren Freifeld
Email: [email protected]
"""
import os
import inspect

from of.utils import Bunch,FilesDirs
dirname_of_this_file = os.path.dirname(os.path.abspath(
                        inspect.getfile(inspect.currentframe())))
if len(dirname_of_this_file)==0:
    raise ValueError
print 'dirname_of_this_file',dirname_of_this_file
dirname = os.path.join(dirname_of_this_file,'..')
dirname = os.path.abspath(dirname)

FilesDirs.raise_if_dir_does_not_exist(dirname)

dirnames = Bunch()
dirnames.cpa = os.path.join(dirname,'cpa_files')



if __name__ == "__main__":
    for k in sorted(dirnames.keys()):
        print '{}:\n\t{}'.format(k,dirnames[k])
    
#!/usr/bin/env python
"""
Created on Sat Feb 15 10:20:58 2014

Author: Oren Freifeld
Email: [email protected]
"""
import copy
from of.utils import Bunch

_params_flow_int = Bunch()
_params_flow_int.dt = 0.01
_params_flow_int.nTimeSteps=1.0 / _params_flow_int.dt 
_params_flow_int.nStepsODEsolver = 10

def get_params_flow_int():
    return copy.deepcopy(_params_flow_int)

if __name__ == "__main__":
    print get_params_flow_int()
示例#5
0
    def getKernel(self):
        tess = self.tess

        filenames = Bunch()
        dname = os.path.dirname(__file__)
        filenames['range_and_dim_are_the_same_shared'] = os.path.join(
            dname, 'transformKernels64_nD_noloop.cu')

        filenames[
            'range_and_dim_are_the_same_only_As_are_shared'] = os.path.join(
                dname, 'transformKernels64_nD_noloop_only_As_are_shared.cu')
        filenames['range_and_dim_are_the_same_noshared'] = os.path.join(
            dname, 'transformKernels64_nD_noloop_noshared.cu')

        filenames['scalar_shared'] = os.path.join(
            dname, 'transformKernels64_nD_scalar_noloop.cu')

        filenames['scalar_only_As_are_shared'] = os.path.join(
            dname, 'transformKernels64_nD_scalar_noloop_only_As_are_shared.cu')

        n = self.dim_domain
        if n not in [1, 2, 3]:
            if self.dim_domain != self.dim_range:
                raise NotImplementedError
            n = 'H'
            n1 = n
            n2 = n
        else:
            n1 = self.dim_domain
            n2 = self.dim_range

        s = '_'.join([str(n)] * 2)

        filenames[s] = Bunch()
        filenames[s] = os.path.join(dname, 'calc_{0}Dto{1}D.cu'.format(n1, n2))

        if self.sharedmemory == 0:
            filenames[s] = filenames[s].replace('.cu', '_no_shared.cu')
        elif self.sharedmemory == 1:
            filenames[s] = filenames[s].replace('.cu',
                                                '_only_As_are_shared.cu')
        elif self.sharedmemory == 2:
            pass
        else:
            raise ValueError(self.sharedmemory)

#        if n==2:
#            if ((computer.has_good_gpu_card and 320<=self.nCells)
#                or
#                (320 <=self.nCells)
#                ):
#                filenames[s]=filenames[s].replace('.cu','_only_As_are_shared.cu')
#
#        elif n == 3:
#            if ((computer.has_good_gpu_card and 320<=self.nCells)
#                or
#                (320 <=self.nCells)
#                ):
#                filenames[s]=filenames[s].replace('.cu','_only_As_are_shared.cu')
#        if n>3:
#            filenames[s]=filenames[s].replace('.cu','_no_shared.cu')
#
#
        if not 'calc_transformation_gpu' in dict(self.__dict__):
            k = str(n1) + '_' + str(n2)
            filename = filenames[k]

            self.kernel_filename = filename
            for i in range(2):
                try:
                    FilesDirs.raise_if_file_does_not_exist(filename)
                    break
                except FileDoesNotExistError:
                    print "Attempt {} out {}".format(i + 1, 5)
                    print "Couldn't find {0}.\nMaybe the network is (temporarily?) down...".format(
                        filename)
                    print "Let me sleep over it for a second before I try again"
                    time.sleep(1)
                    pass
            else:  # In effect, we didn't break out of the loop.
                raise

            with open(filename, 'r') as content_file:
                kernel = content_file.read()
            # We ran into numerical problems with 32bit...
            # Had to switch to 64
            if self.my_dtype == np.float32:
                kernel = re.sub("double", "float", kernel)
            # Define the number of cells here dynamically.
            addition = (
                '#define N_CELLS {}\n'.format(self.nCells) +
                '#define DIM {}\n'.format(self.dim_domain) +
                '#define TESS_TYPE {}'.format(2 - ['II', 'I'].index(tess)) +
                ' // 2 is II; 1 is I\n\n')

            kernel = addition + kernel
            #            print kernel
            self.kernel = kernel

            print "kernel_filename"
            print self.kernel_filename

            try:
                mod = comp.SourceModule(kernel, include_dirs=include_dirs)
            except:
                raise
                print '-' * 60
                print 'comp.SourceModule(kernel) failed!'
                print 'trying without shared memory. The code might run slower.'
                print '-' * 60
                mod = comp.SourceModule(kernel.replace('__shared__', ''))
#                ipshell('comp.SourceModule(kernel) failed!')
#                raise
            if self.dim_domain == self.dim_range:

                # At some point the line below was commented out. Not sure why
                try:
                    self.calc_T_simple_gpu = mod.get_function('calc_T_simple')
                except:
                    pass

                self.calc_T_gpu = mod.get_function('calc_T')
                self.calc_trajectory_gpu = mod.get_function('calc_trajectory')
                self.calc_v_gpu = mod.get_function('calc_v')
                self.calc_cell_idx_gpu = mod.get_function('calc_cell_idx')

#                self.calc_grad_theta_gpu = mod.get_function('calc_grad_theta')
            elif self.dim_range == 1:
                self.calc_v_gpu_scalar = mod.get_function('calc_v_scalar')
                self.calc_T_gpu_scalar = mod.get_function('calc_T_scalar')
            else:
                raise NotImplementedError(self.dim_domain, self.dim_range)
示例#6
0
#!/usr/bin/env python
"""
Created on Tue Feb  4 11:04:35 2014

Author: Oren Freifeld
Email: [email protected]
"""
import os
import inspect

from of.utils import Bunch, FilesDirs
dirname_of_this_file = os.path.dirname(
    os.path.abspath(inspect.getfile(inspect.currentframe())))
if len(dirname_of_this_file) == 0:
    raise ValueError
print 'dirname_of_this_file', dirname_of_this_file
dirname = os.path.join(dirname_of_this_file, '..')
dirname = os.path.abspath(dirname)

FilesDirs.raise_if_dir_does_not_exist(dirname)

dirnames = Bunch()
dirnames.cpa = os.path.join(dirname, 'cpa_files')

if __name__ == "__main__":
    for k in sorted(dirnames.keys()):
        print '{}:\n\t{}'.format(k, dirnames[k])
#!/usr/bin/env python
"""
Created on Sat Feb 15 10:20:58 2014

Author: Oren Freifeld
Email: [email protected]
"""
import copy
from of.utils import Bunch

_params_flow_int = Bunch()
_params_flow_int.nTimeSteps=10
_params_flow_int.dt = 0.0001
_params_flow_int.nStepsODEsolver = 10

def get_params_flow_int():
    return copy.deepcopy(_params_flow_int)

if __name__ == "__main__":
    print get_params_flow_int()