Beispiel #1
0
def fields2vtkfile( fields, filename, mesh = None, only_dofname=None, format="binary",path=None,extrafields=None,header='Header (unused)'):
    """ def fields2vtkfile( fields, filename, mesh = None,
                            only_dofname=None, format="ascii",path=None):
        Given a list of FIELDS (or just one field), a FILENAME and, optionally
        a MESH and some other values, this function goes through all degrees of
        freedoms in the field list and writes their data to the vtk file with name
        FILENAME.

        Currently, scalar and vector data on 2d and 3d meshes is supported.

        Optional extra arguments are:
            format      : 'ascii' or 'binary' (how the data is stored in the vtk file)
            only_dofname: if a name for a degree of freedom (dof) is given here, then only
                          the data for this dof is written to the vtk file.
            mesh        : if not provided, the default mesh in nfem2 is chosen. If there is
                          no default mesh defined, then a mesh needs to be provided here,
                          or the function will report an error.
            path        : if given, the file will be written no PATH/FILENAME.
                          if path is not specified, the file will be written to the
                          nfem default directory (nbase.run_dir)

            extrafields : if given, this needs to be a tuple of a fieldname and the data
                          in Python lists. Example usage: providing the maxangle data
    """

    filepath = nsim.snippets.output_file_location(filename,path)

    log.debug("Entering fields2vtkfile to write %s" % filepath)
    #check that fields are of the right type

    if type(fields) != type([]):
        fieldlist = [fields]
    else:
        fieldlist = fields
    if len(fieldlist) == 0:
        raise NfemValueError, "No field received"
    import ocaml
    if ocaml.sys_ocamlpill_type(fieldlist[0]) != 'FEM Field':
        raise NfemValueError, "Expect FEM Fields as input"

    #get the mesh (should check the type here, really.)
    if mesh == None:
        mesh=get_default_mesh()
    meshinfo = mesh.tolists()
    points = meshinfo[0][2]
    log.debug("Number of points in mesh=%d" % len(points))
    tmp = meshinfo[2][2]
    simplices = map( lambda a: a[0], tmp)
    log.debug("Number of simplices in mesh=%d" % len(simplices))

    #check dimensionality of the mesh
    if len(points[0])==2:
        log.debug("mesh is 2d")
        mesh_dim = 2
    elif len(points[0])==3:
        log.debug("mesh is 3d")
        mesh_dim = 3
    else:
        log.debug("mesh is 1d")
        mesh_dim = 1
        log.warn("1d data is new -- may be buggy (fangohr 26/10/2006)")

    #assume that 'fieldlist' is a list of FEM fields.
    #First, create the following data structure.
    #dof_to_process = [field, dof, dim]
    dof_to_process = []

    for field in fieldlist:
        #learn about dof in this field:
        content = nfem.data_doftypes(field)
        for dof in content:
            name, maxind = dof
            if only_dofname:
                if name != only_dofname:
                    continue
                
            if len(maxind)>1:
                raise NfemValueError,"Can only deal with scalar and vector data at the moment (no tensor data)."
            if len(maxind)==0:
                dim = 1 #this is a scalar
            elif len(maxind)==1:
                if maxind[0] ==2: #this is a 2d vector
                    dim = 2
                elif maxind[0] ==3: #this is a 3d vector
                    dim = 3
                else:
                    raise NfemValueError,"Can only process vectors in 2 and 3 dimensions"
            dof_to_process.append( (field, name, dim) )

            log.debug("adding %s" % str(dof_to_process[-1]) )

    #pyvtk requires us to create a vtk object and to give it the first
    #dof at that time. For all subsequent dofs, we can add them one by one.
    
    #check that we have some dofs to process:
    if len(dof_to_process)==0:
        raise NfemUserError,"Have not found any degrees of freedom (Maybe you have used only_dofname with a wrong name?)"

    names = str(map(lambda a : a[1], dof_to_process))
    log.info("About to write field(s) '%s' to %s (%s)" % (names,filename,filepath))

    #create vtk object with first data set
    field, name,dim = dof_to_process[0]
    data = _field2meshdata(len(points),field,name,dim)
    vtk = _vtk_createVtkData(points,simplices,data,name,header)
    log.log(15,"Adding %d-dim field %s to %s" % (dim,name,filename))

    #and then process all others
    for dof in dof_to_process[1:]:
        field, name,dim = dof
        if only_dofname:
            if name!=only_dofname:
                continue
        log.debug("Adding %d-dim dof %s to %s" % (dim,name,filename))
        data = _field2meshdata(len(points),field,name,dim)
        vtk = _vtk_addPointdata(vtk,data,name)


    #and process all extra fields (not nfem fields)

    if extrafields:
        for name, data in extrafields:
            if only_dofname:
                if name!=only_dofname:
                    continue
            log.log(15,"Adding data %s to %s" % (name,filename))

            #from IPython.Shell import IPShellEmbed
            #ipshell = IPShellEmbed()
            #ipshell()



            vtk = _vtk_addPointdata(vtk,data,name)

    vtk.tofile(filepath,format=format)
Beispiel #2
0
def fields2vtkfile(fields,
                   filename,
                   mesh=None,
                   only_dofname=None,
                   format="binary",
                   path=None,
                   extrafields=None,
                   header='Header (unused)'):
    """ def fields2vtkfile( fields, filename, mesh = None,
                            only_dofname=None, format="ascii",path=None):
        Given a list of FIELDS (or just one field), a FILENAME and, optionally
        a MESH and some other values, this function goes through all degrees of
        freedoms in the field list and writes their data to the vtk file with name
        FILENAME.

        Currently, scalar and vector data on 2d and 3d meshes is supported.

        Optional extra arguments are:
            format      : 'ascii' or 'binary' (how the data is stored in the vtk file)
            only_dofname: if a name for a degree of freedom (dof) is given here, then only
                          the data for this dof is written to the vtk file.
            mesh        : if not provided, the default mesh in nfem2 is chosen. If there is
                          no default mesh defined, then a mesh needs to be provided here,
                          or the function will report an error.
            path        : if given, the file will be written no PATH/FILENAME.
                          if path is not specified, the file will be written to the
                          nfem default directory (nbase.run_dir)

            extrafields : if given, this needs to be a tuple of a fieldname and the data
                          in Python lists. Example usage: providing the maxangle data
    """

    filepath = nsim.snippets.output_file_location(filename, path)

    log.debug("Entering fields2vtkfile to write %s" % filepath)
    #check that fields are of the right type

    if type(fields) != type([]):
        fieldlist = [fields]
    else:
        fieldlist = fields
    if len(fieldlist) == 0:
        raise NfemValueError, "No field received"
    import ocaml
    if ocaml.sys_ocamlpill_type(fieldlist[0]) != 'FEM Field':
        raise NfemValueError, "Expect FEM Fields as input"

    #get the mesh (should check the type here, really.)
    if mesh == None:
        mesh = get_default_mesh()
    meshinfo = mesh.tolists()
    points = meshinfo[0][2]
    log.debug("Number of points in mesh=%d" % len(points))
    tmp = meshinfo[2][2]
    simplices = map(lambda a: a[0], tmp)
    log.debug("Number of simplices in mesh=%d" % len(simplices))

    #check dimensionality of the mesh
    if len(points[0]) == 2:
        log.debug("mesh is 2d")
        mesh_dim = 2
    elif len(points[0]) == 3:
        log.debug("mesh is 3d")
        mesh_dim = 3
    else:
        log.debug("mesh is 1d")
        mesh_dim = 1
        log.warn("1d data is new -- may be buggy (fangohr 26/10/2006)")

    #assume that 'fieldlist' is a list of FEM fields.
    #First, create the following data structure.
    #dof_to_process = [field, dof, dim]
    dof_to_process = []

    for field in fieldlist:
        #learn about dof in this field:
        content = nfem.data_doftypes(field)
        for dof in content:
            name, maxind = dof
            if only_dofname:
                if name != only_dofname:
                    continue

            if len(maxind) > 1:
                raise NfemValueError, "Can only deal with scalar and vector data at the moment (no tensor data)."
            if len(maxind) == 0:
                dim = 1  #this is a scalar
            elif len(maxind) == 1:
                if maxind[0] == 2:  #this is a 2d vector
                    dim = 2
                elif maxind[0] == 3:  #this is a 3d vector
                    dim = 3
                else:
                    raise NfemValueError, "Can only process vectors in 2 and 3 dimensions"
            dof_to_process.append((field, name, dim))

            log.debug("adding %s" % str(dof_to_process[-1]))

    #pyvtk requires us to create a vtk object and to give it the first
    #dof at that time. For all subsequent dofs, we can add them one by one.

    #check that we have some dofs to process:
    if len(dof_to_process) == 0:
        raise NfemUserError, "Have not found any degrees of freedom (Maybe you have used only_dofname with a wrong name?)"

    names = str(map(lambda a: a[1], dof_to_process))
    log.info("About to write field(s) '%s' to %s (%s)" %
             (names, filename, filepath))

    #create vtk object with first data set
    field, name, dim = dof_to_process[0]
    data = _field2meshdata(len(points), field, name, dim)
    vtk = _vtk_createVtkData(points, simplices, data, name, header)
    log.log(15, "Adding %d-dim field %s to %s" % (dim, name, filename))

    #and then process all others
    for dof in dof_to_process[1:]:
        field, name, dim = dof
        if only_dofname:
            if name != only_dofname:
                continue
        log.debug("Adding %d-dim dof %s to %s" % (dim, name, filename))
        data = _field2meshdata(len(points), field, name, dim)
        vtk = _vtk_addPointdata(vtk, data, name)

    #and process all extra fields (not nfem fields)

    if extrafields:
        for name, data in extrafields:
            if only_dofname:
                if name != only_dofname:
                    continue
            log.log(15, "Adding data %s to %s" % (name, filename))

            #from IPython.Shell import IPShellEmbed
            #ipshell = IPShellEmbed()
            #ipshell()

            vtk = _vtk_addPointdata(vtk, data, name)

    vtk.tofile(filepath, format=format)
Beispiel #3
0
             su.conversion_factor_of(SI(1, "K")).value * T3))

    xs, data = sample(sundialsbuffer_final, "T")

    for x, d in map(None, xs, data):
        f2.write('%g\t%g\n' % (x, d))

# Turn off electric heating:
ocaml.linalg_machine_set_iparam(lam, "Phi_ext", 0.0)

print "Done With Heating!"

print "Known fieldnames are", master_mwes_and_fields_by_name.keys()
fields = map(lambda a: a[0], master_mwes_and_fields_by_name.values())
for field in fields:
    print "known fields are", ocaml.sys_ocamlpill_type(field)

for i in range(last_i + 1, int(cooling_time / time_step) + last_i + 2):
    ocaml.raw_cvode_advance(cvode, sundialsbuffer_final, i * time_step, max_it)

    # Copying back data master_mwes_and_fields_by_name:

    for name in master_mwes_and_fields_by_name.keys():
        (mwe, field) = master_mwes_and_fields_by_name[name]
        ocaml.linalg_machine_get_field(lam, field, "v_%s" % name)

    fields = map(lambda a: a[1], master_mwes_and_fields_by_name.values())
    #nfem.visual.fields2vtkfile(fields,'H-%02d.vtk' % i,my_mesh,format='binary')

    xs, data = sample(sundialsbuffer_final, "T")
    cut.append(data)
Beispiel #4
0
    T0 = ocaml.probe_field(sundialsbuffer_final,"T",[0.0, 0.0, 0.0])[0][1]
    T1 = ocaml.probe_field(sundialsbuffer_final,"T",[-499.0, 0.0, 0.0])[0][1]
    T2 = ocaml.probe_field(sundialsbuffer_final,"T",[-25.0, 10.0, 10.0])[0][1]
    print "i: %3d t=%g T: %s (su), T: %s " % (i,i*time_step,repr(T0),su.conversion_factor_of(SI(1,"K"))*T0)
    f.write("%g\t%g\t%g\t%g\n" % (i*time_step*su.conversion_factor_of(SI(1,'s')).value,su.conversion_factor_of(SI(1,"K")).value*T0,su.conversion_factor_of(SI(1,"K")).value*T1,su.conversion_factor_of(SI(1,"K")).value*T2))

# Turn off electric heating:
ocaml.linalg_machine_set_iparam(lam,"Phi_ext",0.0)

print "Done With Heating!"

print "Known fieldnames are",master_mwes_and_fields_by_name.keys()
fields = map( lambda a: a[0],master_mwes_and_fields_by_name.values())
for field in fields:
    print "known fields are",ocaml.sys_ocamlpill_type(field)




for i in range(last_i+1,int(cooling_time/time_step)+last_i+2):
    ocaml.raw_cvode_advance(cvode,sundialsbuffer_final,i*time_step,max_it)

    # Copying back data master_mwes_and_fields_by_name:

    for name in master_mwes_and_fields_by_name.keys():
        (mwe,field)=master_mwes_and_fields_by_name[name]
        ocaml.linalg_machine_get_field(lam,field,"v_%s" % name)
    
    fields = map( lambda a: a[1],master_mwes_and_fields_by_name.values())
    nfem.visual.fields2vtkfile(fields,'H-%02d.vtk' % i,my_mesh,format='binary')
Beispiel #5
0
def field2numpy(field, only_dofname=None):
    """ def field2numpy( field, only_dofname=None)

    Returns a list of tuples. One tuple is returned for every degree of freedom of the field.

    The first entry is the name of the degree of freedom.

    The second entry is a numerix array containing the data (in site order).
    """

    import numpy as numerix

    def _field2numerix(field, name, dim):

        nr_points = data_length(field) / dim
        log.debug("Creating Matrix with size=(%d,%d)" % (nr_points, dim))
        data = numerix.zeros((nr_points, dim), 'f')

        def cb(i, dofname_index, site, pos, value):

            if len(site) > 1:
                log.critical(
                    "Problem -- Have only tested this for 1st order basis functions. Gut feeling is that we only want to export first order data to visualisation but this needs further thought. Order appears to be %d"
                    % (len(site)))
                raise NotImplementedError, "This has only been implemented for first order meshes"

            dofname, index = dofname_index

            if dofname == name:
                if dim == 1:
                    data[site[0]][0] = value
                elif dim in [2, 3]:
                    data[site[0], index[0]] = value
                else:
                    log.critical(
                        "Problem -- data seems not 1, not 2d, not3d. What's up? dim=%d"
                        % dim)
                    raise NfemUserError, "Shouldn't happen (4d-data?5d-data?...)"
            else:
                pass

        field_entry_wise(field, cb)

        return data

    if ocaml.sys_ocamlpill_type(field) != 'FEM Field':
        raise NfemValueError, "Expect FEM Fields as input"

    #First, create the following data structure.
    #dof_to_process = [field, dof, dim]
    dof_to_process = []

    #learn about dof in this field:
    content = data_doftypes(field)

    if len(content) > 1:
        raise NotImplementedError, "This has never been tested with more than one degree of freedom per field"
    for dof in content:
        name, maxind = dof
        if only_dofname:
            if name != only_dofname:
                continue

        if len(maxind) > 1:
            raise NfemValueError, "Can only deal with scalar and vector data at the moment (no tensor data)."
        if len(maxind) == 0:
            dim = 1  #this is a scalar
        elif len(maxind) == 1:
            if maxind[0] == 2:  #this is a 2d vector
                dim = 2
            elif maxind[0] == 3:  #this is a 3d vector
                dim = 3
            else:
                raise NfemValueError, "Can only process vectors in 2 and 3 dimensions"
        dof_to_process.append((field, name, dim))

        log.debug("adding %s" % str(dof_to_process[-1]))

    #pyvtk requires us to create a vtk object and to give it the first
    #dof at that time. For all subsequent dofs, we can add them one by one.

    #check that we have some dofs to process:
    if len(dof_to_process) == 0:
        raise NfemUserError, "Have not found any degrees of freedom (Maybe you have used only_dofname with a wrong name?)"

    names = str(map(lambda a: a[1], dof_to_process))
    log.log(15, "About to sample field(s) '%s' " % (names))

    results = []
    for dof in dof_to_process:
        field, name, dim = dof
        if only_dofname == 1:
            name = only_dofname
        log.log(15, "Adding %d-dim dof %s to return data list" % (dim, name))
        results.append((name, _field2numerix(field, name, dim)))

    return results
Beispiel #6
0
def field2numpy(field, only_dofname=None):
    """ def field2numpy( field, only_dofname=None)

    Returns a list of tuples. One tuple is returned for every degree of freedom of the field.

    The first entry is the name of the degree of freedom.

    The second entry is a numerix array containing the data (in site order).
    """

    import numpy as numerix

    def _field2numerix(field, name, dim):

        nr_points = data_length(field) / dim
        log.debug("Creating Matrix with size=(%d,%d)" % (nr_points, dim))
        data = numerix.zeros((nr_points, dim), "f")

        def cb(i, dofname_index, site, pos, value):

            if len(site) > 1:
                log.critical(
                    "Problem -- Have only tested this for 1st order basis functions. Gut feeling is that we only want to export first order data to visualisation but this needs further thought. Order appears to be %d"
                    % (len(site))
                )
                raise NotImplementedError, "This has only been implemented for first order meshes"

            dofname, index = dofname_index

            if dofname == name:
                if dim == 1:
                    data[site[0]][0] = value
                elif dim in [2, 3]:
                    data[site[0], index[0]] = value
                else:
                    log.critical("Problem -- data seems not 1, not 2d, not3d. What's up? dim=%d" % dim)
                    raise NfemUserError, "Shouldn't happen (4d-data?5d-data?...)"
            else:
                pass

        field_entry_wise(field, cb)

        return data

    if ocaml.sys_ocamlpill_type(field) != "FEM Field":
        raise NfemValueError, "Expect FEM Fields as input"

    # First, create the following data structure.
    # dof_to_process = [field, dof, dim]
    dof_to_process = []

    # learn about dof in this field:
    content = data_doftypes(field)

    if len(content) > 1:
        raise NotImplementedError, "This has never been tested with more than one degree of freedom per field"
    for dof in content:
        name, maxind = dof
        if only_dofname:
            if name != only_dofname:
                continue

        if len(maxind) > 1:
            raise NfemValueError, "Can only deal with scalar and vector data at the moment (no tensor data)."
        if len(maxind) == 0:
            dim = 1  # this is a scalar
        elif len(maxind) == 1:
            if maxind[0] == 2:  # this is a 2d vector
                dim = 2
            elif maxind[0] == 3:  # this is a 3d vector
                dim = 3
            else:
                raise NfemValueError, "Can only process vectors in 2 and 3 dimensions"
        dof_to_process.append((field, name, dim))

        log.debug("adding %s" % str(dof_to_process[-1]))

    # pyvtk requires us to create a vtk object and to give it the first
    # dof at that time. For all subsequent dofs, we can add them one by one.

    # check that we have some dofs to process:
    if len(dof_to_process) == 0:
        raise NfemUserError, "Have not found any degrees of freedom (Maybe you have used only_dofname with a wrong name?)"

    names = str(map(lambda a: a[1], dof_to_process))
    log.log(15, "About to sample field(s) '%s' " % (names))

    results = []
    for dof in dof_to_process:
        field, name, dim = dof
        if only_dofname == 1:
            name = only_dofname
        log.log(15, "Adding %d-dim dof %s to return data list" % (dim, name))
        results.append((name, _field2numerix(field, name, dim)))

    return results