Skip to content

Commit

Permalink
Enhancing access to Output objects (closes idaholab#2885)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeslaughter committed Apr 22, 2014
1 parent 912a736 commit ec95a6d
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 13 deletions.
111 changes: 109 additions & 2 deletions framework/include/outputs/OutputWarehouse.h
Expand Up @@ -135,9 +135,39 @@ class OutputWarehouse
void init();

/**
* Return an Outputter object by name
* Return an Output object by name
* @tparam T The Output object type to return
* @param The name of the output object
* @return A pointer to the output object
*/
OutputBase * getOutputByName(std::string name);
template<typename T>
T * getOutput(std::string name);

/**
* Return a vector of objects by names
* @tparam T The Output object type to return
* @param names A vector of names of the output object
* @return A pointer to the output object
*/
template<typename T>
std::vector<T *> getOutputs(std::vector<std::string> names);

/**
* Return a vector of objects of a given type
* @tparam T The Output object type to return
* @return A pointer to the output object
*/
template<typename T>
std::vector<T *> getOutputs();

/**
* Return a list of output objects with a given type
* @tparam T The output object type
* @return A vector of names
*/
template<typename T>
std::vector<std::string> getOutputNames();

This comment has been minimized.

Copy link
@YaqiWang

YaqiWang Apr 22, 2014

This is what I need, works for me.



private:

Expand Down Expand Up @@ -186,4 +216,81 @@ class OutputWarehouse
friend class FEProblem;
};

template<typename T>
T *
OutputWarehouse::getOutput(std::string name)
{
// Check that the object exists
if (!hasOutput(name))
mooseError("An output object with the name '" << name << "' does not exist.");

// Attempt to cast the object to the correct type
T * output = dynamic_cast<T*>(_object_map[name]);

// Error if the cast fails
if (output == NULL)
mooseError("An output object with the name '" << name << "' for the specified type does not exist");

// Return the object
return output;
}

template<typename T>
std::vector<T *>
OutputWarehouse::getOutputs(std::vector<std::string> names)
{
// The vector to output
std::vector<T *> outputs;

// Populate the vector
for (std::vector<std::string>::iterator it = names.begin(); it != names.end(); ++it)
outputs.push_back(getOutput<T>(*it));

// Return the objects
return outputs;
}

template<typename T>
std::vector<T *>
OutputWarehouse::getOutputs()
{
// The vector to output
std::vector<T *> outputs;

// Populate the vector
for (std::map<std::string, OutputBase *>::iterator it = _object_map.begin(); it != _object_map.end(); ++it)
{
T * output = dynamic_cast<T*>(it->second);
if (output != NULL)
outputs.push_back(output);
}

// Return the objects
return outputs;
}

/**
* Return a list of output objects with a given type
* @tparam T The output object type
* @return A vector of names
*/
template<typename T>
std::vector<std::string>
OutputWarehouse::getOutputNames()
{
// The output vector
std::vector<std::string> names;

// Loop through the objects and store the name if the type cast succeeds
for (std::map<std::string, OutputBase *>::iterator it = _object_map.begin(); it != _object_map.end(); ++it)
{
T * output = dynamic_cast<T*>(it->second);
if (output != NULL)
names.push_back(it->first);
}

// Return the names
return names;
}

#endif // OUTPUTWAREHOUSE_H
9 changes: 0 additions & 9 deletions framework/src/outputs/OutputWarehouse.C
Expand Up @@ -227,12 +227,3 @@ OutputWarehouse::getSyncTimes()
{
return _sync_times;
}

OutputBase *
OutputWarehouse::getOutputByName(std::string name)
{
if (!hasOutput(name))
mooseError("An outputter with the name " << name << " does not exist.");

return _object_map[name];
}
4 changes: 2 additions & 2 deletions framework/src/outputs/PetscOutputter.C
Expand Up @@ -31,8 +31,8 @@ InputParameters validParams<PetscOutputter>()
params.addParam<bool>("nonlinear_residuals", false, "Specifies whether output occurs on each nonlinear residual evaluation");

// Psuedo time step divisors
params.addParam<Real>("nonlinear_residual_dt_divisor", 1000, "Number of divisions applied to time step when outtputting non-linear residuals");
params.addParam<Real>("linear_residual_dt_divisor", 1000, "Number of divisions applied to time step when outtputting linear residuals");
params.addParam<Real>("nonlinear_residual_dt_divisor", 1000, "Number of divisions applied to time step when outputting non-linear residuals");
params.addParam<Real>("linear_residual_dt_divisor", 1000, "Number of divisions applied to time step when outputting linear residuals");

// Start times for residual output
params.addParam<Real>("linear_residual_start_time", "Specifies a start time to begin output on each linear residual evaluation");
Expand Down
53 changes: 53 additions & 0 deletions test/include/outputs/OutputObjectTest.h
@@ -0,0 +1,53 @@
/****************************************************************/
/* DO NOT MODIFY THIS HEADER */
/* MOOSE - Multiphysics Object Oriented Simulation Environment */
/* */
/* (c) 2010 Battelle Energy Alliance, LLC */
/* ALL RIGHTS RESERVED */
/* */
/* Prepared by Battelle Energy Alliance, LLC */
/* Under Contract No. DE-AC07-05ID14517 */
/* With the U. S. Department of Energy */
/* */
/* See COPYRIGHT for full restrictions */
/****************************************************************/

#ifndef OUTPUTOBJECTTEST_H
#define OUTPUTOBJECTTEST_H

// MOOSE includes
#include "Console.h"

// Forward declerations
class OutputObjectTest;

template<>
InputParameters validParams<OutputObjectTest>();

/**
*
*/
class OutputObjectTest : public Console
{
public:

/**
* Class constructor
* @param name
* @param InputParameters
*/
OutputObjectTest(const std::string & name, InputParameters parameters);

/**
* Class destructor
*/
virtual ~OutputObjectTest();

void initialSetup();

private:
MooseEnum _type;

};

#endif //OUTPUTOBJECTTEST_H
5 changes: 5 additions & 0 deletions test/src/base/MooseTestApp.C
Expand Up @@ -171,6 +171,8 @@
// From MOOSE
#include "AddVariableAction.h"

// Outputs
#include "OutputObjectTest.h"

template<>
InputParameters validParams<MooseTestApp>()
Expand Down Expand Up @@ -370,6 +372,9 @@ MooseTestApp::registerObjects(Factory & factory)

registerProblem(MooseTestProblem);
registerProblem(FailingProblem);

// Outputs
registerOutput(OutputObjectTest);
}

void
Expand Down
76 changes: 76 additions & 0 deletions test/src/outputs/OutputObjectTest.C
@@ -0,0 +1,76 @@
/****************************************************************/
/* DO NOT MODIFY THIS HEADER */
/* MOOSE - Multiphysics Object Oriented Simulation Environment */
/* */
/* (c) 2010 Battelle Energy Alliance, LLC */
/* ALL RIGHTS RESERVED */
/* */
/* Prepared by Battelle Energy Alliance, LLC */
/* Under Contract No. DE-AC07-05ID14517 */
/* With the U. S. Department of Energy */
/* */
/* See COPYRIGHT for full restrictions */
/****************************************************************/

#include "OutputObjectTest.h"
#include "MooseApp.h"
#include "Exodus.h"

template<>
InputParameters validParams<OutputObjectTest>()
{
MooseEnum type("getOutput, getOutputs-names, getOutputs, getOutputNames");
InputParameters params = validParams<Console>();
params.addRequiredParam<MooseEnum>("test_type", type, "The type of test to execute");
return params;
}

OutputObjectTest::OutputObjectTest(const std::string & name, InputParameters parameters) :
Console(name, parameters),
_type(getParam<MooseEnum>("test_type"))
{
}

OutputObjectTest::~OutputObjectTest()
{
}

void
OutputObjectTest::initialSetup()
{
Console::initialSetup();

if (_type == "getOutput")
{
OutputObjectTest * ptr = _app.getOutputWarehouse().getOutput<OutputObjectTest>(_name);
if (ptr == this)
mooseError("getOutput test passed");
}

else if (_type == "getOutputs")
{
std::vector<Exodus *> ptrs = _app.getOutputWarehouse().getOutputs<Exodus>();
if (ptrs.size() == 2 && ptrs[0]->name().compare("exodus") == 0 && ptrs[1]->name().compare("exodus2") == 0)
mooseError("getOutputs test passed");
}

else if (_type == "getOutputs-names")
{
std::vector<std::string> names;
names.push_back("exodus2");
names.push_back("exodus");
std::vector<Exodus *> ptrs = _app.getOutputWarehouse().getOutputs<Exodus>(names);
if (ptrs.size() == 2 && ptrs[0]->name().compare("exodus2") == 0 && ptrs[1]->name().compare("exodus") == 0)
mooseError("getOutputs-names test passed");
}

else if (_type == "getOutputNames")
{
std::vector<std::string> names = _app.getOutputWarehouse().getOutputNames<Exodus>();
if (names.size() == 2 && names[0].compare("exodus") == 0 && names[1].compare("exodus2") == 0)
mooseError("getOutputsNames test passed");
}

else
mooseError("You must specify a 'test_type'");
}
30 changes: 30 additions & 0 deletions test/tests/outputs/misc/tests
@@ -0,0 +1,30 @@
[Tests]
[./getOutput]
type = RunException
input = warehouse_access.i
cli_args = 'Outputs/test/test_type=getOutput'
expect_err = "getOutput test passed"
max_parallel = 1
[../]
[./getOutputs]
type = RunException
input = warehouse_access.i
cli_args = 'Outputs/test/test_type=getOutputs'
expect_err = "getOutputs test passed"
max_parallel = 1
[../]
[./getOutputs_with_names]
type = RunException
input = warehouse_access.i
cli_args = 'Outputs/test/test_type=getOutputs-names'
expect_err = "getOutputs-names test passed"
max_parallel = 1
[../]
[./getOutputNames]
type = RunException
input = warehouse_access.i
cli_args = 'Outputs/test/test_type=getOutputNames'
expect_err = "getOutputsNames test passed"
max_parallel = 1
[../]
[]
56 changes: 56 additions & 0 deletions test/tests/outputs/misc/warehouse_access.i
@@ -0,0 +1,56 @@
[Mesh]
type = GeneratedMesh
dim = 2
nx = 10
ny = 10
[]

[Variables]
[./u]
[../]
[]

[Kernels]
[./diff]
type = Diffusion
variable = u
[../]
[]

[BCs]
[./left]
type = DirichletBC
variable = u
boundary = left
value = 0
[../]
[./right]
type = DirichletBC
variable = u
boundary = right
value = 1
[../]
[]

[Executioner]
type = Steady

# Preconditioned JFNK (default)
solve_type = 'PJFNK'


petsc_options_iname = '-pc_type -pc_hypre_type'
petsc_options_value = 'hypre boomeramg'
[]

[Outputs]
exodus = true
vtk = true
[./exodus2]
type = Exodus
file_base = exodus2
[../]
[./test]
type = OutputObjectTest
[../]
[]

0 comments on commit ec95a6d

Please sign in to comment.