Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auxvar parameter optimization #10

Draft
wants to merge 41 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
1568d85
Merge commit '942eade19180e5a04b06fe45e0b251ca5167fec3' into main
Jan 24, 2022
5c40f9b
Merge commit '2503ad7809a4a3144b68090a91c9cb48576f7558' into main
Jan 25, 2022
e3d9149
Merge commit '3c651ebd4da8176335d1437d7e6911b17caa2c05' into main
Jan 25, 2022
56ed23f
Merge commit '9a377c7afa7d40beb3bdcf25ff8842ee70787433' into main
Jan 26, 2022
bc9f17f
Merge commit '3c6fcd3b65fb1c1c98309d6307120dfc1c208e16' into main
Jan 26, 2022
e863549
Merge commit '47c4456522b897f2c8b072297d295ea9590a6650' into main
Jan 27, 2022
9485953
Merge commit '27774a4f1b731d3ce8099ae51c343c3d9879feaa' into main
Jan 27, 2022
0644720
Merge commit '28910901b3986ce3697cc0c08b7b7e878df36309' into main
Jan 27, 2022
106eb7a
Merge commit '30c6d1a92ce2d410ea0ec31577f93bfb4368309d' into main
Jan 28, 2022
f196e73
Merge commit '1d001e550e83ee10773e73f5fbeeae125b1164c1' into main
Jan 30, 2022
365c219
Merge commit 'ae5457427a6def0e14235a28275db6fffa67afd4' into main
Feb 1, 2022
23d4bcb
Merge commit '11f985e9a53cc6d7da3c3a26a5d5a38e05fa9399' into main
Feb 3, 2022
5235053
Merge commit 'b9ff0bc3224e48b03c65264393fb1c5bd3464d1e' into main
Feb 3, 2022
3012317
Merge commit 'd445adc5f93bbe76e829e9fab25fce8fc51b4e90' into main
Feb 4, 2022
190acf1
Merge commit '0f7eafaec1456020a45b59991efe364bd236e78f' into main
Feb 4, 2022
91df636
Merge commit 'b0318b7c754f8f02090ec124f33389ccdcd13c97' into main
Feb 7, 2022
cb58718
Merge commit '3432a55da821b1f622e5fbde64e67c08f02b1116' into main
Feb 8, 2022
2274342
Merge commit '0ef1e96739516a4fc20f5c03bb6c8f83b6bcb38f' into main
Feb 9, 2022
f0374b4
Merge commit '0fd8042169e20865e2242e01fc954aa286d3b804' into main
Feb 10, 2022
61243a4
Merge commit '36e02563ff6fd2c7f4be7688e39ef66a240058fe' into main
Feb 10, 2022
ba1235b
Merge commit '00d677bbee0a25e5a601527931eac54d274235ee' into main
Feb 11, 2022
1019485
Merge commit 'e41a8d6c9152448b4c4f30485eaeb5d4b97df677' into main
Feb 16, 2022
570784c
Merge commit 'bd22f6b7324861fd32b26bd848ffd1abbff1f50e' into main
Feb 17, 2022
db7bd31
Merge commit '94dda8475618b8ff2820609b7ad056833d03eb64' into main
Feb 18, 2022
270b3e7
Merge commit 'eaf19383cde49bb5d5cc4f884caef5e355a72bad' into main
Feb 18, 2022
cb511e0
Merge commit 'da50364f025f11bacb5df7033a8674eb27f3f1b3' into main
Feb 18, 2022
ab75913
Merge commit '83479eac421c6d9c1890c5d811bba5a5e4ccdf23' into main
Feb 19, 2022
0ad4155
Merge commit '10689c5f7d6f46602da405de76d6637f88b80b86' into main
Feb 19, 2022
2a43951
Merge commit 'eb207037cd8337e1686f1624e995838422e93049' into main
Feb 21, 2022
32e7658
Merge commit 'd3ee5c58f5e202f428c6d74ec597f8690edbeae3' into main
Feb 23, 2022
d51e42d
Merge commit 'b197012f3d892dbc0c11cb9fce26ef673380c80e' into main
Feb 23, 2022
6c201df
Merge commit '5b513e4bb55ec676eaed86a61a3bf7467bb5bd03' into main
Feb 23, 2022
a8defe8
Merge commit '02b3dd9546dc8369cfa1fd13310d19504c20218d' into main
Feb 23, 2022
819ea47
Merge commit '98a2bf2df1eefa98b6acc8b870a9a67dd2e038ed' into main
Feb 24, 2022
3ef0388
Merge commit '5d6e49e3a6d4b553f3ca491f59a9215b02d0b113' into main
Feb 24, 2022
f1db4cc
Merge commit '142290ed73261e86a49e46db6a42b1e72b473ae8' into main
Feb 24, 2022
fb4ae47
Merge commit '3572be57a6ed83b192b3c1ffaf67e7d91552450a' into main
Feb 25, 2022
1f2bfcf
Merge commit '081563b3a04a213f1565f89cfb16e75ff07e116a' into main
Feb 26, 2022
62ab302
Add OptimizationReporterBase class and refactor
dewenyushu Feb 28, 2022
021466b
Add positions in multiapp and examples
dewenyushu Mar 1, 2022
0f1be34
Initial implementation of VariableOptimizationReporter
dewenyushu Mar 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add OptimizationReporterBase class and refactor
dewenyushu committed Feb 28, 2022
commit 62ab302f4e793b5fd1f99ca0edd3d5516b2e98ee
8 changes: 4 additions & 4 deletions include/executioners/OptimizeSolve.h
Original file line number Diff line number Diff line change
@@ -2,13 +2,13 @@

#include "SolveObject.h"

#include "OptimizationReporter.h"
#include "OptimizationReporterBase.h"
#include "ExecFlagEnum.h"
#include <petsctao.h>
#include "libmesh/petsc_vector.h"
#include "libmesh/petsc_matrix.h"

class OptimizationReporter;
class OptimizationReporterBase;

class OptimizeSolve : public SolveObject
{
@@ -18,7 +18,7 @@ class OptimizeSolve : public SolveObject

virtual bool solve() override;

const OptimizationReporter & getOptimizationReporter() const { return *_form_function; }
const OptimizationReporterBase & getOptimizationReporterBase() const { return *_form_function; }

// fixme lynn I wish this were all a struct
void getTaoSolutionStatus(std::vector<int> & tot_iters,
@@ -50,7 +50,7 @@ class OptimizeSolve : public SolveObject
const ExecFlagEnum & _solve_on;

/// Form function defining objective, gradient, and hessian
OptimizationReporter * _form_function = nullptr;
OptimizationReporterBase * _form_function = nullptr;

/// Tao optimization object
Tao _tao;
52 changes: 10 additions & 42 deletions include/optimizationreporters/OptimizationReporter.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
#pragma once

#include "OptimizationData.h"
#include "OptimizationReporterBase.h"

#include "OptimizeSolve.h"
#include "libmesh/petsc_vector.h"
#include "libmesh/petsc_matrix.h"
#include "DataIO.h"

class OptimizationReporter : public OptimizationData
class OptimizationReporter : public OptimizationReporterBase
{
public:
static InputParameters validParams();
@@ -21,47 +16,29 @@ class OptimizationReporter : public OptimizationData
* Function to initialize petsc vectors from vpp data
* FIXME: this should be const
*/
void setInitialCondition(libMesh::PetscVector<Number> & param);
void setInitialCondition(libMesh::PetscVector<Number> & param) override;

/**
* Functions to get and check bounds
*/
bool hasBounds() const { return _upper_bounds.size() > 0 && _lower_bounds.size() > 0; }
const std::vector<Real> & getUpperBounds() const { return _upper_bounds; };
const std::vector<Real> & getLowerBounds() const { return _lower_bounds; };
bool hasBounds() const override { return _upper_bounds.size() > 0 && _lower_bounds.size() > 0; }
const std::vector<Real> & getUpperBounds() const override { return _upper_bounds; };
const std::vector<Real> & getLowerBounds() const override { return _lower_bounds; };
/**
* Function to compute default bounds when user did not provide bounds
*/
virtual std::vector<Real> computeDefaultBounds(Real val);
std::vector<Real> computeDefaultBounds(Real val) override;

/**
* Function to compute objective and handle a failed solve.
* This is the last function called in objective routine
*/
virtual Real computeAndCheckObjective(bool solver_converged);

/**
* Function to compute gradient.
* This is the last call of the gradient routine.
*/
virtual void computeGradient(libMesh::PetscVector<Number> & /*gradient*/)
{
mooseError("Gradient function has not been defined for form function type ", _type);
}

/**
* Function to compute gradient.
* This is the last call of the hessian routine.
*/
virtual void computeHessian(libMesh::PetscMatrix<Number> & /*hessian*/)
{
mooseError("Hessian function has not been defined for form function type ", _type);
}
Real computeAndCheckObjective(bool solver_converged) override;

/**
* Function to get the total number of parameters
*/
unsigned int getNumParams() { return _ndof; };
unsigned int getNumParams() override { return _ndof; };

protected:
/// Parameter names
@@ -80,18 +57,9 @@ class OptimizationReporter : public OptimizationData
const std::vector<Real> & _lower_bounds;
const std::vector<Real> & _upper_bounds;

/**
* Function to compute objective.
* This is the last function called in objective routine
*/
Real computeObjective();

/**
* Function to set parameters.
* This is the first function called in objective/gradient/hessian routine
*/
virtual void updateParameters(const libMesh::PetscVector<Number> & x);

private:
friend class OptimizeSolve;
virtual void updateParameters(const libMesh::PetscVector<Number> & x) override;
};
90 changes: 90 additions & 0 deletions include/optimizationreporters/OptimizationReporterBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//* 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

#pragma once

#include "GeneralReporter.h"
#include "OptimizationData.h"
#include "libmesh/petsc_vector.h"
#include "libmesh/petsc_matrix.h"
#include "DataIO.h"

class OptimizationReporterBase : public OptimizationData
{
public:
static InputParameters validParams();
OptimizationReporterBase(const InputParameters & parameters);

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

/**
* Function to initialize petsc vectors from vpp data
* FIXME: this should be const
*/
virtual void setInitialCondition(libMesh::PetscVector<Number> & param) = 0;

/**
* Functions to get and check bounds
*/
virtual bool hasBounds() const = 0;
virtual const std::vector<Real> & getUpperBounds() const = 0;
virtual const std::vector<Real> & getLowerBounds() const = 0;

/**
* Function to compute default bounds when user did not provide bounds
*/
virtual std::vector<Real> computeDefaultBounds(Real val) = 0;

/**
* Function to compute objective and handle a failed solve.
* This is the last function called in objective routine
*/
virtual Real computeAndCheckObjective(bool solver_converged) = 0;

/**
* Function to compute gradient.
* This is the last call of the gradient routine.
*/
virtual void computeGradient(libMesh::PetscVector<Number> & /*gradient*/)
{
mooseError("Gradient function has not been defined for form function type ", _type);
}

/**
* Function to compute gradient.
* This is the last call of the hessian routine.
*/
virtual void computeHessian(libMesh::PetscMatrix<Number> & /*hessian*/)
{
mooseError("Hessian function has not been defined for form function type ", _type);
}

/**
* Function to get the total number of parameters
*/
virtual unsigned int getNumParams() = 0;

protected:
/**
* Function to compute objective.
* This is the last function called in objective routine
*/
Real computeObjective();

/**
* Function to set parameters.
* This is the first function called in objective/gradient/hessian routine
*/
virtual void updateParameters(const libMesh::PetscVector<Number> & x) = 0;

private:
friend class OptimizeSolve;
};
2 changes: 1 addition & 1 deletion src/executioners/OptimizeSolve.C
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ OptimizeSolve::solve()
// Grab form function
if (!_problem.hasUserObject("OptimizationReporter"))
mooseError("No form function object found.");
_form_function = &_problem.getUserObject<OptimizationReporter>("OptimizationReporter");
_form_function = &_problem.getUserObject<OptimizationReporterBase>("OptimizationReporter");

// Initialize solution and matrix
_form_function->setInitialCondition(*_parameters.get());
19 changes: 2 additions & 17 deletions src/optimizationreporters/OptimizationReporter.C
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
InputParameters
OptimizationReporter::validParams()
{
InputParameters params = OptimizationData::validParams();
InputParameters params = OptimizationReporterBase::validParams();
params.addClassDescription("Base class for optimization reporter communication.");
params.addRequiredParam<std::vector<ReporterValueName>>(
"parameter_names", "List of parameter names, one for each group of parameters.");
@@ -26,7 +26,7 @@ OptimizationReporter::validParams()
}

OptimizationReporter::OptimizationReporter(const InputParameters & parameters)
: OptimizationData(parameters),
: OptimizationReporterBase(parameters),
_parameter_names(getParam<std::vector<ReporterValueName>>("parameter_names")),
_nparam(_parameter_names.size()),
_nvalues(getParam<std::vector<dof_id_type>>("num_values")),
@@ -106,18 +106,3 @@ OptimizationReporter::computeAndCheckObjective(bool multiapp_passed)
mooseError("Forward solve multiapp failed!");
return computeObjective();
}

Real
OptimizationReporter::computeObjective()
{
for (size_t i = 0; i < _measurement_values.size(); ++i)
_misfit_values[i] = _simulation_values[i] - _measurement_values[i];

Real val = 0;
for (auto & misfit : _misfit_values)
val += misfit * misfit;

val = 0.5 * val;

return val;
}
39 changes: 39 additions & 0 deletions src/optimizationreporters/OptimizationReporterBase.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//* 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 "OptimizationReporterBase.h"

InputParameters
OptimizationReporterBase::validParams()
{
InputParameters params = OptimizationData::validParams();
params.addClassDescription(
"Reporter to hold measurement and simulation data for optimization problems");
return params;
}

OptimizationReporterBase::OptimizationReporterBase(const InputParameters & parameters)
: OptimizationData(parameters)
{
}

Real
OptimizationReporterBase::computeObjective()
{
for (size_t i = 0; i < _measurement_values.size(); ++i)
_misfit_values[i] = _simulation_values[i] - _measurement_values[i];

Real val = 0;
for (auto & misfit : _misfit_values)
val += misfit * misfit;

val = 0.5 * val;

return val;
}