Beispiel #1
0
void interpolate(const Mesh & mesh, Mesh & qmesh, bool verbose){
    RMatrix cellData;
    RMatrix nodeData;
    std::vector< std::string > cellDataNames;
    std::vector< std::string > nodeDataNames;

    for (std::map< std::string, RVector >::const_iterator it = mesh.exportDataMap().begin();
          it != mesh.exportDataMap().end(); it ++){

        if (it->second.size() == mesh.nodeCount()){
            if (verbose) std::cout << " interpolate node data: " << it->first << std::endl;
            nodeData.push_back(it->second);
            nodeDataNames.push_back(it->first);
        } else if (it->second.size() == mesh.cellCount()){
            if (verbose) std::cout << " interpolate cell data: " << it->first << std::endl;
            cellData.push_back(it->second);
            cellDataNames.push_back(it->first);
        } else {
            if (verbose) std::cout << " omit unknown data: " << it->first << " " <<
                it->second.size()<< std::endl;
        }
    }
    
    if (cellData.rows() > 0){
        RMatrix qCellData;
        interpolate(mesh, cellData, qmesh.cellCenter(), qCellData, verbose) ;
        for (uint i= 0; i < cellData.rows(); i ++){
            qmesh.addExportData(cellDataNames[i], qCellData[i]);
        }
    }
    if (nodeData.rows() > 0){
        RMatrix qNodeData;
        interpolate(mesh, nodeData, qmesh.positions(), qNodeData, verbose) ;
        for (uint i= 0; i < nodeData.rows(); i ++){
            qmesh.addExportData(nodeDataNames[i], qNodeData[i]);
        }
    }
}
void HarmonicModelling::createJacobian( const RVector & model ) {
    //!! jacobian = transpose( A );
    RMatrix * jacobian = dynamic_cast < RMatrix * > ( jacobian_ );
    
    if ( jacobian->rows() != nt_ || jacobian->cols() != np_ ) {
        jacobian->resize( nt_, np_ );

        for ( size_t i = 0 ; i < np_ ; i++ ){
            for ( size_t j = 0 ; j < nt_ ; j++ ){
                (*jacobian)[ j ][ i ] = A_[ i ][ j ];
            }
        }
    }
}
int main( int argc, char *argv [] ){

    bool lambdaOpt = false, isRobust = false, isBlocky = false, doResolution = false;
    bool useAppPar = false, useTan = false, useWater = false, optimizeChi1 = false;
    double lambda = 1, lbound = 0, ubound = 0, errbabs = 20;
    int maxIter = 10, verboseCount = 0, ctype = 0;
    std::string matFile( "A.mat" ), bFile( "b.vec" );

    OptionMap oMap;
    oMap.setDescription("Description. InvLinearMat - Linear Inversion with given matrix and vector\n");
    oMap.addLastArg( bFile, "Data file" );
    oMap.add( verboseCount, "v" , "verbose", "Verbose/debug/dosave mode (multiple use)." );
    oMap.add( lambdaOpt,    "O" , "OptimizeLambda", "Optimize model smoothness using L-curve." );
    oMap.add( optimizeChi1, "C" , "OptimizeChi1", "Optimize lambda subject to chi^2=1." );
    oMap.add( doResolution, "D" , "doResolution", "Do resolution analysis." );
    oMap.add( isRobust,     "R" , "RobustData", "Robust (L1) data weighting." );
    oMap.add( isBlocky,     "B" , "BlockyModel", "Blocky (L1) model constraints." );
    oMap.add( useTan,       "T" , "useTan", "Use (co-)Tan instead of Log for LU." );
    oMap.add( useAppPar,    "A" , "useAppPar", "Use apparent parameter transformation." );
    oMap.add( useWater,     "W" , "useWater", "Use water content transformation." );
    oMap.add( matFile,      "m:", "matFile", "Matrix file [A.mat]" );
    oMap.add( lambda,       "l:", "lambda", "Regularization strength lambda[100]." );
    oMap.add( lbound,       "b:", "lbound", "Lower parameter bound[0]" );
    oMap.add( ubound,       "u:", "ubound", "Upper parameter bound[0-inactive]" );
    oMap.add( errbabs,      "e:", "error", "Absolute error level" );
    oMap.add( maxIter,      "i:", "maxIter", "Maximum Iteration number" );
    oMap.parse( argc, argv );
    bool verbose = ( verboseCount > 0 ), debug = ( verboseCount > 1 ), dosave = ( verboseCount > 2 );

    RMatrix A;
    if ( ! loadMatrixSingleBin( A, matFile ) ) { std::cerr << "Did not find A.mat!" << std::endl; return EXIT_OPEN_FILE; }
    RVector b; load( b, bFile );
    size_t nModel( A.cols() );
    dcout << "size(A) = " << A.rows() << "x" << nModel << "size(b) = " << b.size() << std::endl;

    RVector Asum( A * RVector( nModel, 1.0 ) );
    RVector xapp( b / Asum );
    DEBUG save( xapp, "xapp.vec" );
    dcout << "apparent x: min/max = " << min( xapp ) << "/" << max( xapp ) << std::endl;

    Mesh mesh( createMesh1D( nModel ) );
    DEBUG mesh.save( "mesh1d.bms" );
    mesh.showInfos();
    for ( size_t i = 0; i < mesh.cellCount(); i ++ ) mesh.cell( i ).setAttribute( 2.0 + i );
    mesh.createNeighbourInfos();

    LinearModelling f( mesh, A, verbose );
    f.regionManager().region( 0 )->setConstraintType( ctype );

    Trans < RVector > * transData;
    Trans < RVector > * transModel;
    if ( useAppPar ) {
        if ( useTan ) {
            if ( ubound <= lbound ) ubound = lbound + 1.0;
            transData = new TransNest< RVector >( *new TransCotLU< RVector >( lbound, ubound ),
                                                  *new TransLinear< RVector >( RVector( xapp / b ) ) );
            transModel = new TransCotLU< RVector >( lbound, ubound );
        } else {
            transData = new TransNest< RVector >( *new TransLogLU< RVector >( lbound, ubound ),
                                                  *new TransLinear< RVector >( RVector( xapp / b ) ) );
            transModel = new TransLogLU< RVector >( lbound, ubound );
        }
    } else {
        transData = new Trans< RVector >( );
        transModel = new Trans< RVector >( );
    }
    /*! set up inversion */
    RInversion inv( b, f, *transData, *transModel, verbose, dosave );

    inv.setRecalcJacobian( false ); //! no need since it is linear
    inv.setLambda( lambda );
    inv.setOptimizeLambda( lambdaOpt );
    inv.setRobustData( isRobust );
    inv.setBlockyModel( isBlocky );
    inv.setMaxIter( maxIter );
    inv.setDeltaPhiAbortPercent( 1.0 );
    RVector error( errbabs / b);
    vcout << "error: min/max = " << min( error ) << "/" << max( error ) << std::endl;
    inv.setError( error );

    RVector x( nModel, mean( xapp ) );
    inv.setModel( x );
    inv.setReferenceModel( x );

    if ( optimizeChi1 ) { x = inv.runChi1(); } else { x = inv.run(); }
    vcout << "x = " << x << std::endl;
    save( x, "x.vec" );

    if ( doResolution ) {
        RVector resolution( nModel );
        RVector resMDiag ( nModel );
        RMatrix resM;
        for ( size_t iModel = 0; iModel < nModel; iModel++ ) {
            resolution = inv.modelCellResolution( iModel );
            resM.push_back( resolution );
            resMDiag[ iModel ] = resolution[ iModel ];
        }
        save( resMDiag, "resMDiag.vec" );
        save( resM, "resolution.matrix" );
    }

    delete transData;
    delete transModel;

    return EXIT_SUCCESS;
}
Beispiel #4
0
void interpolate(const Mesh & mesh, const RMatrix & vData,
                 const R3Vector & ipos, RMatrix & iData,
                 bool verbose){

    R3Vector pos(ipos);
    
    if (mesh.dim() == 2){
        if ((zVari(pos) || max(abs(z(pos))) > 0.) && 
            (!yVari(pos) && max(abs(y(pos))) < 1e-8)) {
            if (verbose) 
                std::cout << "Warning! swap YZ coordinates for query "
                            "positions to meet mesh dimensions." << std::endl;
            swapYZ(pos);
        }
    }
        
    if (iData.rows() != vData.rows()){
        iData.resize(vData.rows(), pos.size());
    }

    std::vector < Cell * > cells(pos.size());
    size_t count = 0;
    
    Cell * c = 0;
    for (uint i = 0; i < pos.size(); i ++) {
        
        c = mesh.findCell(pos[i], count, false);
//         if (!c) {__MS(pos[i])
//             c = mesh.findCell(pos[i], count, true);
//             if (!c) exit(0);
//         }
        cells[i] = c;
        if (verbose) std::cout << "\r" << i + 1 << " \t/ " << pos.size();
//                             << "\t searched: " << count << std::endl;
    }
    if (verbose) std::cout << std::endl;

    for (uint i = 0; i < vData.rows(); i ++) {
        if (verbose) std::cout << "\r" << i + 1 << " \t/ " << vData.rows();
        
        RVector data;
        
        if (vData[i].size() != 0){

            if (vData[i].size() == mesh.nodeCount()){
                data = vData[i];
            } else if (vData[i].size() == mesh.cellCount()){
                data = cellDataToPointData(mesh, vData[i]);
            } else {
                throwLengthError(EXIT_VECTOR_SIZE_INVALID,
                                 WHERE_AM_I + 
                                 " data.size not nodeCount and cellCount " + 
                                 toStr(vData[i].size()) + " != " + 
                                 toStr(mesh.nodeCount()) + " != " + 
                                 toStr(mesh.cellCount()));
            }

            for (uint j = 0; j < pos.size(); j ++) {
                if (cells[j]){
                    iData[i][j] = cells[j]->pot(pos[j], data);
                    
//              this check is obsolete if the findCell (from above) is working properly                     
//                     if (cells[j]->shape().isInside(pos[j])){
//                         iData[i][j] = cells[j]->pot(pos[j], data);
//                     } else {
//                         std::cout << WHERE_AM_I << " here is somethng wrong" << std::endl;
//                     }

//                    std::cout << j << " " << iData[i][j] << std::endl;
                    //** return cell data
//                    iData[i][j] = vData[i][cells[j]->id()];
                } else {        
                    iData[i][j] = 0.0;
                    //std::cout << "Cant find cell for " << pos[j]<< std::endl;
//                     for (uint i = 0; i < mesh.cellCount(); i ++){
//                     	if (mesh.cell(i).shape().isInside(pos[j], true)){
//                         	std::cout << mesh.cell(i) << std::endl;
//                         }
//                     }
//                     exit(0);
                }
            }
        } // if vData.size() != 0
    }  // for each in vData
    if (verbose) std::cout << std::endl;
}