예제 #1
0
int SundanceGlobal::finalize()
{
  totalTimer().stop();

  try
  {
    Tabs tab;
    /* we may need to skip timing summaries because of a Trilinos 6.0.x bug */
    if (!skipTimingOutput()) TimeMonitor::summarize();
    //  MPISession::finalize();
  }
  catch(std::exception& e)
  {
    handleException(e);
    return 1;
  }
  return 0;
}
예제 #2
0
int SundanceGlobal::init(int* argc, char*** argv)
{

  try
  {
    /* start up MPI. In a serial run, this will be a no-op */
    //      MPISession::init(argc, argv);
    MPISession::init(argc, (char***) argv);

    /* Start a stopwatch. It will be stopped upon a call to finalize() */
    totalTimer().start();

    Tabs tab;

    /* read standard command line flags */
    std::string configFilename = "";

    bool defaultFpCheck = false;
    bool debugWait = false;
    bool showVersion = false;
    bool showBanner = true;
    bool showTimings = false;
    bool cmdFpCheck = defaultFpCheck;
    int defaultWorkSetSize = 400;
    int cmdWorkSetSize = defaultWorkSetSize;

    Assembler::workSetSize() = defaultWorkSetSize;

    clp().setOption("config", &configFilename, "Configuration file");
    clp().setOption("fpcheck", "nofpcheck", &cmdFpCheck, 
      "Check results of math lib calls in expr evals");
    clp().setOption("version", "noversion", &showVersion, 
      "Show Sundance version number and exit");
    clp().setOption("banner", "nobanner", &showBanner, 
      "Show Sundance banner on root processor at start of run");
    clp().setOption("timings", "notimings", &showTimings, 
      "Show timings at end of run");

    clp().setOption("workset", &cmdWorkSetSize, 
      "Work set size");

      
    clp().setOption("debug", "nodebug", &debugWait, "Whether to attach a debugger to this process, holding until 'wait' is set to 0");


    clp().throwExceptions(false);
    clp().recogniseAllOptions(false);

    CommandLineProcessor::EParseCommandLineReturn rtn 
      = clp().parse(*argc, (char**) *argv);

    TEUCHOS_TEST_FOR_EXCEPTION(rtn != CommandLineProcessor::PARSE_SUCCESSFUL,
      std::runtime_error,
      "Command-line parsing failed");


    if (showVersion)
    {
      if (MPIComm::world().getRank()==0)
      {
        cout << "Simulation built using Sundance version " 
             << VersionString::number() 
             << " (" << VersionString::date() << ")" << std::endl;
        exit(0);
      }
    }
    if (showBanner && MPIComm::world().getRank()==0)
    {
      ostringstream oss;
      oss << "Simulation built using Sundance version " 
           << VersionString::number() 
           << " (" << VersionString::date() << ")" << std::endl;
      
      oss << "Sundance is copyright" 
           << std::endl << " (C) 2005-2012 Sandia National Laboratories " 
           << std::endl
           << " (C) 2007-2012 Texas Tech University"
           << std::endl;
      oss << "and is licensed under the GNU Lesser General Public License, version 2.1" << std::endl;
      oss << std::endl;
      cout << oss.str() << flush;
    }

    MPIComm::world().synchronize();
    if (!showTimings) skipTimingOutput() = true;

    //      debugWait = true;
    if (debugWait)
    {
      int wait=1;
      int pid = getpid();
      std::string myCommandName=((char**)(*argv))[0];
      std::string debugCmd = "ddd --gdb -x ~/.gdbinit " + myCommandName 
        + " " + Teuchos::toString(pid) + " &";
      cout << "launching " << debugCmd << std::endl;
      system(debugCmd.c_str());
      while (wait) {;}
    }


    bool worksetSetOnCmdLine = cmdWorkSetSize != defaultWorkSetSize;
    if (worksetSetOnCmdLine)
    {
      Assembler::workSetSize() = (int) cmdWorkSetSize;
    }
  }
  catch(std::exception& e)
  {
    handleException(e);
    return 1;
  }
  return 0;
} 
예제 #3
0
void ACT::load()//DONE
{
	Timer timer("init");
	Timer totalTimer("ACT total");
	totalTimer.start();
	ConfigFile config;
	string actDir = "actDir";
	string path;
	if(config.keyExists(actDir))
		config.readInto(path, actDir);
	timer.start();
	_loadOthers(path + "topic.others");
	timer.stop();
	LOG(INFO) << boost::str(boost::format("Others done time Used %1%") % timer.getPassedMilliseconds());
	timer.start();
	nTopic = parameter.ntopics;
	if (nTopic == 0)
	{
		nTopic = 200;
	}
	_init();
	timer.stop();
	timer.show();

	timer = Timer("tassign");
	timer.start();
	int arraySize = _ACTa2countMap.size();
	nWords = parameter.nwords;
	nPublications = parameter.ndocs;
	nAuthors = parameter.nauthors;
	nConfs = parameter.nconfs;
	for(int i=0; i<arraySize; i++)
	{
		_ACTa2countMap[i].rehash(nAuthors / nTopic * 30);
		_ACTp2countMap[i].rehash(nPublications / nTopic * 30);
		_ACTw2countMap[i].rehash(nWords / nTopic * 30);
		_ACTc2countMap[i].rehash(nConfs);
	}

	_sumA2Count = new int[nTopic + 1];
	memset(_sumA2Count, 0, sizeof(int)*(nTopic + 1));
	_sumC2Count = new int[nTopic + 1];
	memset(_sumC2Count, 0, sizeof(int)*(nTopic + 1));
	_sumP2Count = new int[nTopic + 1];
	memset(_sumP2Count, 0, sizeof(int)*(nTopic + 1));
	_sumW2Count = new int[nTopic + 1];
	memset(_sumW2Count, 0, sizeof(int)*(nTopic + 1));

	_loadTAssign(path + "topic.tassign");
	timer.stop();
	timer.show();

	timer = Timer("authormap");
	timer.start();
	_loadAuthorMap(path + "authormap.txt");
	timer.stop();
	timer.show();
	timer = Timer("confmap");
	timer.start();
	_loadConfMap(path + "confmap.txt");
	timer.stop();
	timer.show();
	timer = Timer("docmap");
	timer.start();
	_loadDocMap(path + "docmap.txt");
	timer.stop();
	timer.show();
	timer = Timer("wordmap");
	timer.start();
	_loadWordMap(path + "wordmap.txt");
	timer.stop();
	totalTimer.stop();
	LOG(INFO) << boost::str(boost::format("wordmap done time Used %1%") % timer.getPassedMilliseconds());
	LOG(INFO) << boost::str(boost::format("ACT Load, Used %1%") % totalTimer.getPassedMilliseconds());
}
예제 #4
0
int main(int argc, char *argv[]) {

  int status=0; // 0 = pass, failures are incremented
  bool success = true;
  Teuchos::GlobalMPISession mpiSession(&argc,&argv);
  Teuchos::RCP<Teuchos::FancyOStream> out(Teuchos::VerboseObjectBase::getDefaultOStream());

  // Command-line argument for input file
  Albany::CmdLineArgs cmd("input.xml", "inputSG.xml", "inputSG_adjoint.xml");
  cmd.parse_cmdline(argc, argv, *out);
  std::string xmlfilename;
  std::string sg_xmlfilename;
  std::string adjsg_xmlfilename;
  bool do_initial_guess;
  if (cmd.has_third_xml_file) {
    xmlfilename = cmd.xml_filename;
    sg_xmlfilename = cmd.xml_filename2;
    adjsg_xmlfilename = cmd.xml_filename3;
    do_initial_guess = true;
  }
  else if (cmd.has_second_xml_file) {
    xmlfilename = "";
    sg_xmlfilename = cmd.xml_filename;
    adjsg_xmlfilename = cmd.xml_filename2;
    do_initial_guess = false;
  }
  else {
    *out << argv[0] << ":  must supply at least 2 input files!\n";
    std::exit(1);
  }

  try {

    Teuchos::RCP<Teuchos::Time> totalTime =
      Teuchos::TimeMonitor::getNewTimer("AlbanySGAdjoint: ***Total Time***");
    Teuchos::TimeMonitor totalTimer(*totalTime); //start timer
    
    // Setup communication objects
    Teuchos::RCP<Epetra_Comm> globalComm = 
      Albany::createEpetraCommFromMpiComm(Albany_MPI_COMM_WORLD);

    Teuchos::RCP<Stokhos::EpetraVectorOrthogPoly> sg_forward_solution;

    //
    // Solve forward problem
    //
    {
      
    Teuchos::RCP<Teuchos::Time> forwardTime =
      Teuchos::TimeMonitor::getNewTimer("AlbanySGAdjoint: ***Forward Solver Time***");
    Teuchos::TimeMonitor forwardTimer(*forwardTime); //start timer

    // Parse parameters
    Teuchos::RCP<const Teuchos_Comm> comm =
      Tpetra::DefaultPlatform::getDefaultPlatform().getComm();
    // Connect vtune for performance profiling
    if (cmd.vtune) {
      Albany::connect_vtune(comm->getRank());
    }
    Albany::SolverFactory sg_slvrfctry(sg_xmlfilename, comm);
    Teuchos::ParameterList& albanyParams = sg_slvrfctry.getParameters();
    Teuchos::RCP< Teuchos::ParameterList> piroParams = 
      Teuchos::rcp(&(albanyParams.sublist("Piro")),false);
    
    // Create stochastic Galerkin solver
    Teuchos::RCP<Piro::Epetra::StokhosSolver> sg_solver =
      Teuchos::rcp(new Piro::Epetra::StokhosSolver(piroParams, globalComm));

    // Get comm for spatial problem
    Teuchos::RCP<const Epetra_Comm> app_comm = sg_solver->getSpatialComm();

    //MP, can we use comm instead of tappComm?
    Teuchos::RCP<const Teuchos_Comm> tapp_comm = Albany::createTeuchosCommFromEpetraComm(app_comm);

    // Compute initial guess if requested
    Teuchos::RCP<Epetra_Vector> ig;
    if (do_initial_guess) {

      // Create solver
      Albany::SolverFactory slvrfctry(xmlfilename, tapp_comm);
      Teuchos::RCP<EpetraExt::ModelEvaluator> solver = 
         slvrfctry.create(tapp_comm, tapp_comm);

      // Setup in/out args
      EpetraExt::ModelEvaluator::InArgs params_in = solver->createInArgs();
      EpetraExt::ModelEvaluator::OutArgs responses_out = 
	solver->createOutArgs();
      int np = params_in.Np();
      for (int i=0; i<np; i++) {
	Teuchos::RCP<const Epetra_Vector> p = solver->get_p_init(i);
	params_in.set_p(i, p);
      }
      int ng = responses_out.Ng();
      for (int i=0; i<ng; i++) {
	Teuchos::RCP<Epetra_Vector> g = 
	  Teuchos::rcp(new Epetra_Vector(*(solver->get_g_map(i))));
	responses_out.set_g(i, g);
      }

      // Evaluate model
      solver->evalModel(params_in, responses_out);

      // Print responses (not last one since that is x)
      *out << std::endl;
      out->precision(8);
      for (int i=0; i<ng-1; i++) {
	if (responses_out.get_g(i) != Teuchos::null)
	  *out << "Response " << i << " = " << std::endl 
	       << *(responses_out.get_g(i)) << std::endl;
      }

    }

    // Create SG solver
    Teuchos::RCP<Albany::Application> app;
    Teuchos::RCP<EpetraExt::ModelEvaluator> model; {
      model = sg_slvrfctry.createAlbanyAppAndModel(
        app, tapp_comm, Petra::EpetraVector_To_TpetraVectorConst(
          *ig, tapp_comm));
    }
    sg_solver->setup(model);

    // Evaluate SG responses at SG parameters
    EpetraExt::ModelEvaluator::InArgs sg_inArgs = sg_solver->createInArgs();
    EpetraExt::ModelEvaluator::OutArgs sg_outArgs = 
      sg_solver->createOutArgs();
    int np = sg_inArgs.Np();
    for (int i=0; i<np; i++) {
      if (sg_inArgs.supports(EpetraExt::ModelEvaluator::IN_ARG_p_sg, i)) {
	Teuchos::RCP<const Stokhos::EpetraVectorOrthogPoly> p_sg = 
	  sg_solver->get_p_sg_init(i);
	sg_inArgs.set_p_sg(i, p_sg);
      }
    }

    int ng = sg_outArgs.Ng();
    for (int i=0; i<ng; i++) {
      if (sg_outArgs.supports(EpetraExt::ModelEvaluator::OUT_ARG_g_sg, i)) {
	Teuchos::RCP<Stokhos::EpetraVectorOrthogPoly> g_sg =
	  sg_solver->create_g_sg(i);
	sg_outArgs.set_g_sg(i, g_sg);
      }
    }

    sg_solver->evalModel(sg_inArgs, sg_outArgs);

    for (int i=0; i<ng-1; i++) {
      // Don't loop over last g which is x, since it is a long vector
      // to print out.
      if (sg_outArgs.supports(EpetraExt::ModelEvaluator::OUT_ARG_g_sg, i)) {

	// Print mean and standard deviation      
	Teuchos::RCP<Stokhos::EpetraVectorOrthogPoly> g_sg = 
	  sg_outArgs.get_g_sg(i);
	Epetra_Vector g_mean(*(sg_solver->get_g_map(i)));
	Epetra_Vector g_std_dev(*(sg_solver->get_g_map(i)));
	g_sg->computeMean(g_mean);
	g_sg->computeStandardDeviation(g_std_dev);
	out->precision(12);
	*out << "Response " << i << " Mean =      " << std::endl 
	     << g_mean << std::endl;
	*out << "Response " << i << " Std. Dev. = " << std::endl 
	     << g_std_dev << std::endl;

	status += sg_slvrfctry.checkSGTestResults(0, g_sg);
      }
    }
    *out << "\nNumber of Failed Comparisons: " << status << std::endl;
  
    sg_forward_solution = sg_outArgs.get_g_sg(ng-1);

    }


    /* Space reserved for the projection of the forward solution onto
       the higher order basis for the adjoint solution.  
       In general, this will require projecting is physical space
       as well as in stochastic space.
    */



    // 
    // Solve adjoint problem
    //
    {

    Teuchos::RCP<Teuchos::Time> adjointTime =
      Teuchos::TimeMonitor::getNewTimer("AlbanySG: ***Adjoint Solver Time***");
    Teuchos::TimeMonitor adjtotalTimer(*adjointTime); //start timer

    // Parse parameters
    Albany::SolverFactory sg_slvrfctry(adjsg_xmlfilename, 
      Albany::createTeuchosCommFromMpiComm(Albany_MPI_COMM_WORLD));
    Teuchos::ParameterList& albanyParams = sg_slvrfctry.getParameters();
    Teuchos::RCP< Teuchos::ParameterList> piroParams = 
      Teuchos::rcp(&(albanyParams.sublist("Piro")),false);
    
    // Create stochastic Galerkin solver
    Teuchos::RCP<Piro::Epetra::StokhosSolver> sg_solver =
      Teuchos::rcp(new Piro::Epetra::StokhosSolver(piroParams, globalComm));

    // Get comm for spatial problem
    Teuchos::RCP<const Epetra_Comm> app_comm = sg_solver->getSpatialComm();
    Teuchos::RCP<const Teuchos_Comm> tapp_comm = Albany::createTeuchosCommFromEpetraComm(app_comm);

    // Create SG solver
    Teuchos::RCP<Albany::Application> app;
    Teuchos::RCP<EpetraExt::ModelEvaluator> model = 
      sg_slvrfctry.createAlbanyAppAndModel(app, tapp_comm);
    sg_solver->setup(model);

    // Set projected forward solution as the initial guess
    sg_solver->set_x_sg_init(*sg_forward_solution);

    // Evaluate SG responses at SG parameters
    EpetraExt::ModelEvaluator::InArgs sg_inArgs = sg_solver->createInArgs();
    EpetraExt::ModelEvaluator::OutArgs sg_outArgs = 
      sg_solver->createOutArgs();
    int np = sg_inArgs.Np();
    for (int i=0; i<np; i++) {
      if (sg_inArgs.supports(EpetraExt::ModelEvaluator::IN_ARG_p_sg, i)) {
	Teuchos::RCP<const Stokhos::EpetraVectorOrthogPoly> p_sg = 
	  sg_solver->get_p_sg_init(i);
	sg_inArgs.set_p_sg(i, p_sg);
      }
    }

    int ng = sg_outArgs.Ng();
    for (int i=0; i<ng; i++) {
      if (sg_outArgs.supports(EpetraExt::ModelEvaluator::OUT_ARG_g_sg, i)) {
	Teuchos::RCP<Stokhos::EpetraVectorOrthogPoly> g_sg =
	  sg_solver->create_g_sg(i);
	sg_outArgs.set_g_sg(i, g_sg);
      }
    }

    sg_solver->evalModel(sg_inArgs, sg_outArgs);

    for (int i=0; i<ng-1; i++) {
      // Don't loop over last g which is x, since it is a long vector
      // to print out.
      if (sg_outArgs.supports(EpetraExt::ModelEvaluator::OUT_ARG_g_sg, i)) {

	// Print mean and standard deviation      
	Teuchos::RCP<Stokhos::EpetraVectorOrthogPoly> g_sg = 
	  sg_outArgs.get_g_sg(i);
	Epetra_Vector g_mean(*(sg_solver->get_g_map(i)));
	Epetra_Vector g_std_dev(*(sg_solver->get_g_map(i)));
	g_sg->computeMean(g_mean);
	g_sg->computeStandardDeviation(g_std_dev);
	out->precision(12);
	*out << "Response " << i << " Mean =      " << std::endl 
	     << g_mean << std::endl;
	*out << "Response " << i << " Std. Dev. = " << std::endl 
	     << g_std_dev << std::endl;

	status += sg_slvrfctry.checkSGTestResults(0, g_sg);
      }
    }
    *out << "\nNumber of Failed Comparisons: " << status << std::endl;

    /* Space reserved for computing the error representation which involves
       integrating over both physical and stochastic space and may require
       a number of projections.
    */

    }

    totalTimer.~TimeMonitor();
    Teuchos::TimeMonitor::summarize(std::cout,false,true,false);

  }
  TEUCHOS_STANDARD_CATCH_STATEMENTS(true, std::cerr, success);
  if (!success) status+=10000;

  return status;
}
void Ifpack2PreconditionerFactory<MatrixType>::initializePrec(
  const Teuchos::RCP<const LinearOpSourceBase<scalar_type> > &fwdOpSrc,
  PreconditionerBase<scalar_type> *prec,
  const ESupportSolveUse /* supportSolveUse */
  ) const
{
  // Check precondition

  TEUCHOS_ASSERT(Teuchos::nonnull(fwdOpSrc));
  TEUCHOS_ASSERT(this->isCompatible(*fwdOpSrc));
  TEUCHOS_ASSERT(prec);

  Teuchos::Time totalTimer(""), timer("");
  totalTimer.start(true);

  const Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
  const Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel();
  Teuchos::OSTab tab(out);
  if (Teuchos::nonnull(out) && Teuchos::includesVerbLevel(verbLevel, Teuchos::VERB_MEDIUM)) {
    *out << "\nEntering Thyra::Ifpack2PreconditionerFactory::initializePrec(...) ...\n";
  }

  // Retrieve wrapped concrete Tpetra matrix from FwdOp

  const Teuchos::RCP<const LinearOpBase<scalar_type> > fwdOp = fwdOpSrc->getOp();
  TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(fwdOp));

  typedef typename MatrixType::local_ordinal_type local_ordinal_type;
  typedef typename MatrixType::global_ordinal_type global_ordinal_type;
  typedef typename MatrixType::node_type node_type;

  typedef Thyra::TpetraLinearOp<scalar_type, local_ordinal_type, global_ordinal_type, node_type> ThyraTpetraLinOp;
  const Teuchos::RCP<const ThyraTpetraLinOp> thyraTpetraFwdOp = Teuchos::rcp_dynamic_cast<const ThyraTpetraLinOp>(fwdOp);
  TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(thyraTpetraFwdOp));

  typedef Tpetra::Operator<scalar_type, local_ordinal_type, global_ordinal_type, node_type> TpetraLinOp;
  const Teuchos::RCP<const TpetraLinOp> tpetraFwdOp = thyraTpetraFwdOp->getConstTpetraOperator();
  TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(tpetraFwdOp));

  const Teuchos::RCP<const MatrixType> tpetraFwdMatrix = Teuchos::rcp_dynamic_cast<const MatrixType>(tpetraFwdOp);
  TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(tpetraFwdMatrix));

  // Retrieve concrete preconditioner object

  const Teuchos::Ptr<DefaultPreconditioner<scalar_type> > defaultPrec =
    Teuchos::ptr(dynamic_cast<DefaultPreconditioner<scalar_type> *>(prec));
  TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(defaultPrec));

  // Process parameter list

  Teuchos::RCP<const Teuchos::ParameterList> constParamList = paramList_;
  if (constParamList.is_null ()) {
    constParamList = getValidParameters ();
  }
  const std::string preconditionerType = Teuchos::getParameter<std::string>(*constParamList, "Prec Type");
  const Teuchos::RCP<const Teuchos::ParameterList> packageParamList = Teuchos::sublist(constParamList, "Ifpack2 Settings");

  // precTypeUpper is the upper-case version of preconditionerType.
  std::string precTypeUpper (preconditionerType);
  if (precTypeUpper.size () > 0) {
    std::locale locale;
    for (size_t k = 0; k < precTypeUpper.size (); ++k) {
      precTypeUpper[k] = std::toupper<char> (precTypeUpper[k], locale);
    }
  }
  
  // mfh 09 Nov 2013: If the Ifpack2 list doesn't already have the
  // "schwarz: overlap level" parameter, then override it with the
  // value of "Overlap".  This avoids use of the newly deprecated
  // three-argument version of Ifpack2::Factory::create() that takes
  // the overlap as an integer.
  if (constParamList->isType<int> ("Overlap") && ! packageParamList.is_null () && ! packageParamList->isType<int> ("schwarz: overlap level") &&
      precTypeUpper == "SCHWARZ") {
    const int overlap = constParamList->get<int> ("Overlap");
    Teuchos::RCP<Teuchos::ParameterList> nonconstPackageParamList =
      Teuchos::sublist (paramList_, "Ifpack2 Settings");
    nonconstPackageParamList->set ("schwarz: overlap level", overlap);
  }

  // Create the initial preconditioner

  if (Teuchos::nonnull(out) && Teuchos::includesVerbLevel(verbLevel, Teuchos::VERB_LOW)) {
    *out << "\nCreating a new Ifpack2::Preconditioner object...\n";
  }
  timer.start(true);

  typedef Ifpack2::Preconditioner<scalar_type, local_ordinal_type, global_ordinal_type, node_type> Ifpack2Prec;
  typedef Tpetra::RowMatrix<scalar_type, local_ordinal_type,
    global_ordinal_type, node_type> row_matrix_type;
  const Teuchos::RCP<Ifpack2Prec> concretePrecOp =
    Ifpack2::Factory::create<row_matrix_type> (preconditionerType, tpetraFwdMatrix);

  timer.stop();
  if (Teuchos::nonnull(out) && Teuchos::includesVerbLevel(verbLevel, Teuchos::VERB_LOW)) {
    Teuchos::OSTab(out).o() << "> Creation time = " << timer.totalElapsedTime() << " sec\n";
  }

  // Initialize and compute the initial preconditioner

  concretePrecOp->setParameters(*packageParamList);
  concretePrecOp->initialize();
  concretePrecOp->compute();

  // Wrap concrete preconditioner

  const Teuchos::RCP<LinearOpBase<scalar_type> > thyraPrecOp = Thyra::createLinearOp(Teuchos::RCP<TpetraLinOp>(concretePrecOp));
  defaultPrec->initializeUnspecified(thyraPrecOp);

  totalTimer.stop();
  if (Teuchos::nonnull(out) && Teuchos::includesVerbLevel(verbLevel, Teuchos::VERB_LOW)) {
    *out << "\nTotal time in Thyra::Ifpack2PreconditionerFactory::initializePrec(...) = " << totalTimer.totalElapsedTime() << " sec\n";
  }

  if (Teuchos::nonnull(out) && Teuchos::includesVerbLevel(verbLevel, Teuchos::VERB_MEDIUM)) {
    *out << "\nLeaving Thyra::Ifpack2PreconditionerFactory::initializePrec(...) ...\n";
  }
}
LinearOp ModALPreconditionerFactory::buildPreconditionerOperator(BlockedLinearOp & alOp,
      BlockPreconditionerState & state) const
{
   Teko_DEBUG_SCOPE("ModALPreconditionerFactory::buildPreconditionerOperator()", 10);
   Teko_DEBUG_EXPR(Teuchos::Time timer(""));
   Teko_DEBUG_EXPR(Teuchos::Time totalTimer(""));
   Teko_DEBUG_EXPR(totalTimer.start());

   // Only for 2D or 3D problems.
   int dim = blockRowCount(alOp) - 1;
   TEUCHOS_ASSERT(dim == 2 || dim == 3);

   // Build what is necessary for the state object.
   Teko_DEBUG_EXPR(timer.start(true));
   invOpsStrategy_->buildState(alOp, state);
   Teko_DEBUG_EXPR(timer.stop());
   Teko_DEBUG_MSG("ModALPreconditionerFactory::buildPreconditionerOperator():BuildStateTime = "
         << timer.totalElapsedTime(), 2);

   // Extract inverse operators from strategy
   Teko_DEBUG_EXPR(timer.start(true));
   LinearOp invA11p = invOpsStrategy_->getInvA11p(state);
   LinearOp invA22p = invOpsStrategy_->getInvA22p(state);
   LinearOp invA33p;
   if(dim == 3)
   {
      invA33p = invOpsStrategy_->getInvA33p(state);
   }

   // The inverse of S can be built from strategy,
   // or just a diagonal matrix.
   ModALPrecondState * modALState = dynamic_cast<ModALPrecondState*>(&state);
   TEUCHOS_ASSERT(modALState != NULL);
   LinearOp invS;
   if(modALState->isStabilized_)
   {
      invS = invOpsStrategy_->getInvS(state);
   }
   else
   {
      invS = scale(modALState->gamma_, modALState->invPressureMassMatrix_);
   }

   Teko_DEBUG_EXPR(timer.stop());
   Teko_DEBUG_MSG("ModALPrecFact::buildPreconditionerOperator(): GetInvTime = "
         << timer.totalElapsedTime(), 2);

   // Build diagonal operations.
   std::vector<LinearOp> invDiag;
   invDiag.resize(dim + 1);
   invDiag[0] = invA11p;
   invDiag[1] = invA22p;
   if(dim == 2)
   {
      invDiag[2] = scale(-1.0, invS);
   }
   else if(dim == 3)
   {
      invDiag[2] = invA33p;
      invDiag[3] = scale(-1.0, invS);
   }

   // Get the upper triangular matrix.
   BlockedLinearOp U = getUpperTriBlocks(alOp);

   Teko_DEBUG_EXPR(totalTimer.stop());
   Teko_DEBUG_MSG("ModALPrecFact::buildPreconditionerOperator TotalTime = "
         << totalTimer.totalElapsedTime(), 2);

   // Create the preconditioner
   return createBlockUpperTriInverseOp(U, invDiag, "Modified AL preconditioner-Upper");
}
예제 #7
0
int main(int argc, char *argv[]) {

  int status=0; // 0 = pass, failures are incremented
  bool success = true;
  Teuchos::GlobalMPISession mpiSession(&argc,&argv);
  Teuchos::RCP<Teuchos::FancyOStream> out(Teuchos::VerboseObjectBase::getDefaultOStream());

  // Command-line argument for input file
  Albany::CmdLineArgs cmd("input.xml", "inputSG.xml");
  cmd.parse_cmdline(argc, argv, *out);
  std::string xmlfilename;
  std::string sg_xmlfilename;
  bool do_initial_guess;
  if (cmd.has_second_xml_file) {
    xmlfilename = cmd.xml_filename;
    sg_xmlfilename = cmd.xml_filename2;
    do_initial_guess = true;
  }
  else if (cmd.has_first_xml_file) {
    xmlfilename = "";
    sg_xmlfilename = cmd.xml_filename;
    do_initial_guess = false;
  }
  else {
    xmlfilename = "";
    sg_xmlfilename = "inputSG.xml";
    do_initial_guess = false;
  }

  try {

    Teuchos::RCP<Teuchos::Time> totalTime =
      Teuchos::TimeMonitor::getNewTimer("AlbanySG: ***Total Time***");
    Teuchos::TimeMonitor totalTimer(*totalTime); //start timer

    // Setup communication objects
    Teuchos::RCP<Epetra_Comm> globalComm =
      Albany::createEpetraCommFromMpiComm(Albany_MPI_COMM_WORLD);
    Teuchos::RCP<const Teuchos_Comm> tcomm =
      Tpetra::DefaultPlatform::getDefaultPlatform().getComm();

    // Connect vtune for performance profiling
    if (cmd.vtune) {
      Albany::connect_vtune(tcomm->getRank());
    }

    // Parse parameters
    Albany::SolverFactory sg_slvrfctry(sg_xmlfilename, tcomm);
    Teuchos::ParameterList& albanyParams = sg_slvrfctry.getParameters();
    Teuchos::RCP< Teuchos::ParameterList> piroParams =
      Teuchos::rcp(&(albanyParams.sublist("Piro")),false);

    // If SG is not enabled and user chose "Direct" or "AD",
    // for "SG Method", change this "Global", which always works
#ifndef ALBANY_SG
    std::string sg_method =
      piroParams->sublist("Stochastic Galerkin").get("SG Method", "Global");
    if (sg_method == "Direct" || sg_method == "AD") {
      piroParams->sublist("Stochastic Galerkin").set("SG Method", "Global");
      *out << "**********************************************************\n"
           << "* WARNING!  Direct SG method was chosen, however         *\n"
           << "* ALBANY_SG is not enabled.  Changing to Global method.  *\n"
           << "**********************************************************\n"
           << std::endl;
    }
#endif

    // Create stochastic Galerkin solver
    Teuchos::RCP<Piro::Epetra::StokhosSolver> sg_solver =
      Teuchos::rcp(new Piro::Epetra::StokhosSolver(piroParams, globalComm));

    // Get comm for spatial problem
    Teuchos::RCP<const Epetra_Comm> app_comm = sg_solver->getSpatialComm();
    Teuchos::RCP<const Teuchos_Comm> tapp_comm = Albany::createTeuchosCommFromEpetraComm(app_comm);

    // Compute initial guess if requested
    Teuchos::RCP<Epetra_Vector> ig;
    if (do_initial_guess) {

      // Create solver
      Albany::SolverFactory slvrfctry(
	xmlfilename, tcomm);
      Teuchos::RCP<EpetraExt::ModelEvaluator> solver =
	slvrfctry.create(tapp_comm, tapp_comm);

      // Setup in/out args
      EpetraExt::ModelEvaluator::InArgs params_in = solver->createInArgs();
      EpetraExt::ModelEvaluator::OutArgs responses_out =
	solver->createOutArgs();
      int np = params_in.Np();
      for (int i=0; i<np; i++) {
	Teuchos::RCP<const Epetra_Vector> p = solver->get_p_init(i);
	params_in.set_p(i, p);
      }
      int ng = responses_out.Ng();
      for (int i=0; i<ng; i++) {
	Teuchos::RCP<Epetra_Vector> g =
	  Teuchos::rcp(new Epetra_Vector(*(solver->get_g_map(i))));
	responses_out.set_g(i, g);
      }

      // Evaluate model
      solver->evalModel(params_in, responses_out);

      // Print responses (not last one since that is x)
      *out << std::endl;
      out->precision(8);
      for (int i=0; i<ng-1; i++) {
	if (responses_out.get_g(i) != Teuchos::null)
	  *out << "Response " << i << " = " << std::endl
	       << *(responses_out.get_g(i)) << std::endl;
      }

      // Get final solution as initial guess
      ig = responses_out.get_g(ng-1);

      Teuchos::TimeMonitor::summarize(std::cout,false,true,false);
    }

    // Create SG solver
    Teuchos::RCP<Albany::Application> app;
    Teuchos::RCP<const Tpetra_Vector> initial_guessT;
    if (Teuchos::nonnull(ig)) {
      initial_guessT = Petra::EpetraVector_To_TpetraVectorConst(*ig, tcomm);
    }
    Teuchos::RCP<EpetraExt::ModelEvaluator> model =
      sg_slvrfctry.createAlbanyAppAndModel(app, tcomm, initial_guessT);

    // Hack in rigid body modes for ML
    {
      Teuchos::RCP<Teuchos::ParameterList> sg_solver_params =
        Teuchos::sublist(Teuchos::sublist(piroParams, "Stochastic Galerkin"), "SG Solver Parameters");
      Teuchos::RCP<Teuchos::ParameterList> sg_prec_params =
        Teuchos::sublist(sg_solver_params, "SG Preconditioner");

      if (sg_prec_params->isParameter("Mean Preconditioner Type")) {
        if (sg_prec_params->get<std::string>("Mean Preconditioner Type") == "ML") {
          Teuchos::RCP<Teuchos::ParameterList> ml_params =
            Teuchos::sublist(sg_prec_params, "Mean Preconditioner Parameters");
          const Teuchos::RCP<Albany::RigidBodyModes>&
            rbm = app->getProblem()->getNullSpace();
          // Previously, updateMLPL called importML, but there was no coordinate
          // data yet. Now we just update the parameter list.
          rbm->updatePL(ml_params);          
          sg_solver->resetSolverParameters(*sg_solver_params);
        }
      }
    }

    // Setup SG solver
    {
      const Teuchos::RCP<NOX::Epetra::Observer > NOX_observer =
        Teuchos::rcp(new Albany_NOXObserver(app));
      sg_solver->setup(model, NOX_observer);
    }

    // Evaluate SG responses at SG parameters
    EpetraExt::ModelEvaluator::InArgs sg_inArgs = sg_solver->createInArgs();
    EpetraExt::ModelEvaluator::OutArgs sg_outArgs =
      sg_solver->createOutArgs();
    int np = sg_inArgs.Np();
    for (int i=0; i<np; i++) {
      if (sg_inArgs.supports(EpetraExt::ModelEvaluator::IN_ARG_p_sg, i)) {
	Teuchos::RCP<const Stokhos::EpetraVectorOrthogPoly> p_sg =
	  sg_solver->get_p_sg_init(i);
	sg_inArgs.set_p_sg(i, p_sg);
      }
    }

    // By default, request the sensitivities if not explicitly disabled
    const bool computeSensitivities =
      sg_slvrfctry.getAnalysisParameters().sublist("Solve").get("Compute Sensitivities", true);
    int ng = sg_outArgs.Ng();
    for (int i=0; i<ng; i++) {
      if (sg_outArgs.supports(EpetraExt::ModelEvaluator::OUT_ARG_g_sg, i)) {
	Teuchos::RCP<Stokhos::EpetraVectorOrthogPoly> g_sg =
	  sg_solver->create_g_sg(i);
	sg_outArgs.set_g_sg(i, g_sg);
      }

      for (int j=0; j<np; j++) {
	EpetraExt::ModelEvaluator::DerivativeSupport ds =
	  sg_outArgs.supports(EpetraExt::ModelEvaluator::OUT_ARG_DgDp_sg,i,j);
	if (computeSensitivities &&
	    ds.supports(EpetraExt::ModelEvaluator::DERIV_MV_BY_COL)) {
	  int ncol = sg_solver->get_p_map(j)->NumMyElements();
	  Teuchos::RCP<Stokhos::EpetraMultiVectorOrthogPoly> dgdp_sg =
	    sg_solver->create_g_mv_sg(i,ncol);
	  sg_outArgs.set_DgDp_sg(i, j, dgdp_sg);
	}
      }
    }

    sg_solver->evalModel(sg_inArgs, sg_outArgs);

    bool printResponse =
      albanyParams.sublist("Problem").get("Print Response Expansion", true);
    for (int i=0; i<ng-1; i++) {
      // Don't loop over last g which is x, since it is a long vector
      // to print out.
      if (sg_outArgs.supports(EpetraExt::ModelEvaluator::OUT_ARG_g_sg, i)) {

	// Print mean and standard deviation
	Teuchos::RCP<Stokhos::EpetraVectorOrthogPoly> g_sg =
	  sg_outArgs.get_g_sg(i);
	if (g_sg != Teuchos::null && app->getResponse(i)->isScalarResponse()) {
	  Epetra_Vector g_mean(*(sg_solver->get_g_map(i)));
	  Epetra_Vector g_std_dev(*(sg_solver->get_g_map(i)));
	  g_sg->computeMean(g_mean);
	  g_sg->computeStandardDeviation(g_std_dev);
	  out->precision(12);
	  out->setf(std::ios::scientific);
	  *out << "Response " << i << " Mean =      " << std::endl
	       << g_mean << std::endl;
	  *out << "Response " << i << " Std. Dev. = " << std::endl
	       << g_std_dev << std::endl;
	  if (printResponse) {
	    *out << "Response " << i << "           = " << std::endl
		 << *g_sg << std::endl;
	    for (int j=0; j<np; j++) {
	      EpetraExt::ModelEvaluator::DerivativeSupport ds =
		sg_outArgs.supports(EpetraExt::ModelEvaluator::OUT_ARG_DgDp_sg,i,j);
	      if (!ds.none()) {
		Teuchos::RCP<Stokhos::EpetraMultiVectorOrthogPoly> dgdp_sg =
		  sg_outArgs.get_DgDp_sg(i,j).getMultiVector();
		if (dgdp_sg != Teuchos::null)
		  *out << "Response " << i << " Derivative " << j << " = "
		       << std::endl << *dgdp_sg << std::endl;
	      }
	    }
	  }

	  status += sg_slvrfctry.checkSGTestResults(i, g_sg, &g_mean, &g_std_dev);
	}
      }
    }
    *out << "\nNumber of Failed Comparisons: " << status << std::endl;

    totalTimer.~TimeMonitor();
    Teuchos::TimeMonitor::summarize(std::cout,false,true,false);
    Teuchos::TimeMonitor::zeroOutTimers();

  }
  TEUCHOS_STANDARD_CATCH_STATEMENTS(true, std::cerr, success);
  if (!success) status+=10000;

  return status;
}