Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrsd committed Jul 19, 2018
1 parent 66eb1b8 commit 2986d2e
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/include/postprocessors/PrimeProductPostprocessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#ifndef PRIMEPRODUCTPOSTPROCESSOR_H
#define PRIMEPRODUCTPOSTPROCESSOR_H

#include "GeneralPostprocessor.h"

class PrimeProductPostprocessor;
class PrimeProductUserObject;

template <>
InputParameters validParams<PrimeProductPostprocessor>();

/**
* Get the value of proproduct from PrimeProductUserObject
*/
class PrimeProductPostprocessor : public GeneralPostprocessor
{
public:
PrimeProductPostprocessor(const InputParameters & parameters);

virtual void initialize() override {}
virtual void execute() override {}

virtual Real getValue() override;

protected:
const PrimeProductUserObject & _prime_product;
};

#endif // PRIMEPRODUCTPOSTPROCESSOR_H
48 changes: 48 additions & 0 deletions test/include/userobjects/PrimeProductUserObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#ifndef PRIMECOUNTERUSEROBJECT_H
#define PRIMECOUNTERUSEROBJECT_H

#include "GeneralUserObject.h"

class PrimeProductUserObject;

template <>
InputParameters validParams<PrimeProductUserObject>();

/**
* Computes a product of prime numbers. Each threads picks an n-th prime number where n is equal to
* its thread ID. Then when threads join the product of all prime numbers is computed, which is the
* final value provided by this user object.
*/
class PrimeProductUserObject : public GeneralUserObject
{
public:
PrimeProductUserObject(const InputParameters & params);

virtual void initialize() override;
virtual void execute() override;
virtual void finalize() override;

virtual void threadJoin(const UserObject &) override;

unsigned int getProduct() const { return _product; }

protected:
unsigned int factorial(unsigned int n) const;
unsigned int prime(unsigned int n) const;

/// Prime number of this instance
unsigned int _prime;
/// Product for all prime numbers
unsigned int _product;
};

#endif /* PRIMECOUNTERUSEROBJECT_H */
34 changes: 34 additions & 0 deletions test/src/postprocessors/PrimeProductPostprocessor.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "PrimeProductPostprocessor.h"
#include "PrimeProductUserObject.h"

registerMooseObject("MooseTestApp", PrimeProductPostprocessor);

template <>
InputParameters
validParams<PrimeProductPostprocessor>()
{
InputParameters params = validParams<GeneralPostprocessor>();
params.addRequiredParam<UserObjectName>("prime_product", "The name of the user object that holds the prime product");
return params;
}

PrimeProductPostprocessor::PrimeProductPostprocessor(const InputParameters & parameters)
: GeneralPostprocessor(parameters),
_prime_product(getUserObject<PrimeProductUserObject>("prime_product"))
{
}

Real
PrimeProductPostprocessor::getValue()
{
return _prime_product.getProduct();
}
55 changes: 55 additions & 0 deletions test/src/userobjects/PrimeProductUserObject.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "PrimeProductUserObject.h"
#include <unistd.h>

registerMooseObject("MooseTestApp", PrimeProductUserObject);

template <>
InputParameters
validParams<PrimeProductUserObject>()
{
InputParameters params = validParams<GeneralUserObject>();
return params;
}

PrimeProductUserObject::PrimeProductUserObject(const InputParameters & params)
: GeneralUserObject(params)
{
if (libMesh::n_threads() > 20)
mooseError("This object works only with up to 20 threads. If you need more, add more prime "
"numbers into `prime` variable in the execute method.");
}

void
PrimeProductUserObject::initialize()
{
}

void
PrimeProductUserObject::execute()
{
// first 20 prime numbers
static unsigned int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71};
_product = primes[_tid];
}

void
PrimeProductUserObject::finalize()
{
}

void
PrimeProductUserObject::threadJoin(const UserObject & uo)
{
const PrimeProductUserObject & pcuo = dynamic_cast<const PrimeProductUserObject &>(uo);
_product *= pcuo._product;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
time,product
0,0
1,2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
time,product
0,0
1,6
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
time,product
0,0
1,30
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
time,product
0,0
1,210
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
time,product
0,0
1,2310
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
time,product
0,0
1,30030
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
time,product
0,0
1,510510
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
time,product
0,0
1,9699690
49 changes: 49 additions & 0 deletions test/tests/userobjects/threaded_general_user_object/test.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[Mesh]
type = GeneratedMesh
dim = 1
nx = 2
[]

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

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

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

[Executioner]
type = Steady
[]

[UserObjects]
[./prime_product]
type = PrimeProductUserObject
threaded = true
execute_on = timestep_end
[../]
[]

[Postprocessors]
[./product]
type = PrimeProductPostprocessor
prime_product = prime_product
[../]
[]

[Outputs]
csv = true
[]
73 changes: 73 additions & 0 deletions test/tests/userobjects/threaded_general_user_object/tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
[Tests]
[./th1]
type = 'CSVDiff'
input = 'test.i'
cli_args = 'Outputs/file_base=th1'
min_threads = 1
max_threads = 1
csvdiff = 'th1.csv'
[../]

[./th2]
type = 'CSVDiff'
input = 'test.i'
cli_args = 'Outputs/file_base=th2'
min_threads = 2
max_threads = 2
csvdiff = 'th2.csv'
[../]

[./th3]
type = 'CSVDiff'
input = 'test.i'
cli_args = 'Outputs/file_base=th3'
min_threads = 3
max_threads = 3
csvdiff = 'th3.csv'
[../]

[./th4]
type = 'CSVDiff'
input = 'test.i'
cli_args = 'Outputs/file_base=th4'
min_threads = 4
max_threads = 4
csvdiff = 'th4.csv'
[../]

[./th5]
type = 'CSVDiff'
input = 'test.i'
cli_args = 'Outputs/file_base=th5'
min_threads = 5
max_threads = 5
csvdiff = 'th5.csv'
[../]

[./th6]
type = 'CSVDiff'
input = 'test.i'
cli_args = 'Outputs/file_base=th6'
min_threads = 6
max_threads = 6
csvdiff = 'th6.csv'
[../]

[./th7]
type = 'CSVDiff'
input = 'test.i'
cli_args = 'Outputs/file_base=th7'
min_threads = 7
max_threads = 7
csvdiff = 'th7.csv'
[../]

[./th8]
type = 'CSVDiff'
input = 'test.i'
cli_args = 'Outputs/file_base=th8'
min_threads = 8
max_threads = 8
csvdiff = 'th8.csv'
[../]
[]

0 comments on commit 2986d2e

Please sign in to comment.