11 #ifndef _LSPROBLEMIMPLEM_H_ 12 #define _LSPROBLEMIMPLEM_H_ 14 #if defined(CH_Darwin) && defined(__GNUC__) && ( __GNUC__ == 3 ) 17 #define _GLIBCPP_USE_C99 1 34 #include "NamespaceHeader.H" 39 if (a_r == 0)
return 1;
42 for (
int i = 1; i <= a_r; ++i)
66 for (
int idir = 0; idir <
dim; ++idir)
68 corner[idir] = vertex[idir] - 0.5;
76 Real monoCorner = 1.0;
77 for (
int idir = 0; idir <
dim ; ++idir)
79 monoCorner *=
POW(cornerCoord[idir],a_mono[idir]);
82 if (monoCorner > a_maxVal)
84 a_maxVal = monoCorner;
87 if (monoCorner < a_minVal)
89 a_minVal = monoCorner;
97 m_lowerBound.assign(-HUGE_VAL);
98 m_upperBound.assign(HUGE_VAL);
101 if (m_degreeP == 0 &&
dim == 2)
110 if (m_degreeP == 0 &&
dim == 2)
117 IvDim mono = it->first;
120 monoMaxMin(maxMono,minMono,mono,a_ccm.
m_IFData);
121 Real val = it->second;
124 Real newLoBnd = val/minMono;
125 if (newLoBnd > lobnd)
133 Real newLoBnd = val/maxMono;
134 if (newLoBnd > lobnd)
144 IvDim mono = IvDim::Zero;
147 int variableNdx = ndx;
148 m_lowerBound[variableNdx]=lobnd;
149 m_upperBound[variableNdx]=HUGE_VAL;
154 for (
typename PthMomentLoc::const_iterator it = m_monoLocPLess1.begin();
155 it != m_monoLocPLess1.end();++it)
157 IvDim mono = it->first;
160 int ndx = it->second;
161 int variableNdx = ndx + m_numP;
167 momentBounds(lobnd,hibnd,mono, a_ccm.
m_IFData);
171 bool isZeroDegree = (mono.
sum() == 0);
175 Real zeroDegreeLoBnd = 0.0;
186 IvDim mono = it2->first;
187 Real val = it2->second;
190 monoMaxMin(maxMono,minMono,mono,a_ccm.
m_IFData);
193 Real newLoBnd = val/minMono;
194 if (newLoBnd > zeroDegreeLoBnd)
196 zeroDegreeLoBnd = newLoBnd;
202 Real newLoBnd = val/maxMono;
203 if (newLoBnd > zeroDegreeLoBnd)
205 zeroDegreeLoBnd = newLoBnd;
210 lobnd =
Max(lobnd, zeroDegreeLoBnd);
214 m_lowerBound[variableNdx] = lobnd;
215 m_upperBound[variableNdx] = hibnd;
225 const IvDim & a_mono,
241 for (
int idir = 0; idir <
dim; ++idir)
243 corner[idir] = vertex[idir] - 0.5;
251 Real partialCellMag = 1.0;
252 for (
int idir = 0; idir <
dim ; ++idir)
254 Real pPlus1 =(
Real)( a_mono[idir]+1);
255 partialCellMag *=
POW(cornerCoord[idir],pPlus1)/pPlus1;
256 if (cornerCoord[idir] < 0.0) partialCellMag *=-1.0;
258 if (partialCellMag > 0.0) a_hibnd += partialCellMag;
259 if (partialCellMag < 0.0) a_lobnd += partialCellMag;
265 if (m_matrix != NULL)
268 int numRows =
dim*m_numP;
269 int numCols = m_numP + m_numPLess1;
270 freeArray(numRows,numCols,m_matrix);
277 const bool & a_useConstraints)
278 :m_degreeP(a_degreeP),
279 m_numActiveBounds(0),
280 m_useConstraints(a_useConstraints)
291 const int & a_degreeP,
292 const bool & a_useConstraints,
309 if (a_useConstraints)
344 pout() <<
"Singular LS problem. Linearly dependent columns (zero normal?)" << endl;
347 pout() <<
"Conflicting upper/lower bounds in LS problem. Zero or bad normal?" << endl;
352 pout() <<
"BVLS failed to converge properly" << endl;
377 a_residual[2] += ri * ri;
379 a_residual[2] = sqrt(a_residual[2]);
388 if (a_n < 0 || a_m < 0)
392 for (
int i = a_m+1; i <= a_n; ++i)
402 if (a_monoDegree == -1)
410 if (
dim- 1 > a_monoDegree)
413 smaller = a_monoDegree;
418 bigger = a_monoDegree;
420 int numerator =
factorial(
dim - 1 + a_monoDegree,bigger);
424 retval = numerator/denominator;
442 for (
typename PthMomentLoc::const_iterator it =
m_monoLocP.begin();
445 for (
int idir = 0; idir<
dim; ++idir)
448 int row = dim*(it->second) + idir;
449 int pcol = it->second;
454 IvDim mono = it->first;
484 const IvDim & a_mono)
486 if (a_mono[a_idir] > 0)
488 a_coeff = a_mono[a_idir];
490 a_Dmono[a_idir] -= 1;
492 else if (a_mono[a_idir] == 0)
495 for (
int idir = 0; idir <
dim; ++idir)
512 const int & a_degree)
520 for (
int i = 0; i < monomials.
size(); i++)
522 const IvDim & monomial = monomials[i];
524 a_monoLoc[monomial] = i;
525 a_locMono[i] = monomial;
534 a_A =
new Real* [a_rows];
536 for (
int i = 0; i < a_rows; i++)
538 a_A[i] =
new Real [a_cols];
540 Real* scanA = a_A[i];
541 for (
int j = 0; j < a_cols; j++)
552 for (
int i = 0; i < a_rows; i++)
562 a_out <<
"Dim = " <<
dim <<
", degree = " <<
m_degreeP <<
'\n';
563 a_out <<
"m_monoLocP has " <<
m_monoLocP.size() <<
" elements" <<
'\n';
565 for (
typename PthMomentLoc::const_iterator it =
m_monoLocP.begin();
568 a_out <<
"Monomial = " << it->first <<
", Loc = " << it->second <<
'\n';
571 a_out <<
"Dim = " <<
dim <<
'\n';
572 a_out <<
"m_locMonoP has " <<
m_locMonoP.size() <<
" elements" <<
'\n';
574 for (
typename LocPthMoment::const_iterator it =
m_locMonoP.begin();
577 a_out <<
"Loc = " << it->first <<
", Monomial = " << it->second <<
'\n';
580 a_out <<
"m_locMonoPLess1 has " <<
m_locMonoPLess1.size() <<
" elements" <<
'\n';
581 for (
typename PthMomentLoc::const_iterator it =
m_monoLocPLess1.begin();
584 a_out <<
"Monomial = " << it->first <<
", Loc = " << it->second <<
'\n';
587 a_out <<
"m_locMonoPLess1 has " <<
m_locMonoPLess1.size() <<
" elements" <<
'\n';
589 for (
typename LocPthMoment::const_iterator it =
m_locMonoPLess1.begin();
592 a_out <<
"Loc = " << it->first <<
", Monomial = " << it->second <<
'\n';
595 a_out <<
"Matrix and rhs for least squares problem of dim = " << dim << endl;
605 a_lsProblem.
print(a_out);
613 pout() <<
"numRows = " << rows <<
", numCols = " << cols << endl;
615 for (
int i = 0; i < rows; i++)
617 for (
int j = 0; j < cols; j++)
627 pout() <<
"Outputting Rhs" << endl;
630 pout() <<
"m_rhs[" << i <<
"] = " <<
m_rhs[i] << endl;
636 pout() <<
"Outputting Unknowns" << endl;
647 pout() <<
"Outputting Lower/Upper bounds" << endl;
650 pout() <<
"m_lowerBound[" << setw(2) << i <<
"] = " << setw(14) <<
m_lowerBound[i]
651 <<
" m_upperBound[" << setw(2) << i <<
"] = " << setw(14) <<
m_upperBound[i] << endl;
656 pout() <<
"Problem is unconstrained" << endl;
660 #include "NamespaceFooter.H" std::ostream & pout()
Use this in place of std::cout for program output.
void computeBounds(const IndexTM< Real, dim > &a_dx, const CutCellMoments< dim > &a_ccm)
Definition: LSProblemImplem.H:94
void differentiate(int &a_coeff, IvDim &a_Dmono, int &a_idir, const IvDim &a_mono)
Definition: LSProblemImplem.H:481
void print(ostream &a_out) const
Definition: LSProblemImplem.H:560
LSResult solveBoundConstrained(Vector< Real > &a_x, Real **a_A, const Vector< Real > &a_rhs, const Vector< Real > &a_lowerBound, const Vector< Real > &a_upperBound)
int m_numP
Definition: LSProblem.H:186
Definition: ConstrainedLS.H:32
bool m_useConstraints
Definition: LSProblem.H:176
#define LARGEINTVAL
Definition: Notation.H:76
int numberActiveConstraints() const
void outputRhs() const
Definition: LSProblemImplem.H:625
void outputMatrix() const
Definition: LSProblemImplem.H:609
Vector< Real > m_rhs
Definition: LSProblem.H:199
void outputBounds() const
Definition: LSProblemImplem.H:643
int numMonomials(const int &a_monoDegree)
Definition: LSProblemImplem.H:399
Definition: ConstrainedLS.H:33
Definition: ComputeCutCellMoments.H:35
Real POW(const Real &a_x, const int &a_p)
computes x^p
Definition: Factorial.H:33
void momentBounds(Real &a_lobnd, Real &a_hibnd, const IvDim &a_mono, const IFData< dim > &a_ifData)
Definition: LSProblemImplem.H:223
LSProblem(const int &a_degreeP, const bool &a_useConstraints)
Definition: LSProblemImplem.H:276
Definition: ConstrainedLS.H:35
PthMomentLoc m_monoLocPLess1
Definition: LSProblem.H:189
LSResult
Definition: ConstrainedLS.H:29
void outputUnknowns() const
Definition: LSProblemImplem.H:634
CoordinateSystem< dim > m_localCoord
Definition: IFData.H:65
void resize(unsigned int isize)
Definition: Vector.H:346
Definition: ConstrainedLS.H:31
~LSProblem()
Definition: LSProblemImplem.H:263
T sum() const
Definition: IndexTMI.H:260
void fillMap(PthMomentLoc &a_monoLoc, LocPthMoment &a_locMono, const int &a_degree)
Definition: LSProblemImplem.H:510
int factorial(const int &a_n, const int &a_m=0)
Definition: LSProblemImplem.H:384
map< int, IvDim > LocPthMoment
Definition: LSProblem.H:33
Definition: LSquares.H:22
IFData< dim > m_IFData
Definition: CutCellMoments.H:141
int nChooseR(int a_n, int a_r)
Definition: LSProblemImplem.H:36
double Real
Definition: REAL.H:33
int invertNormalEq(const Vector< Real > &a_rhs, Vector< Real > &a_residual)
Definition: LSProblemImplem.H:317
T Abs(const T &a_a)
Definition: Misc.H:53
void monoMaxMin(Real &a_maxVal, Real &a_minVal, const IndexTM< int, dim > &a_mono, const IFData< dim > &a_IFData)
Definition: LSProblemImplem.H:50
size_t size() const
Definition: Vector.H:192
PthMomentLoc m_monoLocP
Definition: LSProblem.H:182
void setRhs(const Vector< Real > &a_rhs)
Definition: LSProblemImplem.H:506
static void Error(const char *const a_msg=m_nullString, int m_exitCode=CH_DEFAULT_ERROR_CODE)
Print out message to cerr and exit with the specified exit code.
int m_order
Definition: LSProblem.H:167
CoordinateSystem< dim > m_cellCenterCoord
Definition: IFData.H:63
ostream & operator<<(ostream &a_out, LSProblem< dim > &a_lsProblem)
Definition: LSProblemImplem.H:602
int m_numPLess1
Definition: LSProblem.H:193
PthMoment m_moments
Definition: CutCellMoments.H:132
Real ** m_matrix
Definition: LSProblem.H:197
LocPthMoment m_locMonoP
Definition: LSProblem.H:183
CornerSigns m_cornerSigns
Definition: IFData.H:58
map< IvDim, int > PthMomentLoc
Definition: LSProblem.H:32
int m_numActiveBounds
Definition: LSProblem.H:173
int m_degreeP
Definition: LSProblem.H:170
Vector< Real > m_upperBound
Definition: LSProblem.H:202
T Max(const T &a_a, const T &a_b)
Definition: Misc.H:39
PthMoment m_EBmoments
Definition: CutCellMoments.H:135
void allocArray(const int &a_rows, const int &a_cols, Real **&a_A)
Definition: LSProblemImplem.H:530
Definition: ConstrainedLS.H:19
void generateMultiIndices(Vector< IndexTM< int, dim > > &a_indices, const int &a_magnitude)
Definition: MultiIndexImplem.H:18
void LeastSquares(Real **A, Vector< Real > &x, const Vector< Real > &rhs)
void freeArray(const int &a_rows, const int &a_cols, Real **&a_A)
Definition: LSProblemImplem.H:548
int dim
Definition: EBInterface.H:146
void setMatrix()
Definition: LSProblemImplem.H:430
Definition: CutCellMoments.H:32
Definition: ConstrainedLS.H:34
Vector< Real > m_unknowns
Definition: LSProblem.H:198
IndexTM< Real, dim > m_normal
Definition: LSProblem.H:179
Vector< Real > m_lowerBound
Definition: LSProblem.H:201
LocPthMoment m_locMonoPLess1
Definition: LSProblem.H:190
static void Abort(const char *const a_msg=m_nullString)
Print out message to cerr and exit via abort() (if serial) or MPI_Abort() (if parallel).