Changeset 21449
- Timestamp:
- 12/22/16 10:39:50 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/c/cores/controlm1qn3_core.cpp
r19709 r21449 26 26 void simul(long* indic,long* n,double* X,double* pf,double* G,long izs[1],float rzs[1],void* dzs); 27 27 28 /*Use struct to provide arguments*/ 29 typedef struct{ 30 FemModel * femmodel; 31 IssmDouble* Jlist; 32 int M; 33 int N; 34 int* i; 35 } m1qn3_struct; 36 28 37 void controlm1qn3_core(FemModel* femmodel){ 29 38 … … 32 41 double f,dxmin,gttol; 33 42 int maxsteps,maxiter; 34 int intn,numberofvertices,num_controls, solution_type;43 int intn,numberofvertices,num_controls,num_cost_functions,solution_type; 35 44 IssmDouble *scaling_factors = NULL; 36 45 IssmDouble *X = NULL; … … 40 49 femmodel->parameters->FindParam(&solution_type,SolutionTypeEnum); 41 50 femmodel->parameters->FindParam(&num_controls,InversionNumControlParametersEnum); 51 femmodel->parameters->FindParam(&num_cost_functions,InversionNumCostFunctionsEnum); 42 52 femmodel->parameters->FindParam(&maxsteps,InversionMaxstepsEnum); 43 53 femmodel->parameters->FindParam(&maxiter,InversionMaxiterEnum); … … 86 96 } 87 97 88 /*Allocate m1qn3 working arrays (see doc )*/98 /*Allocate m1qn3 working arrays (see documentation)*/ 89 99 long m = 100; 90 100 long ndz = 4*n+m*(2*n+1); … … 96 106 _printf0_("____________________________________________________________________\n"); 97 107 108 /*Prepare structure for m1qn3*/ 109 m1qn3_struct mystruct; 110 mystruct.femmodel = femmodel; 111 mystruct.M = maxiter; 112 mystruct.N = num_cost_functions+1; 113 mystruct.Jlist = xNewZeroInit<IssmDouble>(mystruct.M*mystruct.N); 114 mystruct.i = xNewZeroInit<int>(1); 115 98 116 /*Initialize Gradient and cost function of M1QN3*/ 99 indic = 4; / /adjoint and gradient required100 simul(&indic,&n,X,&f,G,izs,rzs,(void*) femmodel);117 indic = 4; /*gradient required*/ 118 simul(&indic,&n,X,&f,G,izs,rzs,(void*)&mystruct); 101 119 102 120 /*Estimation of the expected decrease in f during the first iteration*/ … … 107 125 &n,X,&f,G,&dxmin,&df1, 108 126 >tol,normtype,&impres,&io,imode,&omode,&niter,&nsim,iz,dz,&ndz, 109 &reverse,&indic,izs,rzs,(void*) femmodel);127 &reverse,&indic,izs,rzs,(void*)&mystruct); 110 128 111 129 switch(int(omode)){ … … 137 155 ControlInputSetGradientx(femmodel->elements,femmodel->nodes,femmodel->vertices,femmodel->loads,femmodel->materials,femmodel->parameters,G); 138 156 femmodel->OutputControlsx(&femmodel->results); 139 femmodel->results->AddObject(new GenericExternalResult<double >(femmodel->results->Size()+1,JEnum,f));157 femmodel->results->AddObject(new GenericExternalResult<double*>(femmodel->results->Size()+1,JEnum,mystruct.Jlist,mystruct.M,mystruct.N,0,0)); 140 158 141 159 /*Finalize*/ … … 153 171 xDelete<double>(XL); 154 172 xDelete<double>(scaling_factors); 173 xDelete<double>(mystruct.Jlist); 174 xDelete<int>(mystruct.i); 155 175 } 156 176 … … 158 178 void simul(long* indic,long* n,double* X,double* pf,double* G,long izs[1],float rzs[1],void* dzs){ 159 179 160 /*Recover Femmodel*/ 161 int solution_type; 162 FemModel *femmodel = (FemModel*)dzs; 163 164 /*Recover number of cost functions responses*/ 165 int num_responses,num_controls,numberofvertices; 180 /*Recover Arguments*/ 181 m1qn3_struct *input_struct = (m1qn3_struct*)dzs; 182 FemModel *femmodel = input_struct->femmodel; 183 IssmDouble *Jlist = input_struct->Jlist; 184 int JlistM = input_struct->M; 185 int JlistN = input_struct->N; 186 int *Jlisti = input_struct->i; 187 188 /*Recover some parameters*/ 189 int num_responses,num_controls,numberofvertices,solution_type; 166 190 IssmDouble* scaling_factors = NULL; 167 191 femmodel->parameters->FindParam(&num_responses,InversionNumCostFunctionsEnum); 168 192 femmodel->parameters->FindParam(&num_controls,InversionNumControlParametersEnum); 169 193 femmodel->parameters->FindParam(&scaling_factors,NULL,InversionControlScalingFactorsEnum); 194 femmodel->parameters->FindParam(&solution_type,SolutionTypeEnum); 170 195 numberofvertices=femmodel->vertices->NumberOfVertices(); 171 196 172 /*Constrain input vector */197 /*Constrain input vector and update controls*/ 173 198 IssmDouble *XL = NULL; 174 199 IssmDouble *XU = NULL; … … 183 208 } 184 209 } 185 186 /*Update control input*/187 210 SetControlInputsFromVectorx(femmodel,X); 188 189 /*Recover some parameters*/190 femmodel->parameters->FindParam(&solution_type,SolutionTypeEnum);191 211 192 212 /*Compute solution and adjoint*/ … … 196 216 solutioncore(femmodel); 197 217 218 /*Check size of Jlist to avoid crashes*/ 219 _assert_((*Jlisti)<JlistM); 220 _assert_(JlistN==num_responses+1); 221 198 222 /*Compute objective function*/ 199 IssmDouble* J list= NULL;200 femmodel->CostFunctionx(pf,&J list,NULL);223 IssmDouble* Jtemp = NULL; 224 femmodel->CostFunctionx(pf,&Jtemp,NULL); 201 225 _printf0_("f(x) = "<<setw(12)<<setprecision(7)<<*pf<<" | "); 226 227 /*Record cost function values and delete Jtemp*/ 228 for(int i=0;i<num_responses;i++) Jlist[(*Jlisti)*JlistN+i] = Jtemp[i]; 229 Jlist[(*Jlisti)*JlistN+num_responses] = *pf; 230 xDelete<IssmDouble>(Jtemp); 202 231 203 232 if(*indic==0){ … … 206 235 /*Retrieve objective functions independently*/ 207 236 _printf0_(" N/A |\n"); 208 for(int i=0;i<num_responses;i++) _printf0_(" "<<setw(12)<<setprecision(7)<<Jlist[ i]);237 for(int i=0;i<num_responses;i++) _printf0_(" "<<setw(12)<<setprecision(7)<<Jlist[(*Jlisti)*JlistN+i]); 209 238 _printf0_("\n"); 210 239 211 xDelete<IssmDouble>(Jlist);240 *Jlisti = (*Jlisti) +1; 212 241 xDelete<IssmDouble>(XU); 213 242 xDelete<IssmDouble>(XL); … … 241 270 /*Print info*/ 242 271 _printf0_(" "<<setw(12)<<setprecision(7)<<Gnorm<<" |"); 243 for(int i=0;i<num_responses;i++) _printf0_(" "<<setw(12)<<setprecision(7)<<Jlist[ i]);272 for(int i=0;i<num_responses;i++) _printf0_(" "<<setw(12)<<setprecision(7)<<Jlist[(*Jlisti)*JlistN+i]); 244 273 _printf0_("\n"); 245 274 246 275 /*Clean-up and return*/ 247 xDelete<IssmDouble>(Jlist);276 *Jlisti = (*Jlisti) +1; 248 277 xDelete<IssmDouble>(XU); 249 278 xDelete<IssmDouble>(XL);
Note:
See TracChangeset
for help on using the changeset viewer.