Changeset 25511
- Timestamp:
- 09/01/20 16:33:34 (5 years ago)
- Location:
- issm/trunk-jpl/src/c
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/c/cores/transient_core.cpp
r25497 r25511 316 316 /*__________________________________________________________________________________*/ 317 317 318 /*Get X (control) and Y (model state)*/318 /*Get X (control)*/ 319 319 IssmDouble *X = NULL; int Xsize; 320 320 GetVectorFromControlInputsx(&X,&Xsize,femmodel->elements,femmodel->nodes,femmodel->vertices,femmodel->loads,femmodel->materials,femmodel->parameters,"value"); 321 IssmDouble *Y = NULL; int Ysize; 322 _error_("don't know how to get Y and Ysize here..."); 321 322 /*Get Y (model state) size*/ 323 CountDoublesFunctor* marshallhandle1 = new CountDoublesFunctor(); 324 femmodel->Marshall(marshallhandle1); 325 int Ysize = marshallhandle1->DoubleCount(); 326 delete marshallhandle1; 323 327 324 328 /*Initialize Xb, Yb and Yin*/ 325 double *Xb = xNewZeroInit<double>(Xsize);329 double *Xb = xNewZeroInit<double>(Xsize); 326 330 double *Yb = xNewZeroInit<double>(Ysize); 327 331 int *Yin = xNewZeroInit<int>(Ysize); … … 332 336 333 337 /*Reverse dependent (f)*/ 334 for(int i=0; i < Ysize; i++) tape_codi.registerInput(Y[i]); 338 RegisterInputFunctor* marshallhandle2 = new RegisterInputFunctor(Yin); 339 femmodel->Marshall(marshallhandle2); 340 delete marshallhandle2; 335 341 for(int i=0; i < Xsize; i++) tape_codi.registerInput(X[i]); 336 342 SetControlInputsFromVectorx(femmodel,X); … … 351 357 352 358 for(int i=0;i<Xsize;i++) Xb[i] += X[i].gradient(); 353 for(int i=0;i<Ysize;i++) Yb[i] = Y[i].gradient(); 359 InitAdjointFunctor* marshallhandle3 = new InitAdjointFunctor(Yb); 360 femmodel->Marshall(marshallhandle3); 361 delete marshallhandle3; 354 362 355 363 /*reverse loop for transient step (G)*/ … … 362 370 363 371 /*We need to store the CoDiPack identifier here, since y is overwritten.*/ 364 for(int i=0; i<Ysize; i++) { 365 tape_codi.registerInput(Y[i]); 366 Yin[i] = Y[i].getGradientData(); 367 } 372 RegisterInputFunctor* marshallhandle4 = new RegisterInputFunctor(Yin); 373 femmodel->Marshall(marshallhandle4); 374 delete marshallhandle4; 368 375 369 376 /*Tell codipack that X is the independent*/ … … 373 380 /*Get New state*/ 374 381 transient_step(femmodel); 375 _error_("don't know how to get new Y again here..."); 376 for(int i=0; i<Ysize; i++) tape_codi.registerOutput(Y[i]); 382 383 /*Register output*/ 384 RegisterOutputFunctor* marshallhandle5 = new RegisterOutputFunctor(); 385 femmodel->Marshall(marshallhandle5); 386 delete marshallhandle5; 377 387 378 388 /*stop tracing*/ … … 381 391 /*Reverse transient step (G)*/ 382 392 /* Using y_b here to seed the next reverse iteration there y_b is always overwritten*/ 383 for(int i=0; i<Ysize; i++) Y[i].gradient() = Yb[i]; 393 SetAdjointFunctor* marshallhandle6 = new SetAdjointFunctor(Yb); 394 femmodel->Marshall(marshallhandle6); 395 delete marshallhandle6; 396 384 397 tape_codi.evaluate(); 398 385 399 /* here we access the gradient data via the stored identifiers.*/ 386 for(int i=0; i<Ysize; i++) Xb[i] = tape_codi.gradient(Yin[i]);387 for(int i=0; i<Xsize; i++) Yb[i] += X[i].gradient();400 for(int i=0; i<Ysize; i++) Yb[i] = tape_codi.gradient(Yin[i]); 401 for(int i=0; i<Xsize; i++) Xb[i] += X[i].gradient(); 388 402 } 389 403 … … 398 412 xDelete<IssmDouble>(X); 399 413 xDelete<double>(Xb); 400 xDelete<IssmDouble>(Y);401 414 xDelete<double>(Yb); 402 415 xDelete<int>(Yin); -
issm/trunk-jpl/src/c/shared/io/Marshalling/Marshalling.h
r25510 r25511 20 20 AD_REGISTERINPUT, 21 21 AD_REGISTEROUTPUT, 22 AD_INITADJOINT, 22 23 AD_SETADJOINT, 23 24 #endif … … 152 153 this->tape_codi = &(IssmDouble::getGlobalTape()); 153 154 } 154 int DoubleCount(void){return this->double_count;};155 155 void Echo(void){ 156 156 printf("RegisterInputFunctor Echo:\n"); … … 185 185 186 186 public: 187 RegisterOutputFunctor(void) : MarshallHandle(AD_REGISTER INPUT){187 RegisterOutputFunctor(void) : MarshallHandle(AD_REGISTEROUTPUT){ 188 188 this->double_count = 0; 189 189 this->tape_codi = &(IssmDouble::getGlobalTape()); 190 190 } 191 int DoubleCount(void){return this->double_count;};192 191 void Echo(void){ 193 192 printf("RegisterOutputFunctor Echo:\n"); … … 208 207 for(int i=0;i<size;i++){ 209 208 this->tape_codi->registerOutput(value[i]); 209 this->double_count++; 210 } 211 } 212 } 213 }; /*}}}*/ 214 class InitAdjointFunctor: public MarshallHandle{ /*{{{*/ 215 216 private: 217 int double_count; 218 IssmDouble::TapeType* tape_codi; 219 double* adjoint; 220 221 public: 222 InitAdjointFunctor(double* adjoint_in) : MarshallHandle(AD_INITADJOINT){ 223 this->double_count = 0; 224 this->tape_codi = &(IssmDouble::getGlobalTape()); 225 this->adjoint = adjoint_in; 226 } 227 void Echo(void){ 228 printf("InitAdjointFunctor Echo:\n"); 229 printf(" double_count: %i\n",double_count); 230 } 231 template<typename T> void call(T & value){ 232 /*General case: do nothing*/ 233 } 234 template<typename T> void call(T* & value,int size){ 235 /*General case: do nothing*/ 236 } 237 void call(IssmDouble value){ 238 value.gradient() = adjoint[this->double_count]; 239 this->double_count++; 240 } 241 void call(IssmDouble* value,int size){ 242 if(value){ 243 for(int i=0;i<size;i++){ 244 value[i].gradient() = adjoint[this->double_count]; 245 this->double_count++; 246 } 247 } 248 } 249 }; /*}}}*/ 250 class SetAdjointFunctor: public MarshallHandle{ /*{{{*/ 251 252 private: 253 int double_count; 254 IssmDouble::TapeType* tape_codi; 255 double* adjoint; 256 257 public: 258 SetAdjointFunctor(double* adjoint_in) : MarshallHandle(AD_SETADJOINT){ 259 this->double_count = 0; 260 this->tape_codi = &(IssmDouble::getGlobalTape()); 261 this->adjoint = adjoint_in; 262 } 263 void Echo(void){ 264 printf("SetAdjointFunctor Echo:\n"); 265 printf(" double_count: %i\n",double_count); 266 } 267 template<typename T> void call(T & value){ 268 /*General case: do nothing*/ 269 } 270 template<typename T> void call(T* & value,int size){ 271 /*General case: do nothing*/ 272 } 273 void call(IssmDouble value){ 274 value.gradient() = this->adjoint[this->double_count]; 275 this->double_count++; 276 } 277 void call(IssmDouble* value,int size){ 278 if(value){ 279 for(int i=0;i<size;i++){ 280 value[i].gradient() = this->adjoint[this->double_count]; 210 281 this->double_count++; 211 282 } … … 221 292 case MARSHALLING_SIZE: {SizeCheckpointFunctor* temp = xDynamicCast<SizeCheckpointFunctor*>(this); temp->call(value); break;} 222 293 #ifdef _HAVE_CODIPACK_ 223 case AD_COUNTDOUBLES: {CountDoublesFunctor* temp = xDynamicCast<CountDoublesFunctor*>(this); temp->call(value); break;} 224 case AD_REGISTERINPUT: {RegisterInputFunctor* temp = xDynamicCast<RegisterInputFunctor*>(this); temp->call(value); break;} 225 case AD_REGISTEROUTPUT:{RegisterOutputFunctor* temp = xDynamicCast<RegisterOutputFunctor*>(this); temp->call(value); break;} 226 //case AD_SETADJOINT: {SetAdjointFunction* temp = xDynamicCast<SetAdjointFunction*>(this); temp->call(value); break;} 294 case AD_COUNTDOUBLES: {CountDoublesFunctor* temp = xDynamicCast<CountDoublesFunctor*>(this); temp->call(value); break;} 295 case AD_REGISTERINPUT: {RegisterInputFunctor* temp = xDynamicCast<RegisterInputFunctor*>(this); temp->call(value); break;} 296 case AD_REGISTEROUTPUT:{RegisterOutputFunctor* temp = xDynamicCast<RegisterOutputFunctor*>(this); temp->call(value); break;} 297 case AD_INITADJOINT: {InitAdjointFunctor* temp = xDynamicCast<InitAdjointFunctor*>(this); temp->call(value); break;} 298 case AD_SETADJOINT: {SetAdjointFunctor* temp = xDynamicCast<SetAdjointFunctor*>(this); temp->call(value); break;} 227 299 #endif 228 300 default: _error_("Operation "<<OperationNumber()<<" not supported yet"); … … 235 307 case MARSHALLING_SIZE: {SizeCheckpointFunctor* temp = xDynamicCast<SizeCheckpointFunctor*>(this); temp->call(value,size); break;} 236 308 #ifdef _HAVE_CODIPACK_ 237 case AD_COUNTDOUBLES: {CountDoublesFunctor* temp = xDynamicCast<CountDoublesFunctor*>(this); temp->call(value,size); break;} 238 case AD_REGISTERINPUT: {RegisterInputFunctor* temp = xDynamicCast<RegisterInputFunctor*>(this); temp->call(value,size); break;} 239 case AD_REGISTEROUTPUT:{RegisterOutputFunctor* temp = xDynamicCast<RegisterOutputFunctor*>(this); temp->call(value,size); break;} 240 //case AD_SETADJOINT: {SetAdjointFunction* temp = xDynamicCast<SetAdjointFunction*>(this); temp->call(value,size); break;} 309 case AD_COUNTDOUBLES: {CountDoublesFunctor* temp = xDynamicCast<CountDoublesFunctor*>(this); temp->call(value,size); break;} 310 case AD_REGISTERINPUT: {RegisterInputFunctor* temp = xDynamicCast<RegisterInputFunctor*>(this); temp->call(value,size); break;} 311 case AD_REGISTEROUTPUT:{RegisterOutputFunctor* temp = xDynamicCast<RegisterOutputFunctor*>(this); temp->call(value,size); break;} 312 case AD_INITADJOINT: {InitAdjointFunctor* temp = xDynamicCast<InitAdjointFunctor*>(this); temp->call(value,size); break;} 313 case AD_SETADJOINT: {SetAdjointFunctor* temp = xDynamicCast<SetAdjointFunctor*>(this); temp->call(value,size); break;} 241 314 #endif 242 315 default: _error_("Operation "<<OperationNumber() <<" not supported yet");
Note:
See TracChangeset
for help on using the changeset viewer.