int GaussSeidelPoisson1Drb(Vector u, double tol, int maxit) { int it=0, i, j; double max = tol+1; Vector b = cloneVector(u); Vector r = cloneVector(u); Vector v = cloneVector(u); copyVector(b, u); fillVector(u, 0.0); while (max > tol && ++it < maxit) { copyVector(v, u); copyVector(u, b); collectVector(v); for (j=0;j<2;++j) { #pragma omp parallel for schedule(static) for (i=1+j;i<r->len-1;i+=2) { u->data[i] += v->data[i-1]; u->data[i] += v->data[i+1]; r->data[i] = u->data[i]-(2.0+alpha)*v->data[i]; u->data[i] /= (2.0+alpha); v->data[i] = u->data[i]; } if (j == 0) collectVector(v); } max = maxNorm(r); } freeVector(b); freeVector(r); freeVector(v); return it; }
int GaussJacobiPoisson1D(Vector u, double tol, int maxit) { int it=0, i; Vector b = cloneVector(u); Vector e = cloneVector(u); copyVector(b, u); fillVector(u, 0.0); double max = tol+1; while (max > tol && ++it < maxit) { copyVector(e, u); collectVector(e); copyVector(u, b); #pragma omp parallel for schedule(static) for (i=1;i<e->len-1;++i) { u->data[i] += e->data[i-1]; u->data[i] += e->data[i+1]; u->data[i] /= (2.0+alpha); } axpy(e, u, -1.0); e->data[0] = e->data[e->len-1] = 0.0; max = maxNorm(e); } freeVector(b); freeVector(e); return it; }
AUD_Error gmm_clone( void **phGmmHandle, void *hSrcGmmHandle, AUD_Int8s *pName ) { AUD_ASSERT( phGmmHandle && hSrcGmmHandle ); AUD_ASSERT( *phGmmHandle == NULL ); AUD_Int32s ret = 0; GmmModel *pState = NULL; pState = (GmmModel*)calloc( sizeof(GmmModel), 1 ); if ( pState == NULL ) { *phGmmHandle = NULL; return AUD_ERROR_OUTOFMEMORY; } GmmModel *pSrcModel = (GmmModel*)hSrcGmmHandle; if ( pName == NULL ) { snprintf( (char*)pState->arGmmName, MAX_GMMNAME_LENGTH, "%s", (char*)pSrcModel->arGmmName ); } else { snprintf( (char*)pState->arGmmName, MAX_GMMNAME_LENGTH, "%s", (char*)pName ); } pState->numMix = pSrcModel->numMix; pState->width = pSrcModel->width; pState->step = pState->width; pState->means.rows = pState->numMix; pState->means.cols = pState->step; pState->means.dataType = AUD_DATATYPE_INT32S; ret = createMatrix( &(pState->means) ); AUD_ASSERT( ret == 0 ); ret = cloneMatrix( &(pState->means), &(pSrcModel->means) ); AUD_ASSERT( ret == 0 ); pState->cvars.rows = pState->numMix; pState->cvars.cols = pState->step; pState->cvars.dataType = AUD_DATATYPE_INT64S; ret = createMatrix( &(pState->cvars) ); AUD_ASSERT( ret == 0 ); ret = cloneMatrix( &(pState->cvars), &(pSrcModel->cvars) ); AUD_ASSERT( ret == 0 ); pState->weights.len = pState->numMix; pState->weights.dataType = AUD_DATATYPE_DOUBLE; ret = createVector( &(pState->weights) ); AUD_ASSERT( ret == 0 ); ret = cloneVector( &(pState->weights), &(pSrcModel->weights) ); AUD_ASSERT( ret == 0 ); pState->dets.len = pState->numMix; pState->dets.dataType = AUD_DATATYPE_DOUBLE; ret = createVector( &(pState->dets) ); AUD_ASSERT( ret == 0 ); ret = cloneVector( &(pState->dets), &(pSrcModel->dets) ); AUD_ASSERT( ret == 0 ); *phGmmHandle = pState; return AUD_ERROR_NONE; }
AUD_Error gmm_init( void **phGmmHandle, AUD_Int8s *pModelName, AUD_Int32s numMix, AUD_Int32s featDim, AUD_Vector *pWeights, AUD_Matrix *pMeans, AUD_Matrix *pCvars ) { AUD_ASSERT( phGmmHandle && pModelName && pWeights && pMeans && pCvars ); AUD_ASSERT( *phGmmHandle == NULL ); AUDLOG( "init model: %s\n", pModelName ); AUD_Int32s ret = 0; GmmModel *pState = NULL; pState = (GmmModel*)calloc( sizeof(GmmModel), 1 ); if ( pState == NULL ) { *phGmmHandle = NULL; return AUD_ERROR_OUTOFMEMORY; } strncpy( (char*)pState->arGmmName, (char*)pModelName, MAX_GMMNAME_LENGTH - 1 ); pState->arGmmName[strlen( (char*)pModelName )] = '\0'; pState->numMix = numMix; pState->width = featDim; pState->step = pState->width; pState->means.rows = pState->numMix; pState->means.cols = pState->step; pState->means.dataType = AUD_DATATYPE_INT32S; ret = createMatrix( &(pState->means) ); AUD_ASSERT( ret == 0 ); if ( pMeans != NULL ) { ret = cloneMatrix( &(pState->means), pMeans ); AUD_ASSERT( ret == 0 ); } pState->cvars.rows = pState->numMix; pState->cvars.cols = pState->step; pState->cvars.dataType = AUD_DATATYPE_INT64S; ret = createMatrix( &(pState->cvars) ); AUD_ASSERT( ret == 0 ); if ( pCvars != NULL ) { ret = cloneMatrix( &(pState->cvars), pCvars ); AUD_ASSERT( ret == 0 ); } pState->weights.len = pState->numMix; pState->weights.dataType = AUD_DATATYPE_DOUBLE; ret = createVector( &(pState->weights) ); AUD_ASSERT( ret == 0 ); if ( pWeights != NULL ) { ret = cloneVector( &(pState->weights), pWeights ); AUD_ASSERT( ret == 0 ); } pState->dets.len = pState->numMix; pState->dets.dataType = AUD_DATATYPE_DOUBLE; ret = createVector( &(pState->dets) ); AUD_ASSERT( ret == 0 ); calcdet( &(pState->cvars), &(pState->weights), &(pState->dets) ); *phGmmHandle = pState; return AUD_ERROR_NONE; }
// ============================================================================= int main ( int argc, char *argv[] ) { // Initialize MPI #ifdef HAVE_MPI MPI_Init ( &argc,&argv ); #endif // create Epetra communicator #ifdef HAVE_MPI Teuchos::RCP<Epetra_MpiComm> eComm = Teuchos::rcp<Epetra_MpiComm> ( new Epetra_MpiComm ( MPI_COMM_WORLD ) ); #else Teuchos::RCP<Epetra_SerialComm> eComm = Teuchos::rcp<Epetra_SerialComm> ( new Epetra_SerialComm() ); #endif int status; try { // Create a communicator for Tpetra objects const Teuchos::RCP<const Teuchos::Comm<int> > Comm = Teuchos::DefaultComm<int>::getComm(); // ========================================================================= // handle command line arguments Teuchos::CommandLineProcessor My_CLP; My_CLP.setDocString ( "This program does continuation for the Ginzburg--Landau problem with a LOCA interface.\n" "It is possible to give an initial guess in VTK format on the command line.\n" ); std::string xmlInputFileName = ""; My_CLP.setOption ( "xml-input-file", &xmlInputFileName, "XML file containing the parameter list", true ); // print warning for unrecognized arguments My_CLP.recogniseAllOptions ( true ); // don't throw exceptions My_CLP.throwExceptions ( false ); // finally, parse the stuff! Teuchos::CommandLineProcessor::EParseCommandLineReturn parseReturn; parseReturn = My_CLP.parse ( argc, argv ); Teuchos::RCP<Teuchos::ParameterList> paramList = Teuchos::rcp ( new Teuchos::ParameterList ); std::cout << "Reading parameter list from \"" << xmlInputFileName << "\"." << std::endl; Teuchos::updateParametersFromXmlFile ( xmlInputFileName, paramList.get() ); // ========================================================================= // extract data for the output the parameter list Teuchos::ParameterList outputList; outputList = paramList->sublist ( "Output", true ); // set default directory to be the directory of the XML file itself std::string xmlPath = boost::filesystem::path ( xmlInputFileName ).branch_path().string(); boost::filesystem::path outputDirectory = outputList.get<string> ( "Output directory" ); if ( outputDirectory.root_directory().empty() ) // outputDirectory is empty or is a relative directory. outputDirectory = xmlPath / outputDirectory; std::string contFileBaseName = outputList.get<string> ( "Continuation file base name" ); std::string outputFormat = outputList.get<string> ( "Output format" ); std::string contDataFileName = outputList.get<string> ( "Continuation data file name" ); // ========================================================================= // --------------------------------------------------------------------------- Teuchos::ParameterList glParameters; Teuchos::RCP<ComplexVector> psi; Teuchos::RCP<Recti::Grid::Uniform> grid; Teuchos::ParameterList initialGuessList; initialGuessList = paramList->sublist ( "Initial guess", true ); boost::filesystem::path inputGuessFile = initialGuessList.get<string> ( "File name" ); if ( !inputGuessFile.empty() && inputGuessFile.root_directory().empty() ) // if inputGuessFile is a relative path inputGuessFile = xmlPath / inputGuessFile; TEUCHOS_ASSERT( !inputGuessFile.empty() ); // For technical reasons, the reader can only accept ComplexMultiVectors. Teuchos::RCP<Ginla::FDM::State> state; Recti::Grid::Reader::read ( Comm, inputGuessFile.string(), state, grid, glParameters ); // possibly overwrite the parameters Teuchos::ParameterList & overwriteParamsList = paramList->sublist ( "Overwrite parameter list", true ); bool overwriteParameters = overwriteParamsList.get<bool> ( "Overwrite parameters" ); if ( overwriteParameters ) { Teuchos::ParameterList & overwritePList = overwriteParamsList.sublist( "Parameters", true ); glParameters.setParameters( overwritePList ); // possibly update the scaling of the grid grid->updateScaling( glParameters.get<double>("scaling") ); } // --------------------------------------------------------------------------- Teuchos::RCP<Ginla::MagneticVectorPotential::Virtual> A = Teuchos::rcp ( new Ginla::MagneticVectorPotential::ZSquareSymmetric ( glParameters.get<double> ( "H0" ), glParameters.get<double> ( "scaling" ) ) ); // create the operator Teuchos::RCP<Ginla::FDM::Operator::Virtual> glOperator = Teuchos::rcp ( new Ginla::FDM::Operator::BCCentral ( grid, A, psi->getMap(), psi->getMap() ) ); // // create a perturbation // Teuchos::RCP<GL::Perturbation::Virtual> quadrantsPerturbation = // Teuchos::rcp ( new GL::Perturbation::Quadrants ( grid ) ); // TODO: why not make glOperator depend upon perturbation instead? // GinzburgLandau glProblem = GinzburgLandau ( glOperator, // quadrantsPerturbation ); std::string fn = outputDirectory.string() + "/" + contDataFileName; Teuchos::RCP<Ginla::IO::StatsWriter> statsWriter = Teuchos::rcp( new Ginla::IO::StatsWriter( fn ) ); Teuchos::RCP<Ginla::FDM::LocaSystem::Bordered> glsystem; Teuchos::ParameterList & stepperList = paramList->sublist ( "LOCA" ).sublist ( "Stepper" ); int maxLocaSteps = stepperList.get<int> ( "Max Steps" ); Teuchos::RCP<Ginla::IO::StateWriter> stateWriter = Teuchos::rcp( new Ginla::IO::StateWriter( outputDirectory.string(), contFileBaseName, outputFormat, maxLocaSteps ) ); glsystem = Teuchos::rcp ( new Ginla::FDM::LocaSystem::Bordered ( glOperator, eComm, psi->getMap(), statsWriter, stateWriter ) ); // set the initial value from glParameters std::string contParam = stepperList.get<string> ( "Continuation Parameter" ); TEST_FOR_EXCEPTION ( !glParameters.isParameter ( contParam ), std::logic_error, "Parameter \"" << contParam << "\" given as continuation parameter, but doesn't exist" << "in the glParameters list." ); // check if the initial value was given (will be unused anyway) if ( stepperList.isParameter ( "Initial Value" ) ) { std::cerr << "Warning: Parameter 'LOCA->Stepper->Initial Value' given, but will not be used." << std::endl; } // TODO Get rid of the explicit "double". stepperList.set ( "Initial Value", glParameters.get<double> ( contParam ) ); // --------------------------------------------------------------------------- // Create the necessary objects // --------------------------------------------------------------------------- // Create Epetra factory Teuchos::RCP<LOCA::Abstract::Factory> epetraFactory = Teuchos::rcp ( new LOCA::Epetra::Factory ); // Create global data object Teuchos::RCP<LOCA::GlobalData> globalData = LOCA::createGlobalData ( paramList, epetraFactory ); // --------------------------------------------------------------------------- #ifdef HAVE_LOCA_ANASAZI Teuchos::ParameterList& eigenList = paramList->sublist ( "LOCA" ).sublist ( "Stepper" ) .sublist ( "Eigensolver" ); std::string eigenvaluesFileName = outputDirectory.string() + "/" + outputList.get<string> ( "Eigenvalues file name" ); std::string eigenstateFileNameAppendix = outputList.get<string> ( "Eigenstate file name appendix" ); Teuchos::RCP<Ginla::IO::StatsWriter> eigenStatsWriter = Teuchos::rcp( new Ginla::IO::StatsWriter( eigenvaluesFileName ) ); Teuchos::RCP<Ginla::IO::SaveEigenData> glEigenSaver = Teuchos::RCP<Ginla::IO::SaveEigenData> ( new Ginla::IO::SaveEigenData ( eigenList, glsystem, stateWriter, eigenStatsWriter ) ); Teuchos::RCP<LOCA::SaveEigenData::AbstractStrategy> glSaveEigenDataStrategy = glEigenSaver; eigenList.set ( "Save Eigen Data Method", "User-Defined" ); eigenList.set ( "User-Defined Save Eigen Data Name", "glSaveEigenDataStrategy" ); eigenList.set ( "glSaveEigenDataStrategy", glSaveEigenDataStrategy ); #endif // --------------------------------------------------------------------------- // Create all possible Epetra_Operators. Teuchos::RCP<Epetra_RowMatrix> J = glsystem->getJacobian(); // Teuchos::RCP<Epetra_RowMatrix> M = glsystem->getPreconditioner(); // Create the linear system. // Use the TimeDependent interface for computation of shifted matrices. Teuchos::RCP<LOCA::Epetra::Interface::Required> iReq = glsystem; Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = glsystem; // Teuchos::RCP<NOX::Epetra::Interface::Preconditioner> iPrec = glsystem; Teuchos::ParameterList& nlPrintParams = paramList->sublist ( "NOX" ) .sublist ( "Printing" ); Teuchos::ParameterList& lsParams = paramList->sublist ( "NOX" ) .sublist ( "Direction" ) .sublist ( "Newton" ) .sublist ( "Linear Solver" ); // Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys = Teuchos::rcp( // new NOX::Epetra::LinearSystemAztecOO(nlPrintParams, lsParams, iJac, J, iPrec, M, *soln)); NOX::Epetra::Vector cloneVector( Epetra_Vector( *glsystem->getMap() ) ); Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys = Teuchos::rcp ( new NOX::Epetra::LinearSystemAztecOO ( nlPrintParams, lsParams, iReq, iJac, J, cloneVector ) ); Teuchos::RCP<LOCA::Epetra::Interface::TimeDependent> iTime = glsystem; // --------------------------------------------------------------------------- // --------------------------------------------------------------------------- // Create a group which uses that problem interface. The group will // be initialized to contain the default initial guess for the // specified problem. // translate parameters into a LOCA list LOCA::ParameterVector locaParams = *(Ginla::Helpers::teuchosParameterList2locaParameterVector( glParameters )); // get initial guess NOX::Epetra::Vector initialGuess ( glsystem->createSystemVector( *state ), NOX::Epetra::Vector::CreateView ); Teuchos::RCP<LOCA::Epetra::Group> grp = Teuchos::rcp ( new LOCA::Epetra::Group ( globalData, nlPrintParams, iTime, initialGuess, linSys, linSys, locaParams ) ); grp->setParams ( locaParams ); // --------------------------------------------------------------------------- // Set up the NOX status tests Teuchos::ParameterList& noxList = paramList->sublist ( "NOX", true ); double tol = noxList.get<double> ( "Tolerance" ); int maxNonlinarSteps = noxList.get<int> ( "Max steps" ); Teuchos::RCP<NOX::StatusTest::NormF> normF = Teuchos::rcp ( new NOX::StatusTest::NormF ( tol ) ); Teuchos::RCP<NOX::StatusTest::MaxIters> maxIters = Teuchos::rcp ( new NOX::StatusTest::MaxIters ( maxNonlinarSteps ) ); Teuchos::RCP<NOX::StatusTest::Generic> comboOR = Teuchos::rcp ( new NOX::StatusTest::Combo ( NOX::StatusTest::Combo::OR, normF, maxIters ) ); // --------------------------------------------------------------------------- // Set up the LOCA status tests Teuchos::RCP<LOCA::StatusTest::Abstract> maxLocaStepsTest = Teuchos::rcp ( new LOCA::StatusTest::MaxIters ( maxLocaSteps ) ); Teuchos::RCP<LOCA::StatusTest::Abstract> zeroEnergyTest = Teuchos::rcp ( new Ginla::StatusTest::Energy ( glsystem, 0.0 ) ); Teuchos::RCP<LOCA::StatusTest::Abstract> loopTest = Teuchos::rcp ( new Ginla::StatusTest::Loop ( glsystem ) ); Teuchos::RCP<LOCA::StatusTest::Abstract> turnaroundTest = Teuchos::rcp ( new Ginla::StatusTest::Turnaround () ); Teuchos::RCP<LOCA::StatusTest::Combo> locaCombo = Teuchos::rcp ( new LOCA::StatusTest::Combo ( LOCA::StatusTest::Combo::OR ) ); locaCombo->addStatusTest( maxLocaStepsTest ); // locaCombo->addStatusTest( zeroEnergyTest ); locaCombo->addStatusTest( loopTest ); locaCombo->addStatusTest( turnaroundTest ); // --------------------------------------------------------------------------- // Create the stepper Teuchos::RCP<LOCA::Thyra::Group> grp2 = Teuchos::null; Teuchos::RCP<LOCA::Stepper> stepper = Teuchos::rcp ( new LOCA::Stepper ( globalData, grp, locaCombo, comboOR, paramList ) ); // --------------------------------------------------------------------------- // make sure that the stepper starts off with the correct starting value // pass pointer to stepper to glsystem to be able to read stats from the stepper in there glsystem->setLocaStepper ( stepper ); #ifdef HAVE_LOCA_ANASAZI glEigenSaver->setLocaStepper ( stepper ); #endif // --------------------------------------------------------------------------- // Perform continuation run status = stepper->run(); // --------------------------------------------------------------------------- // clean up LOCA::destroyGlobalData ( globalData ); glsystem->releaseLocaStepper(); #ifdef HAVE_LOCA_ANASAZI glEigenSaver->releaseLocaStepper(); #endif } catch ( std::exception &e ) { std::cerr << e.what() << std::endl; status += 10; } catch ( char const* e ) { std::cerr << "Exception raised: " << e << std::endl; status += 20; } #ifdef HAVE_MPI MPI_Finalize(); #endif // Final return value (0 = successful, non-zero = failure) return status; }
/** * Calculates the EISF if the fit includes a Delta function * @param tableWs - The TableWorkspace to append the EISF calculation to */ void ConvolutionFitSequential::calculateEISF( API::ITableWorkspace_sptr &tableWs) { // Get height data from parameter table const auto columns = tableWs->getColumnNames(); const auto height = searchForFitParams("Height", columns).at(0); const auto heightErr = searchForFitParams("Height_Err", columns).at(0); auto heightY = tableWs->getColumn(height)->numeric_fill<>(); auto heightE = tableWs->getColumn(heightErr)->numeric_fill<>(); // Get amplitude column names const auto ampNames = searchForFitParams("Amplitude", columns); const auto ampErrorNames = searchForFitParams("Amplitude_Err", columns); // For each lorentzian, calculate EISF size_t maxSize = ampNames.size(); if (ampErrorNames.size() > maxSize) { maxSize = ampErrorNames.size(); } for (size_t i = 0; i < maxSize; i++) { // Get amplitude from column in table workspace const auto ampName = ampNames.at(i); auto ampY = tableWs->getColumn(ampName)->numeric_fill<>(); const auto ampErrorName = ampErrorNames.at(i); auto ampErr = tableWs->getColumn(ampErrorName)->numeric_fill<>(); // Calculate EISF and EISF error // total = heightY + ampY auto total = cloneVector(heightY); std::transform(total.begin(), total.end(), ampY.begin(), total.begin(), std::plus<double>()); // eisfY = heightY / total auto eisfY = cloneVector(heightY); std::transform(eisfY.begin(), eisfY.end(), total.begin(), eisfY.begin(), std::divides<double>()); // heightE squared auto heightESq = cloneVector(heightE); heightESq = squareVector(heightESq); // ampErr squared auto ampErrSq = cloneVector(ampErr); ampErrSq = squareVector(ampErrSq); // totalErr = heightE squared + ampErr squared auto totalErr = cloneVector(heightESq); std::transform(totalErr.begin(), totalErr.end(), ampErrSq.begin(), totalErr.begin(), std::plus<double>()); // heightY squared auto heightYSq = cloneVector(heightY); heightYSq = squareVector(heightYSq); // total Squared auto totalSq = cloneVector(total); totalSq = squareVector(totalSq); // errOverTotalSq = totalErr / total squared auto errOverTotalSq = cloneVector(totalErr); std::transform(errOverTotalSq.begin(), errOverTotalSq.end(), totalSq.begin(), errOverTotalSq.begin(), std::divides<double>()); // heightESqOverYSq = heightESq / heightYSq auto heightESqOverYSq = cloneVector(heightESq); std::transform(heightESqOverYSq.begin(), heightESqOverYSq.end(), heightYSq.begin(), heightESqOverYSq.begin(), std::divides<double>()); // sqrtESqOverYSq = squareRoot( heightESqOverYSq ) auto sqrtESqOverYSq = cloneVector(heightESqOverYSq); std::transform(sqrtESqOverYSq.begin(), sqrtESqOverYSq.end(), sqrtESqOverYSq.begin(), static_cast<double (*)(double)>(sqrt)); // eisfYSumRoot = eisfY * sqrtESqOverYSq auto eisfYSumRoot = cloneVector(eisfY); std::transform(eisfYSumRoot.begin(), eisfYSumRoot.end(), sqrtESqOverYSq.begin(), eisfYSumRoot.begin(), std::multiplies<double>()); // eisfErr = eisfYSumRoot + errOverTotalSq auto eisfErr = cloneVector(eisfYSumRoot); std::transform(eisfErr.begin(), eisfErr.end(), errOverTotalSq.begin(), eisfErr.begin(), std::plus<double>()); // Append the calculated values to the table workspace auto columnName = ampName.substr(0, (ampName.size() - std::string("Amplitude").size())); columnName += "EISF"; auto errorColumnName = ampErrorName.substr( 0, (ampErrorName.size() - std::string("Amplitude_Err").size())); errorColumnName += "EISF_Err"; tableWs->addColumn("double", columnName); tableWs->addColumn("double", errorColumnName); size_t maxEisf = eisfY.size(); if (eisfErr.size() > maxEisf) { maxEisf = eisfErr.size(); } Column_sptr col = tableWs->getColumn(columnName); Column_sptr errCol = tableWs->getColumn(errorColumnName); for (size_t j = 0; j < maxEisf; j++) { col->cell<double>(j) = eisfY.at(j); errCol->cell<double>(j) = eisfErr.at(j); } } }