Changeset 23268
- Timestamp:
- 09/12/18 13:34:23 (7 years ago)
- Location:
- issm/trunk-jpl/src/c/cores
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/c/cores/controladm1qn3_core.cpp
r23267 r23268 17 17 18 18 #if defined (_HAVE_M1QN3_) && defined(_HAVE_AD_) 19 /*m1qn3 prototypes */19 /*m1qn3 prototypes {{{*/ 20 20 extern "C" void *ctonbe_; // DIS mode : Conversion 21 21 extern "C" void *ctcabe_; // DIS mode : Conversion … … 36 36 int* i; 37 37 } m1qn3_struct; 38 /*}}}*/ 38 39 39 40 /*m1qm3 functions*/ … … 91 92 tape_codi.reset(); 92 93 //alloc_profiler.Tag(FinishInit, true); 94 #else 95 tape_codi.reset(); 93 96 #endif 94 97 … … 181 184 182 185 FemModel* femmodel = input_struct->femmodel; 183 int num_responses,num_controls, numberofvertices,solution_type;186 int num_responses,num_controls,solution_type; 184 187 femmodel->parameters->FindParam(&solution_type,SolutionTypeEnum); 185 int* N = NULL;186 int N_add = 0;187 int* control_enum = NULL;188 188 189 189 /*In transient, we need to make sure we do not modify femmodel at each iteration, make a copy*/ … … 197 197 198 198 /*Recover some parameters*/ 199 IssmDouble* scaling_factors = NULL; 199 IssmDouble *scaling_factors = NULL; 200 int *N = NULL; 201 int *control_enum = NULL; 200 202 femmodel->parameters->FindParam(&num_responses,InversionNumCostFunctionsEnum); 201 203 femmodel->parameters->FindParam(&num_controls,InversionNumControlParametersEnum); … … 203 205 femmodel->parameters->FindParam(&N,NULL,ControlInputSizeNEnum); 204 206 femmodel->parameters->FindParam(&control_enum,NULL,InversionControlParametersEnum); 205 numberofvertices=femmodel->vertices->NumberOfVertices();207 int numberofvertices=femmodel->vertices->NumberOfVertices(); 206 208 207 209 /*Constrain input vector and update controls*/ … … 211 213 GetPassiveVectorFromControlInputsx(&XU,femmodel->elements,femmodel->nodes,femmodel->vertices,femmodel->loads,femmodel->materials,femmodel->parameters,"upperbound"); 212 214 213 N_add = 0;215 int N_add = 0; 214 216 for (int c=0;c<num_controls;c++){ 215 217 for(int i=0;i<numberofvertices*N[c];i++){ … … 304 306 //if(my_rank==0) _printf_("\nTIME: "<<now<<"\n"); 305 307 306 /*diverse: */307 int dummy;308 int num_independents=0;309 310 308 /*intermediary: */ 309 int num_independents=intn; 311 310 IssmPDouble *aWeightVector=NULL; 312 311 IssmPDouble *weightVectorTimesJac=NULL; 313 314 /*output: */ 315 IssmPDouble *totalgradient=NULL; 316 317 /*retrieve parameters: */ 318 num_independents = intn; 312 IssmPDouble *totalgradient=xNewZeroInit<IssmPDouble>(num_independents); 319 313 320 314 /*if no dependents, no point in running a driver: */ … … 341 335 /* Ok, now we are going to call the fos_reverse in a loop on the index, from 0 to num_dependents, so 342 336 * as to generate num_dependents gradients: */ 343 totalgradient=xNewZeroInit<IssmPDouble>(num_independents_old);344 345 337 for(int aDepIndex=0;aDepIndex<num_dependents_old;aDepIndex++){ 346 338 … … 379 371 /*Get gradient for CoDiPack{{{*/ 380 372 if(VerboseAutodiff())_printf0_(" CoDiPack fos_reverse\n"); 381 int aDepIndex=0; /*FIXME: do we really need this?*/ 382 383 /*retrieve direction index: */ 384 femmodel->parameters->FindParam(&aDepIndex,AutodiffFosReverseIndexEnum); 385 if (my_rank==0) { 386 if (aDepIndex<0 || aDepIndex>=num_dependents || codi_global.output_indices.size() <= aDepIndex){ 387 _error_("index value for AutodiffFosReverseIndexEnum should be in [0,num_dependents-1]"); 388 } 389 tape_codi.setGradient(codi_global.output_indices[aDepIndex], 1.0); 390 } 391 tape_codi.evaluate(); 392 393 weightVectorTimesJac=xNew<double>(num_independents); 394 /*call driver: */ 395 auto in_size = codi_global.input_indices.size(); 396 for(size_t i = 0; i < in_size; ++i) { 397 weightVectorTimesJac[i] = tape_codi.getGradient(codi_global.input_indices[i]); 398 } 399 400 /*Add to totalgradient: */ 401 totalgradient=xNewZeroInit<IssmPDouble>(num_independents_old); 402 if(my_rank==0) for(int i=0;i<num_independents;i++) { 403 totalgradient[i]+=weightVectorTimesJac[i]; 404 } 405 406 /*free resources :*/ 407 xDelete(weightVectorTimesJac); 373 374 /* call the fos_reverse in a loop on the index, from 0 to num_dependents, so 375 * as to generate num_dependents gradients: */ 376 for(int aDepIndex=0;aDepIndex<num_dependents_old;aDepIndex++){ 377 378 /*initialize direction index in the weights vector: */ 379 aWeightVector=xNewZeroInit<IssmPDouble>(num_dependents); 380 if(my_rank==0) tape_codi.setGradient(codi_global.output_indices[aDepIndex],1.0); 381 tape_codi.evaluate(); 382 383 /*Get gradient for this dependent */ 384 weightVectorTimesJac=xNew<IssmPDouble>(num_independents); 385 auto in_size = codi_global.input_indices.size(); 386 for(size_t i = 0; i < in_size; ++i) { 387 weightVectorTimesJac[i] = tape_codi.getGradient(codi_global.input_indices[i]); 388 } 389 if(my_rank==0) for(int i=0;i<num_independents;i++) { 390 totalgradient[i]+=weightVectorTimesJac[i]; 391 } 392 /*free resources :*/ 393 xDelete(weightVectorTimesJac); 394 xDelete(aWeightVector); 395 } 408 396 /*}}}*/ 409 397 #else … … 426 414 Jlist[(*Jlisti)*JlistN+num_responses] = reCast<IssmPDouble>(J); 427 415 428 /*429 IssmDouble* test = xNew<IssmDouble>(intn);430 femmodel->parameters->FindParam(&test,NULL,InversionXBestEnum);431 for(int i=0;i<10;i++)_printf_("X "<<test[i]<<"\n");432 xDelete<IssmDouble>(intn);433 */434 416 if(*indic==0){ 435 417 /*dry run, no gradient required*/ -
issm/trunk-jpl/src/c/cores/controlm1qn3_core.cpp
r23252 r23268 12 12 13 13 #if defined (_HAVE_M1QN3_) && !defined(_HAVE_AD_) 14 /*m1qn3 prototypes */14 /*m1qn3 prototypes {{{*/ 15 15 extern "C" void *ctonbe_; // DIS mode : Conversion 16 16 extern "C" void *ctcabe_; // DIS mode : Conversion … … 34 34 int* i; 35 35 } m1qn3_struct; 36 37 void controlm1qn3_core(FemModel* femmodel){ 36 /*}}}*/ 37 38 void controlm1qn3_core(FemModel* femmodel){/*{{{*/ 38 39 39 40 /*Intermediaries*/ … … 173 174 xDelete<double>(mystruct.Jlist); 174 175 xDelete<int>(mystruct.i); 175 } 176 177 /*Cost function definition*/ 178 void simul(long* indic,long* n,double* X,double* pf,double* G,long izs[1],float rzs[1],void* dzs){ 176 }/*}}}*/ 177 void simul(long* indic,long* n,double* X,double* pf,double* G,long izs[1],float rzs[1],void* dzs){/*{{{*/ 179 178 180 179 /*Recover Arguments*/ … … 278 277 xDelete<IssmDouble>(XL); 279 278 xDelete<IssmDouble>(scaling_factors); 280 } 279 }/*}}}*/ 281 280 282 281 #else 283 void controlm1qn3_core(FemModel* femmodel){ 284 _error_("M1QN3 not installed"); 285 } 282 void controlm1qn3_core(FemModel* femmodel){_error_("M1QN3 not installed");} 286 283 #endif //_HAVE_M1QN3_
Note:
See TracChangeset
for help on using the changeset viewer.