示例#1
0
文件: variables.py 项目: certik/sfepy
    def create_matrix_graph( self, var_names = None, vvar_names = None ):
        """
        Create tangent matrix graph. Order of dof connectivities is not
        important here...
        """
        def _prepare_dc_lists( adof_conns, var_names = None ):
            if var_names is None:
                var_names = adof_conns.iterkeys()

            gdcs = {}
            for var_name in var_names:
                adcs = adof_conns[var_name]
                for ig, dc in adcs.volume_d_cs.iteritems():
##                     print dc
                    gdcs.setdefault( ig, [] ).append( dc )
            return gdcs

        shape = (self.avdi.ptr[-1], self.adi.ptr[-1])
        output( 'matrix shape:', shape )
        if nm.prod( shape ) == 0:
            output( 'no matrix!' )
            return None

        cgdcs = _prepare_dc_lists( self.adof_conns, var_names )
##         print cgdcs
##         pause()
        if self.has_virtual_d_cs:
            rgdcs = _prepare_dc_lists( self.avdof_conns, vvar_names )
        else:
            rgdcs = cgdcs

        ##
        # Make all permutations per element group.
        rdcs = []
        cdcs = []
        for ig in rgdcs.iterkeys():
            rgdc, cgdc = rgdcs[ig], cgdcs[ig]
            for perm in la.cycle( [len( rgdc ), len( cgdc )] ):
                rdcs.append( rgdc[perm[0]] )
                cdcs.append( cgdc[perm[1]] )
#                print ' ', perm, '->', rdcs[-1].shape, cdcs[-1].shape

        output( 'assembling matrix graph...' )
        tt = time.clock()

#	shape = nm.array( shape, dtype = nm.long )
        ret, prow, icol = raw_graph( int( shape[0] ), int( shape[1] ),
                                    len( rdcs ), rdcs, cdcs )
        output( '...done in %.2f s' % (time.clock() - tt) )
        nnz = prow[-1]
        output( 'matrix structural nonzeros: %d (%.2e%% fill)' \
                % (nnz, float( nnz ) / nm.prod( shape ) ) )
##         print ret, prow, icol, nnz
	
        data = nm.zeros( (nnz,), dtype = self.dtype )
        matrix = sp.csr_matrix( (data, icol, prow), shape )
##         matrix.save( 'matrix', format = '%d %d %e\n' )
##         pause()

        return matrix
示例#2
0
文件: blockgen.py 项目: certik/sfepy
def main():
    parser = OptionParser(usage=usage, version="%prog")
    parser.add_option(
        "-o", "", metavar="filename", action="store", dest="output_filename", default="out.vtk", help=help["filename"]
    )
    parser.add_option(
        "-d", "--dims", metavar="dims", action="store", dest="dims", default="[1.0, 1.0, 1.0]", help=help["dims"]
    )
    parser.add_option(
        "-s", "--shape", metavar="shape", action="store", dest="shape", default="[11, 11, 11]", help=help["shape"]
    )
    parser.add_option(
        "-c",
        "--centre",
        metavar="centre",
        action="store",
        dest="centre",
        default="[0.0, 0.0, 0.0]",
        help=help["centre"],
    )
    (options, args) = parser.parse_args()

    dims = eval("nm.array( %s, dtype = nm.float64 )" % options.dims)
    shape = eval("nm.array( %s, dtype = nm.int32 )" % options.shape)
    centre = eval("nm.array( %s, dtype = nm.float64 )" % options.centre)

    print dims
    print shape
    print centre

    dim = shape.shape[0]

    x0 = centre - 0.5 * dims
    dd = dims / (shape - 1)

    grid = nm.zeros(shape, dtype=nm.float64)
    n_nod = nm.prod(shape)
    coors = nm.zeros((n_nod, dim + 1), dtype=nm.float64)

    # This is 3D only...
    bar = MyBar("       nodes:")
    bar.init(n_nod)
    for ii, ic in enumerate(cycle(shape)):
        ix, iy, iz = ic
        grid[ix, iy, iz] = ii
        coors[ii, :-1] = x0 + ic * dd
        if not (ii % 100):
            bar.update(ii)
    print
    n_el = nm.prod(shape - 1)
    conn = nm.zeros((n_el, 8), dtype=nm.int32)
    bar = MyBar("       elements:")
    bar.init(n_el)
    for ii, (ix, iy, iz) in enumerate(cycle(shape - 1)):
        conn[ii, :] = [
            grid[ix, iy, iz],
            grid[ix + 1, iy, iz],
            grid[ix + 1, iy + 1, iz],
            grid[ix, iy + 1, iz],
            grid[ix, iy, iz + 1],
            grid[ix + 1, iy, iz + 1],
            grid[ix + 1, iy + 1, iz + 1],
            grid[ix, iy + 1, iz + 1],
        ]
        if not (ii % 100):
            bar.update(ii)
    print
    mat_id = nm.zeros((n_el,), dtype=nm.int32)
    desc = "3_8"

    mesh = Mesh.from_data(options.output_filename, coors, [conn], [mat_id], [desc])
    mesh.write(options.output_filename, io="auto")
示例#3
0
def main():

    parser = OptionParser( usage = usage, version = "%prog 42" )
    parser.add_option( "-s", "--scale", type = int, metavar = 'scale',
                       action = "store", dest = "scale",
                       default = 2, help = help['scale'] )
    parser.add_option( "-e", "--eps", type = float, metavar = 'eps',
                       action = "store", dest = "eps",
                       default = 1e-8, help = help['eps'] )
    parser.add_option( "-t", "--test",
                       action = "store_true", dest = "test",
                       default = False, help = help['test'] )
    parser.add_option( "-n", "--no-mvd",
                       action = "store_true", dest = "nomvd",
                       default = False, help = help['nomvd'] )
    (options, args) = parser.parse_args()

    if options.test:
        test()
        return

    if (len( args ) == 2):
        filename_in = args[0]
        filename_out = args[1]
    else:
        parser.print_help()
        return

    print 'scale:', options.scale
    print 'eps:', options.eps

    mesh_in = Mesh.from_file( filename_in )
    bbox = mesh_in.get_bounding_box()
    print 'bbox:\n', bbox
    mscale = bbox[1] - bbox[0]
    centre0 = 0.5 * (bbox[1] + bbox[0])
    print 'centre:\n', centre0

    scale = nm.array( options.scale, dtype = nm.float64 )

    # Normalize original coordinates.
    coor0 = (mesh_in.nod0[:,:-1] - centre0) / (mscale)
    dim = mesh_in.dim

    coor0, mesh_in.conns = fix_double_nodes( coor0, mesh_in.conns, options.eps )
    if not options.nomvd:
        mes0 = get_min_edge_size( coor0, mesh_in.conns )
        mvd0 = get_min_vertex_distance( coor0, mes0 )
        if mes0 > (mvd0 + options.eps):
            print '          original min. "edge" length: %.5e' % mes0
            print 'original approx. min. vertex distance: %.5e' % mvd0
            print '-> still double nodes in input mesh!'
            print 'try increasing eps...'
            raise ValueError

    for indx in cycle( [options.scale] * dim ):
        aindx = nm.array( indx, dtype = nm.float64 )
        centre = 0.5 * (2.0 * aindx - scale + 1.0)
        print indx, centre

        if aindx.sum() == 0:
            coor = coor0 + centre
            conns = mesh_in.conns
        else:
            coor1 = coor0 + centre
            conns1 = mesh_in.conns

            cmap = find_map( coor, coor1, eps = options.eps )
            if not cmap.size:
                print 'non-periodic mesh!'
#                raise ValueError
            else:
                print cmap.size / 2
            coor, conns = merge_mesh( coor, conns, coor1, conns1, cmap,
                                     eps = options.eps )

    if not options.nomvd:
        mes = get_min_edge_size( coor, conns )
        mvd = get_min_vertex_distance( coor, mes0 )

        print '          original min. "edge" length: %.5e' % mes0
        print '             final min. "edge" length: %.5e' % mes
        print 'original approx. min. vertex distance: %.5e' % mvd0
        print '   final approx. min. vertex distance: %.5e' % mvd
        if mvd < 0.99999 * mvd0:
            if mvd0 < (mes0 - options.eps):
                print '-> probably non-periodic input mesh!'
                print '   ... adjacent sides were not connected!'
                print '   try increasing eps...'
            else:
                print '-> input mesh might be periodic'
                print '   try increasing eps...'
        else:
            print '-> input mesh looks periodic'
    else:
        print 'non-periodic input mesh detection skipped!'

    print 'renormalizing...'
    coor = (coor * mscale) / scale
    print 'saving...'
    mesh_out = make_mesh( coor, conns, mesh_in )
    mesh_out.write( filename_out, io = 'auto' )
    print 'done.'