1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
// GetDP - Copyright (C) 1997-2018 P. Dular and C. Geuzaine, University of Liege
//
// See the LICENSE.txt file for license information. Please report all
// issues on https://gitlab.onelab.info/getdp/getdp/issues
//
// Contributed by Erin Kuci
#include <map>
#include <string>
#include <vector>
#include "ProData.h"
#include "ProParser.h"
#include "Cal_Quantity.h"
#include "Message.h"
#include "GetDPConfig.h"
#if defined(HAVE_MMA) && defined(HAVE_PETSC)
#include "mma_mat.h"
#include "mma_primaldual.h"
// utility functions
static void CreateVector(Vec& Vector, PetscInt nn)
{
VecCreate(PETSC_COMM_WORLD, &Vector);
VecSetSizes(Vector,PETSC_DECIDE,nn);
VecSetFromOptions(Vector);
}
static void UpdateVector(const PetscReal value, const PetscInt row, Vec& vector)
{
VecSetValue(vector,row,value,INSERT_VALUES);
}
static void GetValueVector(double &value, const PetscInt id, const Vec& vector)
{
PetscScalar val;
VecGetValues(vector, 1, &id, &val);
value = PetscRealPart(val);
}
static void AssembleVector(const Vec& vector)
{
VecAssemblyBegin(vector);
VecAssemblyEnd(vector);
}
static void CreateMatrix(Mat& Matrix, PetscInt nbrows, PetscInt nbcols)
{
// full matrix... but in AIJ format for subsequent operations in mma
PetscInt prealloc = nbcols;
MatCreateAIJ(MPI_COMM_WORLD, PETSC_DECIDE, PETSC_DECIDE, nbrows, nbcols,
prealloc, PETSC_NULL, prealloc, PETSC_NULL, &Matrix);
// Preallocation routines automatically set now MAT_NEW_NONZERO_ALLOCATION_ERR,
// what causes a problem when the mask of the matrix changes (e.g. moving band)
// We must disable the error generation and allow new allocation (if needed)
MatSetOption(Matrix,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);
// override the default options with the ones from the option database (if
// any)
MatSetFromOptions(Matrix);
}
static void UpdateMatrixRowFromVector(double* myvecvals, int vecsize,
PetscInt row, Mat& Matrix)
{
// Count the number of nonzero elements in the vector:
//int numnonzero = 0;
//for (int i = 0; i < vecsize; i++){if (myvecvals[i] != 0){numnonzero++;}}
// Get the adresses and values of the nonzeros:
//int nonzeroadresses[numnonzero];
//double nonzerovalues[numnonzero];
//int index = 0;
//for (int i = 0; i < vecsize; i++){
// if (myvecvals[i] != 0){
// nonzeroadresses[index] = i;
// nonzerovalues[index] = myvecvals[i];
// index++;
// }
//}
int nonzeroadresses[vecsize];
PetscScalar nonzerovalues[vecsize];
for (int i = 0; i < vecsize; i++){
nonzeroadresses[i] = i;
nonzerovalues[i] = (PetscScalar)myvecvals[i];
}
// fill the matrix
PetscInt row_vec[1] = {row};
MatSetValues(Matrix,1,row_vec,vecsize,nonzeroadresses,nonzerovalues,
INSERT_VALUES);
// MatSetValues(Matrix,1,row_vec,numnonzero,nonzeroadresses,nonzerovalues,
// INSERT_VALUES);
}
static void AssembleMatrix(const Mat& Matrix)
{
MatAssemblyBegin(Matrix, MAT_FINAL_ASSEMBLY);
MatAssemblyEnd(Matrix, MAT_FINAL_ASSEMBLY);
}
class optimizerContext
{
public:
// name of getdp variables used for I/O with the parser
std::string algorithm;
std::string currentPoint;
std::string objective, objectiveSensitivity;
std::vector<std::string> constraints, constraintsSensitivity;
// internal petsc data
PetscInt nvar, mcon;
Vec xval_pt, LowerBounds, UpperBounds, GradOfObjective, Constraints;
Mat GradOfConstraints;
// subproblem
MMA_MAT *subProblem;
// subproblem solver
MMA_PRIMALDUAL_INTERIORPOINT *subProblemSolver;
public:
optimizerContext(char *nameAlgorithm, char *nameCurrentPoint,
char *nameObjective, char *nameObjectiveSensitivity,
List_T *nameConstraints, List_T *nameConstraintsSensitivity,
List_T *currentPointLowerBounds, List_T *currentPointUpperBounds)
: algorithm(nameAlgorithm), currentPoint(nameCurrentPoint),
objective(nameObjective), objectiveSensitivity(nameObjectiveSensitivity)
{
// names of GetDP exchange variables
for(int i = 0; i < List_Nbr(nameConstraints); i++){
char *c; List_Read(nameConstraints, i, &c);
constraints.push_back(c);
}
for(int i = 0; i < List_Nbr(nameConstraintsSensitivity); i++){
char *c; List_Read(nameConstraintsSensitivity, i, &c);
constraintsSensitivity.push_back(c);
}
std::map<std::string, std::map<int, std::vector<double> > >::iterator itv =
GetDPNumbersMap.find(currentPoint);
nvar = itv->second.size();
// FIXME: get actual number of constraints
mcon = constraints.size();
// Lower bounds of the current point
CreateVector(LowerBounds, nvar);
if(List_Nbr(currentPointLowerBounds) == 1){
double v; List_Read(currentPointLowerBounds, 0, &v);
VecSet(LowerBounds, v);
}
else{
Message::Warning("Multiple lower bounds not implemented yet");
}
AssembleVector(LowerBounds);
// Upper bounds of the current point
CreateVector(UpperBounds, nvar);
if(List_Nbr(currentPointUpperBounds) == 1){
double v; List_Read(currentPointUpperBounds, 0, &v);
VecSet(UpperBounds, v);
}
else{
Message::Warning("Multiple lower bounds not implemented yet");
}
AssembleVector(UpperBounds);
// Create all the useful vectors/matrices
CreateVector(xval_pt, nvar);
CreateVector(GradOfObjective, nvar);
CreateVector(Constraints, mcon);
CreateMatrix(GradOfConstraints, mcon, nvar);
// Create the subproblem
subProblem = new MMA_MAT(mcon, nvar, LowerBounds, UpperBounds);
subProblem->verbosity = 0;
// Create the solver of the subproblem
subProblemSolver = new MMA_PRIMALDUAL_INTERIORPOINT(subProblem);
subProblemSolver->verbosity = 0;
printInfo();
}
void printInfo()
{
Message::Info("Optimizer algorithm: %s", algorithm.c_str());
Message::Info("Optimizer Number of design variables: %i", nvar);
Message::Info("Optimizer Number of constraints %i", mcon);
//VecView(LowerBounds, PETSC_VIEWER_STDOUT_WORLD);
//VecView(UpperBounds, PETSC_VIEWER_STDOUT_WORLD);
}
};
static optimizerContext *context = 0;
static void UpdateCurrentPointList(const std::string &name, const Vec &data)
{
double datav;
std::map<std::string, std::map<int, std::vector<double> > >::iterator itv =
GetDPNumbersMap.find(name);
if(itv != GetDPNumbersMap.end()){
printf(" - table Current Point \n");
int ii = 0;
for(std::map<int, std::vector<double> >::iterator it = itv->second.begin();
it != itv->second.end(); it++){
GetValueVector(datav, ii, data);
for(unsigned int i = 0; i < it->second.size(); i++){
it->second[i] = datav;
//printf("%g ", it->second[i]);
}
ii++;
}
}
else{
std::map<std::string, std::vector<double> >::iterator its =
GetDPNumbers.find(name);
if(its != GetDPNumbers.end()){
printf(" - scalar Current Point \n");
for(unsigned int i = 0; i < its->second.size(); i++){
GetValueVector(datav, i, data);
its->second[i] = datav;
//printf("%g ", its->second[i]);
}
}
else{
Message::Warning("Unknown %s", name.c_str());
}
}
}
static void UpdateVecFromInput(const std::string &type, const std::string &name,
Vec &vv, int idGlobal=0)
{
std::map<std::string, std::map<int, std::vector<double> > >::iterator itv =
GetDPNumbersMap.find(name);
if(itv != GetDPNumbersMap.end()){
printf(" - table %s:\n", type.c_str());
int ii = 0;
for(std::map<int, std::vector<double> >::iterator it = itv->second.begin();
it != itv->second.end(); it++){
UpdateVector(it->second[0], idGlobal+ii, vv);
//printf(" ele %d: ", it->first);
//for(unsigned int i = 0; i < it->second.size(); i++){
//printf("%g ", it->second[i]);
//}
//printf("\n");
ii++;
}
}
else{
std::map<std::string, std::vector<double> >::iterator its =
GetDPNumbers.find(name);
if(its != GetDPNumbers.end()){
printf(" - scalar variable %s: \n", type.c_str());
for(unsigned int i = 0; i < its->second.size(); i++){
UpdateVector(its->second[i], idGlobal+i, vv);
}
}
else{
Message::Warning("Unknown %s: %s", type.c_str(), name.c_str());
}
}
}
static void UpdateMatFromInput(const std::string &type, const std::string &name,
Mat &vv, int idGlobal=0)
{
std::map<std::string, std::map<int, std::vector<double> > >::iterator itv =
GetDPNumbersMap.find(name);
if(itv != GetDPNumbersMap.end()){
printf(" - table %s:\n", type.c_str());
double myvecvals[itv->second.size()];
int ii = 0;
for(std::map<int, std::vector<double> >::iterator it = itv->second.begin();
it != itv->second.end(); it++){
//UpdateVector(it->second[0], idGlobal+ii, vv);
myvecvals[ii] = it->second[0];
//printf(" ele %d: ", it->first);
//for(unsigned int i = 0; i < it->second.size(); i++){
// printf("%g ", it->second[i]);
//}
//printf("\n");
ii++;
}
UpdateMatrixRowFromVector(myvecvals, itv->second.size(), idGlobal, vv);
}
else{
std::map<std::string, std::vector<double> >::iterator its =
GetDPNumbers.find(name);
if(its != GetDPNumbers.end()){
double myvecvals[its->second.size()];
printf(" - scalar variable %s \n", type.c_str());
for(unsigned int i = 0; i < its->second.size(); i++){
myvecvals[i] = its->second[i];
//printf("%g ", its->second[i]);
}
//printf("\n");
UpdateMatrixRowFromVector(myvecvals, its->second.size(), idGlobal, vv);
}
else{
Message::Warning("Unknown %s: %s", type.c_str(), name.c_str());
}
}
}
void Operation_OptimizerInitialize(struct Operation *Operation_P)
{
context = new optimizerContext
(Operation_P->Case.OptimizerInitialize.algorithm,
Operation_P->Case.OptimizerInitialize.currentPoint,
Operation_P->Case.OptimizerInitialize.objective,
Operation_P->Case.OptimizerInitialize.objectiveSensitivity,
Operation_P->Case.OptimizerInitialize.constraints,
Operation_P->Case.OptimizerInitialize.constraintsSensitivity,
Operation_P->Case.OptimizerInitialize.currentPointLowerBounds,
Operation_P->Case.OptimizerInitialize.currentPointUpperBounds);
}
void Operation_OptimizerUpdate(struct Operation *Operation_P)
{
//subProblem->nvar
printf("Opti update:\n");
// Update the vectors
UpdateVecFromInput("currentPoint", context->currentPoint, context->xval_pt);
AssembleVector(context->xval_pt);
//VecView(context->xval_pt, PETSC_VIEWER_STDOUT_WORLD);
UpdateVecFromInput("objectiveSensitivity", context->objectiveSensitivity,
context->GradOfObjective);
AssembleVector(context->GradOfObjective);
//VecView(context->GradOfObjective, PETSC_VIEWER_STDOUT_WORLD);
// FIXME: constraints are not in the same order as they have been declared
// => Maybe their sensitivity are in a different order => mix
for(unsigned int i = 0; i < context->constraints.size(); i++){
UpdateVecFromInput("constraints", context->constraints[i],
context->Constraints, i);
}
AssembleVector(context->Constraints);
//VecView(context->Constraints, PETSC_VIEWER_STDOUT_WORLD);
for(unsigned int i = 0; i < context->constraintsSensitivity.size(); i++){
UpdateMatFromInput("constraint sensitivity", context->constraintsSensitivity[i],
context->GradOfConstraints, i);
}
AssembleMatrix(context->GradOfConstraints);
//MatView(context->GradOfConstraints, PETSC_VIEWER_STDOUT_WORLD);
// Call the optimizer to update the current point
context->subProblemSolver->UpdateCurrentPoint(context->xval_pt,
context->GradOfObjective,
context->Constraints,
context->GradOfConstraints);
//VecView(context->xval_pt, PETSC_VIEWER_STDOUT_WORLD);
Value v;
v.Type = SCALAR;
context->subProblem->DesignChange(context->xval_pt, v.Val[0]);
Cal_StoreInVariable(&v, Operation_P->Case.OptimizerUpdate.residual);
// Update the list of design variables
UpdateCurrentPointList(context->currentPoint, context->xval_pt);
}
void Operation_OptimizerFinalize(struct Operation *Operation_P)
{
VecDestroy(&context->xval_pt);
VecDestroy(&context->LowerBounds);
VecDestroy(&context->UpperBounds);
VecDestroy(&context->GradOfObjective);
VecDestroy(&context->Constraints);
MatDestroy(&context->GradOfConstraints);
}
#else
void Operation_OptimizerInitialize(struct Operation *Operation_P)
{
Message::Error("This version of GetDP is not compiled with MMA support");
}
void Operation_OptimizerUpdate(struct Operation *Operation_P)
{
Message::Error("This version of GetDP is not compiled with MMA support");
}
void Operation_OptimizerFinalize(struct Operation *Operation_P)
{
Message::Error("This version of GetDP is not compiled with MMA support");
}
#endif
|