www.mooseframework.org
MooseObjectUnitTest.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include "gtest/gtest.h"
13 
14 #include "MooseMesh.h"
15 #include "FEProblem.h"
16 #include "AppFactory.h"
17 #include "MooseMain.h"
18 
69 class MooseObjectUnitTest : public ::testing::Test
70 {
71 public:
75  MooseObjectUnitTest(const std::string & app_name)
76  : _app(Moose::createMooseApp(app_name, 0, nullptr)), _factory(_app->getFactory())
77  {
78  buildObjects();
79  }
80 
81 protected:
82  void buildObjects()
83  {
84  InputParameters mesh_params = _factory.getValidParams("GeneratedMesh");
85  mesh_params.set<MooseEnum>("dim") = "3";
86  mesh_params.set<unsigned int>("nx") = 2;
87  mesh_params.set<unsigned int>("ny") = 2;
88  mesh_params.set<unsigned int>("nz") = 2;
89  _mesh = _factory.createUnique<MooseMesh>("GeneratedMesh", "name1", mesh_params);
90  _mesh->setMeshBase(_mesh->buildMeshBaseObject());
91  _mesh->buildMesh();
92 
93  InputParameters problem_params = _factory.getValidParams("FEProblem");
94  problem_params.set<MooseMesh *>("mesh") = _mesh.get();
95  problem_params.set<std::string>("_object_name") = "name2";
96  _fe_problem = _factory.create<FEProblem>("FEProblem", "problem", problem_params);
97 
98  _fe_problem->createQRules(QGAUSS, FIRST, FIRST, FIRST);
99 
100  _app->actionWarehouse().problemBase() = _fe_problem;
101  }
102 
103  template <typename T>
104  T & addObject(const std::string & type, const std::string & name, InputParameters & params);
105 
106  std::unique_ptr<MooseMesh> _mesh;
107  std::shared_ptr<MooseApp> _app;
109  std::shared_ptr<FEProblem> _fe_problem;
110 };
111 
112 template <typename T>
113 T &
114 MooseObjectUnitTest::addObject(const std::string & type,
115  const std::string & name,
116  InputParameters & params)
117 {
118  auto objects = _fe_problem->addObject<T>(type, name, params);
119  mooseAssert(objects.size() == 1, "Doesn't work with threading");
120  return *objects[0];
121 }
std::string name(const ElemQuality q)
std::shared_ptr< FEProblem > _fe_problem
T & addObject(const std::string &type, const std::string &name, InputParameters &params)
std::shared_ptr< MooseApp > _app
Generic factory class for build all sorts of objects.
Definition: Factory.h:28
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition: FEProblem.h:20
MooseObjectUnitTest(const std::string &app_name)
QGAUSS
FIRST
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
std::unique_ptr< MooseMesh > _mesh
std::shared_ptr< MooseObject > create(const std::string &obj_name, const std::string &name, const InputParameters &parameters, THREAD_ID tid=0, bool print_deprecated=true)
Definition: Factory.C:110
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition: Factory.C:67
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Base class for building basic unit tests for MOOSE objects that can live alone (like user objects...
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:88
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
std::shared_ptr< MooseApp > createMooseApp(const std::string &default_app_name, int argc, char *argv[])
Create a MooseApp from command-line arguments.
Definition: MooseMain.C:39
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
std::unique_ptr< MooseObject > createUnique(const std::string &obj_name, const std::string &name, const InputParameters &parameters, THREAD_ID tid=0, bool print_deprecated=true)
Build an object (must be registered) - THIS METHOD IS DEPRECATED (Use create<T>()) ...
Definition: Factory.C:86