def exportPetscToAscii(nReceivers, xComponentFile,
                       yComponentFile, zComponentFile, out_file):
    ''' Export a saved electric fields in petsc format to Ascii format.

    :param int nReceivers: total number of receivers
    :param str xComponentFile: petsc file name to be readed and exported
    :param str yComponentFile: petsc file name to be readed and exported
    :param str zComponentFile: petsc file name to be readed and exported
    :param str out_file: out file name for electric fields
    :return: electric field in numpy format
    :rtype: ndarray
    '''
    # Build indexes list of receivers
    indx_receivers = np.arange(0, nReceivers, dtype=np.complex)
    # Allocate space for Electric fields
    dataAscii = np.zeros((nReceivers, 4), dtype=np.complex)
    dataMatlab = np.zeros((nReceivers, 3), dtype=np.complex)
    # Read field
    tmp = readPetscVector(xComponentFile, communicator=PETSc.COMM_SELF)
    dataX = tmp.getArray()
    tmp.destroy()
    tmp = readPetscVector(yComponentFile, communicator=PETSc.COMM_SELF)
    dataY = tmp.getArray()
    tmp.destroy()
    tmp = readPetscVector(zComponentFile, communicator=PETSc.COMM_SELF)
    dataZ = tmp.getArray()
    tmp.destroy()

    for iRecv in np.arange(nReceivers):
        dataAscii[iRecv, 0] = iRecv
        dataAscii[iRecv, 1] = dataX[iRecv]
        dataAscii[iRecv, 2] = dataY[iRecv]
        dataAscii[iRecv, 3] = dataZ[iRecv]
        # Set data for matlab matrix
        dataMatlab[iRecv, 0] = dataX[iRecv]
        dataMatlab[iRecv, 1] = dataY[iRecv]
        dataMatlab[iRecv, 2] = dataZ[iRecv]

    dataAscii = dataAscii.view(float)
    dataAscii = np.delete(dataAscii, 1, axis=1)

    file_header = ('Receiver\t\t\tX-component\t\t\t\t\t' +
                   'Y-component\t\t\t\t\tZ-component')
    file_footer = ('--------   -------------------------' +
                   '----------------------    ----------' +
                   '------------------------------------' +
                   '    ----------------------------------------------')
    table_format = ('    %i \t     %4.16e%+4.16ej' +
                    ' \t%4.16e%+4.16ej \t  %4.16e%+4.16ej')
    np.savetxt(out_file, dataAscii, fmt=table_format,
               header=file_header, footer=file_footer)

    # Insert time stamp to file
    with open(out_file, 'a') as outf:
        fmt = '%Y/%m/%d %H:%M:%S'
        TT = datetime.datetime.now().strftime(fmt)
        outf.write('PETGEM execution on: ' + TT)

    return dataMatlab
Esempio n. 2
0
    printMessage('  Nodes coordinates', rank)
    nodes = readPetscMatrix(modelling['NODES_FILE'], communicator=None)
    # elements-nodes connectivity
    printMessage('  Elements-nodes connectivity', rank)
    elemsN = readPetscMatrix(modelling['MESH_CONNECTIVITY_FILE'],
                             communicator=None)
    # elements-edges connectivity
    printMessage('  Elements-edges connectivity', rank)
    elemsE = readPetscMatrix(modelling['DOFS_CONNECTIVITY_FILE'],
                             communicator=None)
    # edgesN connectivity
    printMessage('  Edges-nodes connectivity', rank)
    edgesN = readPetscMatrix(modelling['DOFS_NODES_FILE'], communicator=None)
    # Boundary-Edges
    printMessage('  Boundary-Edges', rank)
    bEdges = readPetscVector(modelling['BOUNDARIES_FILE'], communicator=None)
    # Sparsity pattern (NNZ) for matrix allocation
    printMessage('  Vector for matrix allocation', rank)
    Q = readPetscVector(modelling['NNZ_FILE'], communicator=None)
    nnz = (Q.getArray().real).astype(PETSc.IntType)
    # Conductivity model
    printMessage('  Conductivity model', rank)
    elemsSigma = readPetscVector(modelling['CONDUCTIVITY_MODEL_FILE'],
                                 communicator=None)
    # Receivers data
    printMessage('  Receivers data', rank)
    receivers = readPetscMatrix(modelling['RECEIVERS_FILE'], communicator=None)
    # End log event for importing task
    importLog.end()

    # ###############################################