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

optimization-based reshape operators #309

Draft
wants to merge 50 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
81da27f
First commit for EdgeOptim Operator.
May 23, 2019
0c0fd07
Optimization based edge reshape included for order less than 3.
May 29, 2019
fe25096
edge reshape only.
Jun 9, 2019
9be8ef8
face nodes included in the optimizer.
Jun 19, 2019
535ac22
included statndalone face repositioning.
Jun 20, 2019
a09a287
optim wo adapt
Jul 27, 2019
08e8c13
edge optimization based on all invaliidities.
Aug 2, 2019
98eeaf3
Debugging on Avinash's code
mortezah Nov 6, 2019
a171c42
Debugging on Avinash's code day 2
mortezah Nov 7, 2019
7acc845
Debugging Day-3
Nov 9, 2019
23bc9d5
Debug edge optim day 4
mortezah Nov 9, 2019
f55f06d
Debugging Day 5
Nov 10, 2019
5a2f564
Debugging Day 5a
Nov 11, 2019
394fd59
Debugging Day 5a
Nov 11, 2019
d2ca507
Debugging Day 6 - change in markedEdgestoFix
Nov 11, 2019
a2c2d0a
Debugging Day 6: changes to EdgeOptimizer logic
Nov 11, 2019
d841f06
Debugging Day 6- acceptance based on getting the operating TET valid
Nov 12, 2019
c334ad3
Debugging Day 6: New logic, commented out unused lines
Nov 12, 2019
6a83b76
Debugging Day 7
Nov 12, 2019
52faaa2
Debugging Day 8
Nov 13, 2019
1129dfa
try face before edge reshape
Nov 15, 2019
e84d91c
Without constraining face nodes on model surface
Nov 16, 2019
b11c764
rewrote edge distribution criterion
mortezah Nov 17, 2019
f382558
Debugging -changed Edge and Face Node Distribution evaluation
Nov 18, 2019
94ce147
Debugging increased invalid Edge index
Nov 18, 2019
dc142d7
Debugging reshape operator, final. without entity numbering.
Nov 20, 2019
b8c7c80
new function fixInvalidEdgesCollapseAndSwap
Nov 20, 2019
7301f4f
debugging for antenna
Nov 22, 2019
b69ad47
delta value change
Nov 22, 2019
8f4594e
debug-single-pass
Nov 24, 2019
eb7471c
debug_antenna_1_invalidity
Nov 25, 2019
e03b344
Clean up + crvDBG + External LBFGS
mortezah Nov 27, 2019
be87eed
Example of using crvDBG.h
mortezah Nov 27, 2019
10bc76e
minor changes
Dec 9, 2019
5d480e3
Reverts to internal LBFGS library
mortezah Feb 12, 2020
71aa886
Fixes forward decleration issues
mortezah Feb 12, 2020
30603e7
Re-factors objective functions evaluations
mortezah Feb 14, 2020
905bb59
Re-factors getGrad + add tol
mortezah Feb 14, 2020
ae42476
code clean.
Feb 15, 2020
7553470
fixed getGrad() + increased r
Feb 15, 2020
de1b46d
Implements the fval computatin based on detJ
Feb 16, 2020
9dea00c
Brings in the size-field info for the reshape ops
mortezah Feb 16, 2020
b279d6e
-new Objective function + corresponding modifications
Feb 29, 2020
df542ec
fixed weights for NIKL optimization funtion
Feb 29, 2020
a6b1005
Cleans the code before dev is merged into branch
mortezah Mar 2, 2020
f21a392
Merge branch 'develop' into crv_edge_optim
mortezah Mar 2, 2020
206b826
Fixes problems with warning--error compile mode
mortezah Mar 2, 2020
0f186dd
fixed acceptance criteria for internal edge reshape.
Mar 6, 2020
0ef2a9e
includes some crv header files public.
Mar 6, 2020
3cc5625
Removes unnecessary file from the repo
mortezah May 26, 2020
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
10 changes: 10 additions & 0 deletions crv/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Package sources
set(SOURCES
crv.cc
crvDBG.cc
crvAdapt.cc
crvBernstein.cc
crvBezier.cc
Expand All @@ -20,11 +21,20 @@ set(SOURCES
crvTables.cc
crvQuality.cc
crvVtk.cc
LBFGS.cc
crvOptimizations.cc
crvObjectiveFunctions.cc
)

# Package headers
set(HEADERS
crv.h
crvAdapt.h
crvBezier.h
crvQuality.h
crvOptimizations.h
crvObjectiveFunctions.h
LBFGS.h
)

# Add the crv library
Expand Down
189 changes: 189 additions & 0 deletions crv/LBFGS.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#include "LBFGS.h"
#include "crvObjectiveFunctions.h"
#include <PCU.h>
#include "apfMatrix.h"

namespace crv{

//void LBFGS::setInitialValue(std::vector<double> x)
//{
// x0 = x;
//}

std::vector<double> LBFGS::getCurrentX()
{
return currentX;
}

static double dotP(const std::vector<double> &v1, const std::vector<double> &v2)
{
double sum = 0.0;
for (std::size_t i = 0; i < v1.size(); i++) {
sum += v1[i]*v2[i];
}
return sum;
}

double LBFGS::getFvalueAfter()
{
return fValAfter;
}

double LBFGS::lineSearch(std::vector<double> &xold, std::vector<double> &g, std::vector<double> &direction, double stpmax)
{
double alpha = 1.0e-4, tolOndeltaX = 1.0e-8;
int itrs = 2000;
double a, alam, alam2 = 0.0, alamin, b, disc, f2 = 0.0;
double rhs1, rhs2, slope = 0.0, sum = 0.0, temp, test, tmplam;
int n = xold.size();

std::vector<double> xnew(xold.size(), 0.0);
sum = sqrt(dotP(direction,direction));
if (sum > stpmax) {
for (int i = 0; i < n; i++) {
direction[i] = direction[i]*stpmax/sum;
}
}
// for (int i = 0; i < n; i++) slope += g[i]*p[i];
slope = dotP(g,direction);
//if (slope >= 0.0) std::cout<<"slope positive"<<std::endl;

test = 0.0;
for (int i = 0; i < n; i++) {
temp = std::abs(direction[i])/std::max(std::abs(xold[i]),1.0);
if (temp > test) test = temp;
}
alamin = tolOndeltaX/test;
alam = 1.0;

double fold = objFunc->getValue(xold);

for (int k = 0; k < itrs; k++) {
for (int i =0; i < n; i++) xnew[i] = xold[i] + alam*direction[i];

double fnew = objFunc->getValue(xnew);
if (alam < alamin || fnew <= fold + alpha*alam*slope) {
return alam;
}
else {
if (alam == 1.0)
tmplam = -slope/(2.0* (fnew-fold-slope));
else {
rhs1 = fnew - fold - alam*slope;
rhs2 = f2 - fold - alam2*slope;
a = (rhs1/(alam*alam) - rhs2/(alam2*alam2))/(alam - alam2);
b = (-alam2*rhs1/(alam*alam) + alam*rhs2/(alam2*alam2))/(alam-alam2);
if (a == 0.0) tmplam = -slope/(2.0*b);
else {
disc = b*b - 3.0*a*slope;
if (disc < 0.0) tmplam = 0.5*alam;
else if (b <= 0.0) tmplam = (-b + sqrt(disc))/(3.0*a);
else tmplam = -slope/(b + sqrt(disc));
}
if (tmplam > 0.5*alam) tmplam = 0.5*alam;
}
}
alam2 = alam;
alam = std::max(tmplam, 0.1*alam);
}
return alam;
}

void LBFGS::moveArrayToLeft(std::vector<double> a[], int r)
{
for (int i = 0; i < r-1; i++)
a[i] = a[i+1];
}

bool LBFGS::run()
{
std::vector<double> p(x0.size(), 0.0);
std::vector<double> xs[r];
std::vector<double> gs[r];
std::vector<double> gdiffs[r];
double gammas[r];

for(int i = 0; i < r; ++i)
{
xs[i] = std::vector<double>(x0.size(), 0.0);
gs[i] = std::vector<double>(x0.size(), 0.0);
gdiffs[i] = std::vector<double>(x0.size(), 0.0);
gammas[i] = 0.0;
}

xs[0] = x0;
gs[0] = objFunc->getGrad(x0);

for (std::size_t i = 0; i < xs[0].size(); i++) p[i] = -gs[0][i];

for (int k = 0; k < iter; k++) {
int I = 0;
int J = 0;
if (k+1 < r) {
I = k+1;
J = k;
}
else {
I = r-1;
J = I-1;
moveArrayToLeft(xs, r);
moveArrayToLeft(gs, r);
}
double stpmax = (std::max(sqrt(dotP(p,p)), double(objFunc->getSpaceDim())));
double lambda = lineSearch(xs[J], gs[J], p, stpmax);

for (std::size_t j = 0; j < xs[I].size(); j++)
xs[I][j] = xs[J][j] + lambda * p[j];

gs[I] = objFunc->getGrad(xs[I]);

if ( I > 0) {
for (std::size_t jj = 0; jj < gs[I].size(); jj++)
gdiffs[I-1][jj] = gs[I][jj] - gs[I-1][jj];
}

if ((dotP(gs[I],gs[I]) < tol) || (dotP(gdiffs[I-1], gdiffs[I-1]) < tol)) {
currentX = xs[I];
fValAfter = objFunc->getValue(xs[I]);
//std::cout<<"number of LBFGS iterations: "<<k<<std::endl;
return true;
}

for (std::size_t j = 0; j < xs[I].size(); j++) p[j] = -gs[I][j];

for (int i = I-1; i >= 0; --i) {
std::vector<double> s(xs[I].size(), 0.0);
std::vector<double> y(xs[I].size(), 0.0);
for( std::size_t j = 0; j < xs[i+1].size(); j++) {
s[j] = xs[i+1][j] - xs[i][j];
y[j] = gs[i+1][j] - gs[i][j];
}
double rho = 1 / dotP(s, y);
gammas[i] = rho * dotP(s, p);
for(std::size_t j = 0; j < p.size(); j++)
p[j] = p[j] - gammas[i]*y[j];
}

for (int i = 0; i <= I-1; i++) {
std::vector<double> s(xs[I].size(), 0.0);
std::vector<double> y(xs[I].size(), 0.0);
for( std::size_t j = 0; j < xs[i+1].size(); j++) {
s[j] = xs[i+1][j] - xs[i][j];
y[j] = gs[i+1][j] - gs[i][j];
}
double rho = 1 / dotP(s, y);
double phi = rho* dotP(y, p);
for(std::size_t j = 0; j < p.size(); j++)
p[j] = p[j] + s[j]* (gammas[i] - phi);
}

if (dotP(p, gs[I]) > 0)
for (std::size_t j = 0; j < p.size(); j++) p[j] = -p[j];

}
return false;

}

} // end of namespace

47 changes: 47 additions & 0 deletions crv/LBFGS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

#ifndef LBFGS_H
#define LBFGS_H

#include <iostream>
#include <pcu_util.h>
#include <vector>

namespace crv{

// forward declare ObjFunction so the type is known to the following classes
class ObjFunction;

class LBFGS
{
public:
LBFGS(double inTol, int inIter, const std::vector<double> &x, ObjFunction *inObjFunc):
tol(inTol), iter(inIter), x0(x), objFunc(inObjFunc)
{
//setInitialValue(x0);
}

~LBFGS() {}

public:
//void setInitialValue(std::vector<double> x);
std::vector<double> getCurrentX();
double getFvalueAfter();
double lineSearch(std::vector<double> &xold, std::vector<double> &g, std::vector<double> &direction, double stpmax);
void moveArrayToLeft(std::vector<double> a[], int r);
bool run();

public:
double tol;
int iter;
std::vector<double> x0;
ObjFunction *objFunc;
std::vector<double> currentX;
double fValAfter;

private:
int r = 50;
};

}

#endif
8 changes: 8 additions & 0 deletions crv/crv.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
* \brief the curving functions are contained in this namespace */
namespace crv {


/** \brief choices of objective function for reshape*/
enum {
NIJK, // based on Jacobian coefficients
DETJ, // based on Jacobian Determinant Values at Integration Points
DETJNIJK // based on TODO : to be decided
};

/** \brief actually 1 greater than max order */
static unsigned const MAX_ORDER = 19;

Expand Down
10 changes: 6 additions & 4 deletions crv/crvAdapt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ ma::Input* configureShapeCorrection(

static int fixInvalidElements(crv::Adapt* a)
{
a->input->shouldForceAdaptation = true;
a->input->shouldForceAdaptation = false;
int count = crv::fixLargeBoundaryAngles(a)
+ crv::fixInvalidEdges(a);
+ crv::fixInvalidFaces(a)
+ crv::fixInvalidEdges(a);
int originalCount = count;
int prev_count;
int i = 0;
Expand All @@ -174,12 +175,14 @@ static int fixInvalidElements(crv::Adapt* a)
break;
prev_count = count;
count = crv::fixLargeBoundaryAngles(a)
+ crv::fixInvalidEdges(a);
+ crv::fixInvalidFaces(a)
+ crv::fixInvalidEdges(a);
++i;
} while(count < prev_count);

crv::fixLargeBoundaryAngles(a);
ma::clearFlagFromDimension(a,ma::COLLAPSE | ma::BAD_QUALITY,1);
ma::clearFlagFromDimension(a, ma::SNAP, 2);
a->input->shouldForceAdaptation = false;
return originalCount - count;
}
Expand Down Expand Up @@ -220,7 +223,6 @@ void adapt(ma::Input* in)
fixCrvElementShapes(a);
}

allowSplitCollapseOutsideLayer(a);

if (in->maximumIterations > 0) {
fixInvalidElements(a);
Expand Down
11 changes: 7 additions & 4 deletions crv/crvCurveMesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
#include "crvBezier.h"
#include "crvShape.h"
#include "crvSnap.h"

#include "crvMath.h"
#include <pcu_util.h>
#include <iostream>

namespace crv {

Expand Down Expand Up @@ -193,9 +194,11 @@ bool BezierCurver::run()

convertInterpolatingToBezier();

if( m_mesh->getDimension() >= 2 && m_order == 2){
ma::Input* shapeFixer = configureShapeCorrection(m_mesh);
crv::adapt(shapeFixer);
if(m_mesh->getDimension() >= 2) {
if (m_order == 2 || m_order == 3) {
ma::Input* shapeFixer = configureShapeCorrection(m_mesh);
crv::adapt(shapeFixer);
}
}

m_mesh->acceptChanges();
Expand Down
Loading