Skip to content
Snippets Groups Projects
Commit 759b7618 authored by Thomas Toulorge's avatar Thomas Toulorge
Browse files

High order optimizer: moved functions for computation of distance to CAD to separate files

parent d91001df
Branches
Tags
No related merge requests found
...@@ -8,6 +8,7 @@ set(SRC ...@@ -8,6 +8,7 @@ set(SRC
OptHOM.cpp OptHOM.cpp
OptHomRun.cpp OptHomRun.cpp
OptHomIntegralBoundaryDist.cpp OptHomIntegralBoundaryDist.cpp
OptHomCADDist.cpp
ParamCoord.cpp ParamCoord.cpp
SuperEl.cpp SuperEl.cpp
OptHomElastic.cpp OptHomElastic.cpp
......
This diff is collapsed.
...@@ -54,15 +54,9 @@ public: ...@@ -54,15 +54,9 @@ public:
int optimize(double lambda, double lambda3, double barrier_min, double barrier_max, int optimize(double lambda, double lambda3, double barrier_min, double barrier_max,
bool optimizeMetricMin, int pInt, int itMax, int optPassMax, bool optimizeMetricMin, int pInt, int itMax, int optPassMax,
int optimizeCAD, double optCADDistMax, double tolerance); int optimizeCAD, double optCADDistMax, double tolerance);
int optimize_inhouse(double weight, double weightCAD, double b_min, double b_max,
bool optimizeMetricMin, int pInt, int itMax, int optPassMax,
int optCAD, double distanceMax, double tolerance);
void recalcJacDist(); void recalcJacDist();
inline void getJacDist(double &minJ, double &maxJ, double &maxD, double &avgD); inline void getJacDist(double &minJ, double &maxJ, double &maxD, double &avgD);
void updateMesh(const alglib::real_1d_array &x); void updateMesh(const alglib::real_1d_array &x);
void evalObjGrad(std::vector<double> &x, double &Obj, bool gradsNeeded,
std::vector<double> &gradObj);
void evalObjGrad(const alglib::real_1d_array &x, double &Obj, void evalObjGrad(const alglib::real_1d_array &x, double &Obj,
alglib::real_1d_array &gradObj); alglib::real_1d_array &gradObj);
void printProgress(const alglib::real_1d_array &x, double Obj); void printProgress(const alglib::real_1d_array &x, double Obj);
...@@ -80,16 +74,11 @@ public: ...@@ -80,16 +74,11 @@ public:
// true : minimize the distance between mesh and CAD // true : minimize the distance between mesh and CAD
bool addApproximationErrorObjGrad(double Fact, double &Obj, alglib::real_1d_array &gradObj, simpleFunction<double>& fct); bool addApproximationErrorObjGrad(double Fact, double &Obj, alglib::real_1d_array &gradObj, simpleFunction<double>& fct);
bool addJacObjGrad(double &Obj, alglib::real_1d_array &gradObj); bool addJacObjGrad(double &Obj, alglib::real_1d_array &gradObj);
bool addJacObjGrad(double &Obj, std::vector<double> &);
bool addBndObjGrad (double Fact, double &Obj, alglib::real_1d_array &gradObj); bool addBndObjGrad (double Fact, double &Obj, alglib::real_1d_array &gradObj);
bool addBndObjGrad2(double Fact, double &Obj, alglib::real_1d_array &gradObj);
bool addBndObjGrad(double Fact, double &Obj, std::vector<double> &gradObj);
bool addMetricMinObjGrad(double &Obj, alglib::real_1d_array &gradObj); bool addMetricMinObjGrad(double &Obj, alglib::real_1d_array &gradObj);
bool addDistObjGrad(double Fact, double &Obj, std::vector<double> &gradObj);
bool addDistObjGrad(double Fact, double &Obj, alglib::real_1d_array &gradObj); bool addDistObjGrad(double Fact, double &Obj, alglib::real_1d_array &gradObj);
void calcScale(alglib::real_1d_array &scale); void calcScale(alglib::real_1d_array &scale);
void OptimPass(alglib::real_1d_array &x, int itMax); void OptimPass(alglib::real_1d_array &x, int itMax);
void OptimPass(std::vector<double> &x, int itMax);
}; };
void OptHOM::getJacDist(double &minJ, double &maxJ, double &maxD, double &avgD) void OptHOM::getJacDist(double &minJ, double &maxJ, double &maxD, double &avgD)
......
// TODO: Header
#include "MLine.h"
#include "MTriangle.h"
#include "GModel.h"
#include "OptHomCADDist.h"
double MFaceGFaceDistance(MTriangle *t, GFace *gf,
std::vector<std::vector<SVector3> > *gsfT,
std::map<MVertex*,SVector3> *normalsToCAD) {
const double h = t->maxEdge();
double jac[3][3];
double distFace = 0.0;
// for (int j=0;j<3;j++){
for (int j=0;j<t->getNumVertices();j++){
// get parametric coordinates of jth vertex
// the last line of the jacobian is the normal
// to the element @ (u_mesh,v_mesh)
if (gsfT){
double detJ = t->getJacobian((*gsfT)[j],jac);
}
else{
const nodalBasis &elbasis = *t->getFunctionSpace();
double u_mesh = elbasis.points(j,0);
double v_mesh = elbasis.points(j,1);
double detJ = t->getJacobian(u_mesh,v_mesh,0,jac);
}
SVector3 tg_mesh (jac[2][0],jac[2][1],jac[2][2]);
tg_mesh.normalize();
SVector3 tg_cad ;
if (normalsToCAD)tg_cad = (*normalsToCAD)[t->getVertex(j)];
else {
SPoint2 p_cad;
reparamMeshVertexOnFace(t->getVertex (j), gf, p_cad);
tg_cad = gf->normal(p_cad);
tg_cad.normalize();
}
SVector3 diff1 = (dot(tg_cad, tg_mesh) > 0) ?
tg_cad - tg_mesh : tg_cad + tg_mesh;
// printf("%g %g %g vs %g %g %g\n",tg_cad.x(),tg_cad.y(),tg_cad.z(),tg_mesh.x(),tg_mesh.y(),tg_mesh.z());
distFace += diff1.norm();
}
return distFace;
}
double MLineGEdgeDistance (MLine *l, GEdge *ge, FILE *f) {
const nodalBasis &elbasis = *l->getFunctionSpace();
const double h = .25*0.5*distance (l->getVertex (0), l->getVertex (1) ) / (l->getNumVertices()-1);
double jac[3][3];
double distEdge = 0.0;
// if(f)printf("%d\n",l->getNumVertices());
for (int j=0;j<l->getNumVertices();j++){
double t_mesh = elbasis.points(j,0);
// if (f) printf("%g ",t_mesh);
double detJ = l->getJacobian(t_mesh,0,0,jac);
SVector3 tg_mesh (jac[0][0],jac[0][1],jac[0][2]);
tg_mesh.normalize();
double t_cad;
reparamMeshVertexOnEdge(l->getVertex (j), ge, t_cad);
SVector3 tg_cad = ge->firstDer(t_cad);
tg_cad.normalize();
SVector3 diff1 = (dot(tg_cad, tg_mesh) > 0) ?
tg_cad - tg_mesh : tg_cad + tg_mesh;
if (f){
fprintf (f,"SP(%g,%g,%g){%g};\n",l->getVertex (j)->x(),
l->getVertex (j)->y(),l->getVertex (j)->z(),h*diff1.norm());
}
// SVector3 n = crossprod(tg_cad,tg_mesh);
// printf("%g %g vs %g %g\n",tg_cad.x(),tg_cad.y(),tg_mesh.x(),tg_mesh.y());
distEdge += diff1.norm();
}
// if(f)printf("\n");
return h*distEdge;
}
void distanceFromElementsToGeometry(GModel *gm, int dim, std::map<MElement*,double> &distances){
std::map<MEdge,double,Less_Edge> dist2Edge;
for (GModel::eiter it = gm->firstEdge(); it != gm->lastEdge(); ++it){
if ((*it)->geomType() == GEntity::Line)continue;
for (unsigned int i=0;i<(*it)->lines.size(); i++){
double d = MLineGEdgeDistance ( (*it)->lines[i] , *it );
MEdge e = (*it)->lines[i]->getEdge(0);
dist2Edge[e] = d;
}
}
// printf("DISTANCE TO GEOMETRY : 1D PART %22.15E\n",Obj);
std::map<MFace,double,Less_Face> dist2Face;
for(GModel::fiter it = gm->firstFace(); it != gm->lastFace(); ++it){
if ((*it)->geomType() == GEntity::Plane)continue;
for (unsigned int i=0;i<(*it)->triangles.size(); i++){
double d = MFaceGFaceDistance ( (*it)->triangles[i] , *it );
MFace f = (*it)->triangles[i]->getFace(0);
dist2Face[f] = d;
}
}
std::vector<GEntity*> entities;
gm->getEntities(entities);
for (int iEnt = 0; iEnt < entities.size(); ++iEnt) {
GEntity* &entity = entities[iEnt];
if (entity->dim() != dim) continue;
for (int iEl = 0; iEl < entity->getNumMeshElements();iEl++) { // Detect bad elements
MElement *element = entity->getMeshElement(iEl);
double d = 0.0;
for (int iEdge = 0; iEdge < element->getNumEdges(); ++iEdge) {
MEdge e = element->getEdge(iEdge);
std::map<MEdge,double,Less_Edge>::iterator it = dist2Edge.find(e);
if(it != dist2Edge.end())d+=it->second;
}
for (int iFace = 0; iFace < element->getNumFaces(); ++iFace) {
MFace f = element->getFace(iFace);
std::map<MFace,double,Less_Face>::iterator it = dist2Face.find(f);
if(it != dist2Face.end())d+=it->second;
}
distances[element] = d;
}
}
}
double distanceToGeometry(GModel *gm)
{
FILE *f = fopen("toto.pos","w");
fprintf(f,"View \"\"{\n");
double Obj = 0.0;
for (GModel::eiter it = gm->firstEdge(); it != gm->lastEdge(); ++it){
if ((*it)->geomType() == GEntity::Line)continue;
for (unsigned int i=0;i<(*it)->lines.size(); i++){
//Obj += MLineGEdgeDistance ( (*it)->lines[i] , *it,f );
Obj = std::max(MLineGEdgeDistance ( (*it)->lines[i] , *it, f ),Obj);
}
}
printf("DISTANCE TO GEOMETRY : 1D PART %22.15E\n",Obj);
for(GModel::fiter it = gm->firstFace(); it != gm->lastFace(); ++it){
if ((*it)->geomType() == GEntity::Plane)continue;
// printf("FACE %d with %d triangles\n",(*it)->tag(),(*it)->triangles.size());
for (unsigned int i=0;i<(*it)->triangles.size(); i++){
//Obj += MFaceGFaceDistance ( (*it)->triangles[i] , *it );
Obj = std::max(Obj,MFaceGFaceDistance ( (*it)->triangles[i] , *it ));
}
}
printf("DISTANCE TO GEOMETRY : 1D AND 2D PART %22.15E\n",Obj);
fprintf(f,"};\n");
fclose(f);
return Obj;
}
// TODO: Header
#ifndef OPTHOMCADDIST_H_
#define OPTHOMCADDIST_H_
class GModel;
class MElement;
class MLine;
class MTriangle;
class GEdge;
class GFace;
double MFaceGFaceDistance(MTriangle *t, GFace *gf,
std::vector<std::vector<SVector3> > *gsfT=0,
std::map<MVertex*,SVector3> *normalsToCAD=0);
double MLineGEdgeDistance (MLine *l, GEdge *ge, FILE *f = 0);
void distanceFromElementsToGeometry(GModel *gm, int dim, std::map<MElement*, double> &distances);
double distanceToGeometry(GModel *gm);
#endif /* OPTHOMCADDIST_H_ */
// TODO: Copyright
#ifndef _OPTHOMOBJCONTRIBCADDISTOLD_H_
#define _OPTHOMOBJCONTRIBCADDISTOLD_H_
#include "MeshOptObjContrib.h"
template<class FuncType>
class ObjContribCADDistOld : public ObjContrib, public FuncType
{
public:
ObjContribCADDistOld(double weight, double geomTol);
virtual ~ObjContribCADDistOld() {}
virtual ObjContrib *copy() const;
virtual void initialize(Patch *mesh);
virtual bool fail() { return false; }
virtual bool addContrib(double &Obj, alglib::real_1d_array &gradObj);
virtual void updateParameters() { FuncType::updateParameters(_min, _max); }
virtual bool targetReached() { return FuncType::targetReached(_min, _max); }
virtual bool stagnated() { return FuncType::stagnated(_min, _max); }
virtual void updateMinMax();
protected:
Patch *_mesh;
double _weight;
double _geomTol;
};
template<class FuncType>
ObjContribCADDistOld<FuncType>::ObjContribCADDistOld(double weight, double geomTol) :
ObjContrib("CADDist", FuncType::getNamePrefix()+"CADDist"),
_mesh(0), _weight(weight), _geomTol(geomTol)
{
}
template<class FuncType>
ObjContrib *ObjContribCADDistOld<FuncType>::copy() const
{
return new ObjContribCADDistOld<FuncType>(*this);
}
template<class FuncType>
void ObjContribCADDistOld<FuncType>::initialize(Patch *mesh)
{
_mesh = mesh;
updateMinMax();
FuncType::initialize(_min, _max);
}
template<class FuncType>
bool ObjContribCADDistOld<FuncType>::addContrib(double &Obj, alglib::real_1d_array &gradObj)
{
_min = BIGVAL;
_max = -BIGVAL;
std::vector<double> gradF;
for (int iEl = 0; iEl < _mesh->nEl(); iEl++) {
double f;
if (_mesh->bndDistAndGradients(iEl, f, gradF, _geomTol)) {
_min = std::min(_min, f);
_max = std::max(_max, f);
Obj += FuncType::compute(f) * _weight;
const double dFact = _weight * FuncType::computeDiff(f);
for (size_t i = 0; i < _mesh->nPCEl(iEl); ++i)
gradObj[_mesh->indPCEl(iEl, i)] += gradF[i] * dFact;
}
}
return true;
}
template<class FuncType>
void ObjContribCADDistOld<FuncType>::updateMinMax()
{
_min = BIGVAL;
_max = -BIGVAL;
std::vector<double> dumGradF;
for (int iEl = 0; iEl < _mesh->nEl(); iEl++) {
double f;
if (_mesh->bndDistAndGradients(iEl, f, dumGradF, _geomTol)) {
_min = std::min(_min, f);
_max = std::max(_max, f);
}
}
}
#endif /* _OPTHOMOBJCONTRIBCADDISTOLD_H_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment