| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// LOCA: Library of Continuation Algorithms Package |
| 4 |
// |
| 5 |
// Copyright 2001-2005 NTESS and the LOCA contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#include "LOCALinearConstraint.H" |
| 11 |
#include "LOCA_Parameter_Vector.H" |
| 12 |
|
| 13 |
LinearConstraint::LinearConstraint(int NumConstraints, |
| 14 |
const LOCA::ParameterVector& pVec, |
| 15 |
NOX::Abstract::Vector& cloneVec) : |
| 16 |
m(NumConstraints), |
| 17 |
constraints(m,1), |
| 18 |
isValidConstraints(false), |
| 19 |
x(), |
| 20 |
dgdx(), |
| 21 |
isZeroDgDx(false), |
| 22 |
p(pVec), |
| 23 |
pvec(Teuchos::View, p.getDoubleArrayPointer(), p.length(), p.length(), 1), |
| 24 |
dgdp(NumConstraints, pVec.length()) |
| 25 |
{ |
| 26 |
constraints.putScalar(0.0); |
| 27 |
x = cloneVec.createMultiVector(1); |
| 28 |
dgdx = cloneVec.createMultiVector(m); |
| 29 |
dgdp.putScalar(0.0); |
| 30 |
} |
| 31 |
|
| 32 |
LinearConstraint::LinearConstraint(const LinearConstraint& source, |
| 33 |
NOX::CopyType type) : |
| 34 |
m(source.m), |
| 35 |
constraints(source.constraints), |
| 36 |
isValidConstraints(false), |
| 37 |
x(source.x->clone(type)), |
| 38 |
dgdx(source.dgdx->clone(type)), |
| 39 |
isZeroDgDx(source.isZeroDgDx), |
| 40 |
p(source.p), |
| 41 |
pvec(source.pvec), |
| 42 |
dgdp(source.dgdp) |
| 43 |
{ |
| 44 |
if (source.isValidConstraints && type == NOX::DeepCopy) |
| 45 |
isValidConstraints = true; |
| 46 |
} |
| 47 |
|
| 48 |
LinearConstraint::~LinearConstraint() |
| 49 |
{ |
| 50 |
} |
| 51 |
|
| 52 |
void |
| 53 |
LinearConstraint::copy(const LOCA::MultiContinuation::ConstraintInterface& src) |
| 54 |
{ |
| 55 |
const LinearConstraint& source = dynamic_cast<const LinearConstraint&>(src); |
| 56 |
|
| 57 |
if (this != &source) { |
| 58 |
m = source.m; |
| 59 |
constraints = source.constraints; |
| 60 |
isValidConstraints = source.isValidConstraints; |
| 61 |
*x = *source.x; |
| 62 |
*dgdx = *source.dgdx; |
| 63 |
isZeroDgDx = source.isZeroDgDx; |
| 64 |
p = source.p; |
| 65 |
pvec.assign(source.pvec); |
| 66 |
dgdp = source.dgdp; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
Teuchos::RCP<LOCA::MultiContinuation::ConstraintInterface> |
| 71 |
LinearConstraint::clone(NOX::CopyType type) const |
| 72 |
{ |
| 73 |
return Teuchos::rcp(new LinearConstraint(*this, type)); |
| 74 |
} |
| 75 |
|
| 76 |
int |
| 77 |
LinearConstraint::numConstraints() const |
| 78 |
{ |
| 79 |
return m; |
| 80 |
} |
| 81 |
|
| 82 |
void |
| 83 |
LinearConstraint::setX(const NOX::Abstract::Vector& y) |
| 84 |
{ |
| 85 |
(*x)[0] = y; |
| 86 |
isValidConstraints = false; |
| 87 |
} |
| 88 |
|
| 89 |
void |
| 90 |
LinearConstraint::setParam(int paramID, double val) |
| 91 |
{ |
| 92 |
p[paramID] = val; |
| 93 |
} |
| 94 |
|
| 95 |
void |
| 96 |
LinearConstraint::setParams( |
| 97 |
const std::vector<int>& paramIDs, |
| 98 |
const NOX::Abstract::MultiVector::DenseMatrix& vals) |
| 99 |
{ |
| 100 |
for (unsigned int i=0; i<paramIDs.size(); i++) |
| 101 |
p[paramIDs[i]] = vals(i,0); |
| 102 |
} |
| 103 |
|
| 104 |
NOX::Abstract::Group::ReturnType |
| 105 |
LinearConstraint::computeConstraints() |
| 106 |
{ |
| 107 |
// compute dg/dx*x + dg/dp*p |
| 108 |
x->multiply(1.0, *dgdx, constraints); |
| 109 |
if (pvec.numRows() > 0) |
| 110 |
constraints.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, dgdp, |
| 111 |
pvec, 1.0); |
| 112 |
isValidConstraints = true; |
| 113 |
|
| 114 |
return NOX::Abstract::Group::Ok; |
| 115 |
} |
| 116 |
|
| 117 |
NOX::Abstract::Group::ReturnType |
| 118 |
LinearConstraint::computeDX() |
| 119 |
{ |
| 120 |
return NOX::Abstract::Group::Ok; |
| 121 |
} |
| 122 |
|
| 123 |
NOX::Abstract::Group::ReturnType |
| 124 |
LinearConstraint::computeDP(const std::vector<int>& paramIDs, |
| 125 |
NOX::Abstract::MultiVector::DenseMatrix& dp, |
| 126 |
bool isValidG) |
| 127 |
{ |
| 128 |
if (!isValidG) { |
| 129 |
for (int i=0; i<m; i++) |
| 130 |
dp(i,0) = constraints(i,0); |
| 131 |
} |
| 132 |
|
| 133 |
for (unsigned int i=0; i<paramIDs.size(); i++) { |
| 134 |
for (int j=0; j<m; j++) |
| 135 |
dp(j,i+1) = dgdp(j,paramIDs[i]); |
| 136 |
} |
| 137 |
|
| 138 |
return NOX::Abstract::Group::Ok; |
| 139 |
} |
| 140 |
|
| 141 |
bool |
| 142 |
LinearConstraint::isConstraints() const |
| 143 |
{ |
| 144 |
return isValidConstraints; |
| 145 |
} |
| 146 |
|
| 147 |
bool |
| 148 |
LinearConstraint::isDX() const |
| 149 |
{ |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
const NOX::Abstract::MultiVector::DenseMatrix& |
| 154 |
LinearConstraint::getConstraints() const |
| 155 |
{ |
| 156 |
return constraints; |
| 157 |
} |
| 158 |
|
| 159 |
const NOX::Abstract::MultiVector* |
| 160 |
LinearConstraint::getDX() const |
| 161 |
{ |
| 162 |
return dgdx.get(); |
| 163 |
} |
| 164 |
|
| 165 |
bool |
| 166 |
LinearConstraint::isDXZero() const |
| 167 |
{ |
| 168 |
return isZeroDgDx; |
| 169 |
} |
| 170 |
|
| 171 |
void |
| 172 |
LinearConstraint::setDgDx(const NOX::Abstract::MultiVector& A) |
| 173 |
{ |
| 174 |
*dgdx = A; |
| 175 |
isValidConstraints = false; |
| 176 |
} |
| 177 |
|
| 178 |
void |
| 179 |
LinearConstraint::setDgDp(const NOX::Abstract::MultiVector::DenseMatrix& A) |
| 180 |
{ |
| 181 |
dgdp.assign(A); |
| 182 |
isValidConstraints = false; |
| 183 |
} |
| 184 |
|
| 185 |
void |
| 186 |
LinearConstraint::setIsZeroDX(bool flag) |
| 187 |
{ |
| 188 |
isZeroDgDx = flag; |
| 189 |
} |