00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 #include <stdlib.h>
00142 #include <stdio.h>
00143 #include <string.h>
00144 #include "petscmat.h"
00145 #include "syspro.h"
00146 #include "sysprotransform.h"
00147 #include "syspro_impl.h"
00148
00149 #define NPREPROCESS 25
00150 SystemPreprocessor *preprocessors=NULL;
00151 char **currentpreprocessors,**currentchoices=NULL;
00152 int *currentoptions=NULL;
00153 int npreprocess=0,preprocesslevel;
00154 static void **preprocessorcontexts,*solutioncontext;
00155 static PetscErrorCode(**unsetpreprocessor)();
00156
00157 struct PreprocessorsGlobalInfo_ {
00158 PetscErrorCode(*problemmonitor)(NumericalProblem);
00159 PetscErrorCode(*classstaticsetup)(char*);
00160
00161
00162
00163
00164 PetscErrorCode(*classdynamicsetup)(char*,NumericalProblem);
00165
00166
00167
00168
00169
00170 PetscErrorCode(*classproblemcloner)(char*,char*,int,NumericalProblem,NumericalProblem);
00171
00172
00173
00174 PetscErrorCode(*computecategory)(char*,NumericalProblem);
00175
00176
00177
00178 PetscErrorCode(*metadatacomputer)(char*,char*,Mat,void*,PetscTruth*);
00179 PetscErrorCode(*clonecontext)(char*,char*,void*,void**);
00180 PetscErrorCode(*freecontext)(void*);
00181 PetscErrorCode(*problemsolver)(NumericalProblem,void*,NumericalSolution*);
00182 PetscErrorCode(*problemdelete)(NumericalProblem);
00183 PetscErrorCode(*errortracer)(NumericalProblem,NumericalSolution,char*);
00184 PetscErrorCode(*solutioncreator)(NumericalProblem,NumericalSolution*);
00185 PetscErrorCode(*solutioncopy)(NumericalSolution,NumericalSolution);
00186 PetscErrorCode(*solutiondelete)(NumericalSolution);
00187 PetscErrorCode(*solutioncontextdelete)(NumericalSolution);
00188 } ;
00189 typedef struct PreprocessorsGlobalInfo_* PreprocessorsGlobalInfo;
00190 static PreprocessorsGlobalInfo GlobalInfo = NULL;
00191
00192 #undef __FUNCT__
00193 #define __FUNCT__ "CreateGlobalInfo"
00194 static PetscErrorCode CreateGlobalInfo()
00195 {
00196 PetscErrorCode ierr;
00197 PetscFunctionBegin;
00198 if (!GlobalInfo) {
00199 ierr = PetscMalloc
00200 (sizeof(struct PreprocessorsGlobalInfo_),&GlobalInfo); CHKERRQ(ierr);
00201 ierr = PetscMemzero
00202 (GlobalInfo,sizeof(struct PreprocessorsGlobalInfo_)); CHKERRQ(ierr);
00203 }
00204 CHKMEMQ;
00205 PetscFunctionReturn(0);
00206 }
00207
00208 #undef __FUNCT__
00209 #define __FUNCT__ "SysProInitialize"
00210
00211 PetscErrorCode SysProInitialize()
00212 {
00213 PetscErrorCode ierr;
00214 PetscFunctionBegin;
00215 ierr = CreateGlobalInfo(); CHKERRQ(ierr);
00216 ierr = PetscMalloc
00217 (NPREPROCESS*sizeof(SystemPreprocessor),&preprocessors); CHKERRQ(ierr);
00218 ierr = PetscMalloc
00219 (NPREPROCESS*sizeof(char*),¤tpreprocessors); CHKERRQ(ierr);
00220 ierr = PetscMalloc
00221 (NPREPROCESS*sizeof(void*),&preprocessorcontexts); CHKERRQ(ierr);
00222 ierr = PetscMalloc
00223 (NPREPROCESS*sizeof(char*),¤tchoices); CHKERRQ(ierr);
00224 ierr = PetscMalloc
00225 (NPREPROCESS*sizeof(int),¤toptions); CHKERRQ(ierr);
00226 ierr = PetscMalloc
00227 (NPREPROCESS*sizeof(PetscErrorCode(*)()),&unsetpreprocessor); CHKERRQ(ierr);
00228 ierr = PetscMemzero
00229 (unsetpreprocessor,NPREPROCESS*sizeof(PetscErrorCode(*)())); CHKERRQ(ierr);
00230 CHKMEMQ;
00231 PetscFunctionReturn(0);
00232 }
00233
00234 #undef __FUNCT__
00235 #define __FUNCT__ "SysProFinalize"
00236 PetscErrorCode SysProFinalize()
00237 {
00238 int ip; PetscErrorCode ierr;
00239 PetscFunctionBegin;
00240 for (ip=0; ip<npreprocess; ip++) {
00241 SystemPreprocessor thisone = preprocessors[ip];
00242 if (unsetpreprocessor[ip]) {
00243 ierr = (unsetpreprocessor[ip])(); CHKERRQ(ierr);
00244 }
00245 if (thisone->preserved) {
00246 ierr = PetscFree(thisone->preserved); CHKERRQ(ierr);
00247 }
00248 ierr = PetscFree(thisone->name); CHKERRQ(ierr);
00249 ierr = DeregisterTransform(thisone->transform); CHKERRQ(ierr);
00250 ierr = PetscFree(thisone); CHKERRQ(ierr);
00251 }
00252 ierr = PetscFree(preprocessors); CHKERRQ(ierr);
00253 ierr = PetscFree(currentpreprocessors); CHKERRQ(ierr);
00254 ierr = PetscFree(preprocessorcontexts); CHKERRQ(ierr);
00255 ierr = PetscFree(currentchoices); CHKERRQ(ierr);
00256 ierr = PetscFree(currentoptions); CHKERRQ(ierr);
00257 ierr = PetscFree(unsetpreprocessor); CHKERRQ(ierr);
00258 ierr = PetscFree(GlobalInfo); CHKERRQ(ierr);
00259 CHKMEMQ;
00260 PetscFunctionReturn(0);
00261 }
00262
00263 #undef __FUNCT__
00264 #define __FUNCT__ "DeclarePreprocessor"
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 PetscErrorCode DeclarePreprocessor
00319 (char *name,
00320 PetscErrorCode(*this_preprocessor_setup)(),
00321 PetscErrorCode(*specific_setup)(NumericalProblem,SalsaTransform),
00322 PetscErrorCode(*specific_unset)(NumericalProblem),
00323 PetscErrorCode(*global_unset)(),
00324 PetscErrorCode(*ctxcreate)(NumericalProblem,void**),
00325 PetscErrorCode(*ctxdelete)(void*),
00326 PetscErrorCode(*start_function)(char*,int,PetscTruth,NumericalProblem,NumericalProblem*,void*,void**,PetscTruth*),
00327 PetscErrorCode(*end_function)(char*,PetscTruth,void*,void*,NumericalProblem,NumericalProblem,NumericalSolution,NumericalSolution)
00328 )
00329 {
00330 SystemPreprocessor snew;
00331 PetscErrorCode ierr;
00332
00333 PetscFunctionBegin;
00334 if (!preprocessors) SETERRQ(1,"Forgotten SysProInitialize");
00335 if (npreprocess>=NPREPROCESS)
00336 SETERRQ(1,"Can not declare further system preprocessors");
00337 {
00338 int p;
00339 for (p=0; p<npreprocess; p++)
00340 if (!strcmp(name,preprocessors[p]->name))
00341 SETERRQ1(1,"There already is a preprocessor called <%s>",name);
00342 }
00343 unsetpreprocessor[npreprocess] = global_unset;
00344 ierr = PetscMalloc(sizeof(struct SystemPreprocessor_),&snew); CHKERRQ(ierr);
00345 ierr = PetscMemzero(snew,sizeof(struct SystemPreprocessor_)); CHKERRQ(ierr);
00346 preprocessors[npreprocess++] = snew;
00347
00348 snew->exhaustive = PETSC_FALSE;
00349 ierr = PetscStrallocpy(name,&(snew->name)); CHKERRQ(ierr);
00350 ierr = NewTransform(name,&(snew->transform)); CHKERRQ(ierr);
00351 if (GlobalInfo->classstaticsetup) {
00352 ierr = (GlobalInfo->classstaticsetup)(name); CHKERRQ(ierr);
00353 }
00354 if (!this_preprocessor_setup)
00355 SETERRQ(1,"No setup function given");
00356 ierr = (*this_preprocessor_setup)(); CHKERRQ(ierr);
00357 snew->setup = specific_setup;
00358 snew->unset = specific_unset;
00359 snew->ctxcreate = ctxcreate; snew->ctxdelete = ctxdelete;
00360 snew->start_function = start_function; snew->end_function = end_function;
00361 CHKMEMQ;
00362 PetscFunctionReturn(0);
00363 }
00364
00365 #undef __FUNCT__
00366 #define __FUNCT__ "SysProDeclareProblemMonitor"
00367 PetscErrorCode SysProDeclareProblemMonitor
00368 (PetscErrorCode(*monitor)(NumericalProblem))
00369 {
00370 PetscFunctionBegin;
00371 if (monitor) GlobalInfo->problemmonitor = monitor;
00372 PetscFunctionReturn(0);
00373 }
00374
00375 #undef __FUNCT__
00376 #define __FUNCT__ "SysProDeclareErrorTracer"
00377 PetscErrorCode SysProDeclareErrorTracer
00378 (PetscErrorCode(*tracer)(NumericalProblem,NumericalSolution,char*))
00379 {
00380 PetscFunctionBegin;
00381 if (tracer) GlobalInfo->errortracer = tracer;
00382 PetscFunctionReturn(0);
00383 }
00384
00385 #undef __FUNCT__
00386 #define __FUNCT__ "SysProGetErrorTracer"
00387 PetscErrorCode SysProGetErrorTracer
00388 (PetscErrorCode(**tracer)(NumericalProblem,NumericalSolution,char*))
00389 {
00390 PetscFunctionBegin;
00391 if (tracer) *tracer = GlobalInfo->errortracer;
00392 PetscFunctionReturn(0);
00393 }
00394
00395 #undef __FUNCT__
00396 #define __FUNCT__ "DeclarePreprocessorIntelligentChoice"
00397
00398 PetscErrorCode DeclarePreprocessorIntelligentChoice
00399 (char *name,PetscErrorCode(*picker)(NumericalProblem,char**,char**))
00400 {
00401 SystemPreprocessor pp;
00402 PetscErrorCode ierr;
00403 PetscFunctionBegin;
00404 ierr = SystemPreprocessorGetByName(name,&pp); CHKERRQ(ierr);
00405 pp->intelligence = picker;
00406 PetscFunctionReturn(0);
00407 }
00408
00409 #undef __FUNCT__
00410 #define __FUNCT__ "NumericalProblemGetComm"
00411 PetscErrorCode NumericalProblemGetComm(NumericalProblem prob,MPI_Comm *comm)
00412 {
00413 PetscFunctionBegin;
00414 *comm = prob->comm;
00415 PetscFunctionReturn(0);
00416 }
00417
00418 #undef __FUNCT__
00419 #define __FUNCT__ "SysProDeclareFunctions"
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439 PetscErrorCode SysProDeclareFunctions
00440 (
00441 PetscErrorCode(*classstaticsetup)(char*),
00442 PetscErrorCode(*classdynamicsetup)(char*,NumericalProblem),
00443 PetscErrorCode(*classproblemcloner)(char*,char*,int,NumericalProblem,NumericalProblem),
00444 PetscErrorCode(*problemsolver)(NumericalProblem,void*,NumericalSolution*),
00445 PetscErrorCode(*problemdelete)(NumericalProblem),
00446 PetscErrorCode(*solutioncreator)(NumericalProblem,NumericalSolution*),
00447 PetscErrorCode(*solutioncopy)(NumericalSolution,NumericalSolution),
00448 PetscErrorCode(*solutiondelete)(NumericalSolution),
00449 PetscErrorCode(*ctxcloner)(char*,char*,void*,void**),
00450 PetscErrorCode(*ctxfree)(void*),
00451 PetscErrorCode(*solutioncontextdelete)(NumericalSolution)
00452 )
00453 {
00454 PetscFunctionBegin;
00455
00456 if (classstaticsetup) GlobalInfo->classstaticsetup = classstaticsetup;
00457 if (classdynamicsetup) GlobalInfo->classdynamicsetup = classdynamicsetup;
00458 if (classproblemcloner) GlobalInfo->classproblemcloner = classproblemcloner;
00459
00460 if (!problemsolver) SETERRQ(1,"A solver routine is required");
00461 GlobalInfo->problemsolver = problemsolver;
00462 GlobalInfo->problemdelete = problemdelete;
00463
00464 if (ctxcloner) GlobalInfo->clonecontext = ctxcloner;
00465 if (ctxfree) GlobalInfo->freecontext = ctxfree;
00466
00467 if (solutioncreator) GlobalInfo->solutioncreator = solutioncreator;
00468 if (solutioncopy) GlobalInfo->solutioncopy = solutioncopy;
00469 if (solutiondelete) GlobalInfo->solutiondelete = solutiondelete;
00470 if (solutioncontextdelete)
00471 GlobalInfo->solutioncontextdelete = solutioncontextdelete;
00472 CHKMEMQ;
00473
00474 PetscFunctionReturn(0);
00475 }
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489 #undef __FUNCT__
00490 #define __FUNCT__ "SysProGetContextFunctions"
00491 static PetscErrorCode SysProGetContextFunctions
00492 (PetscErrorCode(**ctxcloner)(char*,char*,void*,void**),
00493 PetscErrorCode(**ctxfree)(void*)
00494 )
00495 {
00496 PetscFunctionBegin;
00497 if (ctxcloner) *ctxcloner = GlobalInfo->clonecontext;
00498 if (ctxfree) *ctxfree = GlobalInfo->freecontext;
00499 CHKMEMQ;
00500 PetscFunctionReturn(0);
00501 }
00502
00503 static PetscErrorCode SysProProblemCloneContext
00504 (char *preprocessor,char *type,NumericalProblem in,NumericalProblem out)
00505 {
00506 PetscErrorCode(*cloner)(char*,char*,void*,void**),ierr;
00507 PetscFunctionBegin;
00508 ierr = SysProGetContextFunctions(&cloner,PETSC_NULL); CHKERRQ(ierr);
00509 if (cloner) {
00510 ierr = (*cloner)(preprocessor,type,in->ctx,&(out->ctx)); CHKERRQ(ierr);
00511 }
00512 CHKMEMQ;
00513 PetscFunctionReturn(0);
00514 }
00515
00516 static PetscErrorCode SysProProblemDeleteContext(NumericalProblem problem)
00517 {
00518 PetscErrorCode(*freer)(void*),ierr;
00519 PetscFunctionBegin;
00520 ierr = SysProGetContextFunctions(PETSC_NULL,&freer); CHKERRQ(ierr);
00521
00522
00523
00524
00525
00526 CHKMEMQ;
00527 PetscFunctionReturn(0);
00528 }
00529
00530 #undef __FUNCT__
00531 #define __FUNCT__ "RegisterPreprocessorSetting"
00532 static PetscErrorCode RegisterPreprocessorSetting
00533 (char *preprocess,char *type,int option)
00534 {
00535 PetscFunctionBegin;
00536 currentpreprocessors[preprocesslevel] = preprocess;
00537 currentchoices[preprocesslevel] = type;
00538 currentoptions[preprocesslevel] = option;
00539 CHKMEMQ;
00540 PetscFunctionReturn(0);
00541 }
00542
00543 #undef __FUNCT__
00544 #define __FUNCT__ "PreprocessorGetSetting"
00545 PetscErrorCode PreprocessorGetSetting(char *preprocess,char **type,int *option)
00546 {
00547 int i;
00548 PetscFunctionBegin;
00549 for (i=0; i<=preprocesslevel; i++) {
00550 if (!strcmp(currentpreprocessors[i],preprocess)) {
00551 *type = currentchoices[i];
00552 *option = currentoptions[i];
00553 goto found;
00554 }
00555 }
00556 SETERRQ1(1,"Prprocessor <%s> is not active",preprocess);
00557 found:
00558 CHKMEMQ;
00559 PetscFunctionReturn(0);
00560 }
00561
00562
00563 #undef __FUNCT__
00564 #define __FUNCT__ "RetrievePreprocessorChoice"
00565 PetscErrorCode RetrievePreprocessorChoice(int idx,char **type,int *option)
00566 {
00567 PetscFunctionBegin;
00568 *type = currentchoices[idx];
00569 *option = currentoptions[idx];
00570 CHKMEMQ;
00571 PetscFunctionReturn(0);
00572 }
00573
00574
00575 #undef __FUNCT__
00576 #define __FUNCT__ "PreprocessorGetIndex"
00577 PetscErrorCode PreprocessorGetIndex(char *name,int *prenumber)
00578 {
00579 int ipre; PetscTruth flg; PetscErrorCode ierr;
00580 PetscFunctionBegin;
00581 *prenumber = -1;
00582 for (ipre=0; ipre<npreprocess; ipre++) {
00583 ierr = PetscStrcmp(name,preprocessors[ipre]->name,&flg); CHKERRQ(ierr);
00584 if (flg) {
00585 *prenumber = ipre;
00586 goto found;
00587 }
00588 }
00589 SETERRQ1(1,"Could not find preprocessor <%s>",name);
00590 found:
00591 CHKMEMQ;
00592 PetscFunctionReturn(0);
00593 }
00594
00595 #undef __FUNCT__
00596 #define __FUNCT__ "SystemPreprocessorGetByName"
00597 PetscErrorCode SystemPreprocessorGetByName
00598 (char *name,SystemPreprocessor *pp)
00599 {
00600 int ipre; PetscErrorCode ierr;
00601 PetscFunctionBegin;
00602 ierr = PreprocessorGetIndex(name,&ipre); CHKERRQ(ierr);
00603 *pp = preprocessors[ipre];
00604 CHKMEMQ;
00605 PetscFunctionReturn(0);
00606 }
00607
00608 #undef __FUNCT__
00609 #define __FUNCT__ "TransformGetByName"
00610 PetscErrorCode TransformGetByName
00611 (char *name,SalsaTransform *tf)
00612 {
00613 SystemPreprocessor pp; PetscErrorCode ierr;
00614 PetscFunctionBegin;
00615 ierr = SystemPreprocessorGetByName(name,&pp); CHKERRQ(ierr);
00616 *tf = pp->transform;
00617 CHKMEMQ;
00618 PetscFunctionReturn(0);
00619 }
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644 #undef __FUNCT__
00645 #define __FUNCT__ "RegisterPreprocessorContext"
00646 PetscErrorCode RegisterPreprocessorContext(char *pre,void *ctx)
00647 {
00648 PetscErrorCode ierr;
00649 PetscFunctionBegin;
00650 if (!preprocessorcontexts) {
00651 SETERRQ(1,"Preprocessor contexts array should have been allocated");
00652 } else {
00653 PetscTruth flg; int ipre;
00654 ierr = PetscStrcmp(pre,"solution",&flg); CHKERRQ(ierr);
00655 if (flg) {
00656 solutioncontext = ctx;
00657 } else {
00658 ierr = PreprocessorGetIndex(pre,&ipre); CHKERRQ(ierr);
00659 preprocessorcontexts[ipre] = ctx;
00660 }
00661 }
00662 CHKMEMQ;
00663 PetscFunctionReturn(0);
00664 }
00665
00666 #undef __FUNCT__
00667 #define __FUNCT__ "PreprocessorGetContext"
00668 PetscErrorCode PreprocessorGetContext(char *pre,void **ctx)
00669 {
00670 PetscTruth flg; int ipre; PetscErrorCode ierr;
00671 PetscFunctionBegin;
00672 ierr = PetscStrcmp(pre,"solution",&flg); CHKERRQ(ierr);
00673 if (flg) {
00674 *ctx = solutioncontext;
00675 } else {
00676 ierr = PreprocessorGetIndex(pre,&ipre); CHKERRQ(ierr);
00677 if (!preprocessorcontexts[ipre])
00678 SETERRQ1(1,"No context set for preprocessor <%s>",pre);
00679 *ctx = preprocessorcontexts[ipre];
00680 }
00681 CHKMEMQ;
00682 PetscFunctionReturn(0);
00683 }
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694 #undef __FUNCT__
00695 #define __FUNCT__ "PreprocessorSpecificSetup"
00696 static PetscErrorCode PreprocessorSpecificSetup
00697 (char *preprocess,NumericalProblem problem,PetscTruth user_choices)
00698 {
00699 SystemPreprocessor pp; SalsaTransform tf; PetscErrorCode ierr;
00700 PetscFunctionBegin;
00701 ierr = SystemPreprocessorGetByName(preprocess,&pp); CHKERRQ(ierr);
00702 ierr = TransformGetByName(preprocess,&tf); CHKERRQ(ierr);
00703
00704 if (GlobalInfo->problemmonitor) {
00705 ierr = (GlobalInfo->problemmonitor)(problem); CHKERRQ(ierr);
00706 }
00707
00708
00709
00710
00711 if (!user_choices) {
00712
00713
00714
00715 ierr = PreprocessorApplyAprioriSelection(pp); CHKERRQ(ierr);
00716
00717 if (pp->setup) {
00718 ierr = (pp->setup)(problem,tf); CHKERRQ(ierr);
00719 }
00720 }
00721
00722
00723
00724
00725 {
00726 int i;
00727 for (i=0; i<tf->n_objects; i++) {
00728 SalsaTransformObject to = tf->transformobjects[i]; void *sctx;
00729 PetscErrorCode(*f)(NumericalProblem,void*,SuitabilityValue*);
00730 ierr = TransformObjectGetSuitabilityFunction(to,&sctx,&f); CHKERRQ(ierr);
00731 if (f) {
00732 SuitabilityValue v;
00733 ierr = (*f)(problem,sctx,&v); CHKERRQ(ierr);
00734 printf("method %s compute suitability as %e\n",to->name,v);
00735 if (v==0) {
00736 ierr = TransformObjectMark(to); CHKERRQ(ierr);
00737 }
00738 }
00739 }
00740 }
00741 ierr = ReportEnabledPreprocessors(preprocess); CHKERRQ(ierr);
00742 CHKMEMQ;
00743 PetscFunctionReturn(0);
00744 }
00745
00746 #undef __FUNCT__
00747 #define __FUNCT__ "SysproPreprocessorStartFunction"
00748 static PetscErrorCode SysproPreprocessorStartFunction
00749 (SystemPreprocessor preprocessor,char *type,int option,PetscTruth overwrite,
00750 NumericalProblem inproblem,NumericalProblem *outproblem,
00751 void *preprocessor_context,void **transform_context, PetscTruth *success)
00752 {
00753 PetscErrorCode ierr;
00754 PetscFunctionBegin;
00755 if (!(preprocessor->start_function))
00756 SETERRQ1(1,"Preprocessor <%s> has no start function",preprocessor->name);
00757 ierr = (preprocessor->start_function)
00758 (type,option,overwrite,inproblem,outproblem,
00759 preprocessor_context,transform_context,success); CHKERRQ(ierr);
00760 if (!*success) PetscFunctionReturn(0);
00761
00762
00763
00764
00765
00766
00767 CHKMEMQ;
00768 PetscFunctionReturn(0);
00769 }
00770
00771 #undef __FUNCT__
00772 #define __FUNCT__ "SysProPreprocessorEndFunction"
00773 static PetscErrorCode SysProPreprocessorEndFunction
00774 (SystemPreprocessor preprocessor,char *pclassname,char *type,
00775 PetscTruth do_not_keep_original,void *gctx,void *ctx,
00776 NumericalProblem next_problem,NumericalProblem problem,
00777 NumericalSolution next_solution,NumericalSolution *rsolution)
00778 {
00779 NumericalSolution solution; PetscErrorCode ierr;
00780 PetscFunctionBegin;
00781
00782
00783
00784
00785 if (next_solution &&
00786 (preprocessor->end_function || GlobalInfo->solutioncopy)) {
00787 if (!GlobalInfo->solutioncreator)
00788 SETERRQ1(1,"Preprocessor %s requires solution creator",pclassname);
00789 if (!GlobalInfo->solutiondelete)
00790 SETERRQ1(1,"Preprocessor %s requires solution destructor",pclassname);
00791 ierr = (GlobalInfo->solutioncreator)(problem,&solution); CHKERRQ(ierr);
00792 if (preprocessor->end_function) {
00793 ierr = (preprocessor->end_function)
00794 (type,do_not_keep_original,gctx,ctx,
00795 next_problem,problem,next_solution,solution); CHKERRQ(ierr);
00796 } else if (GlobalInfo->solutioncopy && next_solution) {
00797 ierr = (GlobalInfo->solutioncopy)(next_solution,solution); CHKERRQ(ierr);
00798 }
00799 ierr = (GlobalInfo->solutiondelete)(next_solution); CHKERRQ(ierr);
00800 } else solution = next_solution;
00801
00802 if (GlobalInfo->problemdelete) {
00803 ierr = (GlobalInfo->problemdelete)(next_problem); CHKERRQ(ierr);
00804 }
00805 ierr = SysProProblemDeleteContext(next_problem); CHKERRQ(ierr);
00806 if (!do_not_keep_original) { }
00807 if (GlobalInfo->errortracer) {
00808 char caption[200];
00809 sprintf(caption,"After preprocessor <%s>=<%s>",pclassname,type);
00810 ierr = (GlobalInfo->errortracer)
00811 (problem,solution,caption); CHKERRQ(ierr);
00812 }
00813 *rsolution = solution;
00814 PetscFunctionReturn(0);
00815 }
00816
00817 #undef __FUNCT__
00818 #define __FUNCT__ "ChooseFirstTransform"
00819 static PetscErrorCode ChooseFirstTransform
00820 (NumericalProblem problem,char *preprocess,PetscTruth user_choices,PetscTruth exhaustive,
00821 SalsaTransform transform,SystemPreprocessor preprocessor,
00822 SalsaTransformObject *transformitem,PetscTruth *moretransform,
00823 char **type)
00824 {
00825 PetscErrorCode ierr;
00826 PetscFunctionBegin;
00827 if (user_choices || exhaustive || !preprocessor->intelligence ) {
00828 ierr = TransformGetNextUnmarkedItem
00829 (transform,NULL,transformitem,moretransform); CHKERRQ(ierr);
00830 if (!*moretransform)
00831 SETERRQ1(1,"Can not find any <%s>\n",preprocessor->name);
00832 ierr = TransformObjectGetName(*transformitem,type); CHKERRQ(ierr);
00833 ierr = SysProTraceMessage
00834 ("Preprocessor choice: %s=%s\n",preprocess,*type); CHKERRQ(ierr);
00835 } else {
00836 char *reason;
00837
00838
00839
00840
00841 *moretransform = PETSC_TRUE;
00842 if (GlobalInfo->computecategory) {
00843 char *c;
00844 c = strtok(preprocessor->required,",");
00845 while (c) {
00846 ierr = (GlobalInfo->computecategory)(c,problem); CHKERRQ(ierr);
00847 c = strtok(NULL,",");
00848 }
00849 }
00850 ierr = (preprocessor->intelligence)(problem,type,&reason); CHKERRQ(ierr);
00851 ierr = SysProTraceMessage
00852 ("Intelligent choice <%s=%s> because <%s>\n",preprocess,*type,reason); CHKERRQ(ierr);
00853 }
00854 CHKMEMQ;
00855 PetscFunctionReturn(0);
00856 }
00857
00858 #undef __FUNCT__
00859 #define __FUNCT__ "PreprocessedSolution"
00860
00861
00862
00863
00864
00865
00866
00867 PetscErrorCode PreprocessedSolution
00868 (char *pclassname,NumericalProblem problem,void *prevctx,
00869 NumericalSolution *rsolution)
00870 {
00871 NumericalSolution solution=NULL; SystemPreprocessor preprocessor;
00872 SalsaTransform transform; SalsaTransformObject transformitem;
00873 PetscTruth exhaustive,do_not_keep_original,success,moretransform,user_choices;
00874 char *type;
00875 void *preprocessor_context;
00876 PetscErrorCode ierr;
00877
00878 PetscFunctionBegin;
00879
00880 ierr = ReportSysProCallStackState(pclassname); CHKERRQ(ierr);
00881
00882 ierr = SystemPreprocessorGetByName
00883 (pclassname,&preprocessor); CHKERRQ(ierr);
00884 transform = preprocessor->transform;
00885 exhaustive = preprocessor->exhaustive;
00886 ierr = TransformGetUserChoices(transform,&user_choices); CHKERRQ(ierr);
00887 CHKMEMQ;
00888
00889
00890
00891
00892
00893 if (GlobalInfo->classdynamicsetup) {
00894 ierr = (GlobalInfo->classdynamicsetup)(pclassname,problem); CHKERRQ(ierr);
00895 }
00896
00897
00898
00899
00900
00901 ierr = PreprocessorSpecificSetup
00902 (pclassname,problem,user_choices); CHKERRQ(ierr);
00903
00904
00905
00906
00907
00908 if (preprocessor->ctxcreate) {
00909 ierr = (preprocessor->ctxcreate)
00910 (problem,&preprocessor_context); CHKERRQ(ierr);
00911 ierr = RegisterPreprocessorContext
00912 (pclassname,preprocessor_context); CHKERRQ(ierr)
00913 }
00914
00915
00916
00917
00918 ierr = ChooseFirstTransform
00919 (problem,pclassname,user_choices,exhaustive,transform,preprocessor,
00920 &transformitem,&moretransform,&type); CHKERRQ(ierr);
00921
00922
00923
00924
00925 do_not_keep_original = TRUTH(!exhaustive && !GlobalInfo->errortracer);
00926 CHKMEMQ;
00927
00928
00929
00930
00931 while (moretransform) {
00932 char *next_solver; int option;
00933 PetscTruth optionsloop,optionflg,solution_is_allocated = PETSC_FALSE;
00934
00935 optionsloop = exhaustive;
00936
00937
00938
00939
00940
00941 ierr = TransformItemGetFirstOption
00942 (pclassname,type,&option,&optionflg); CHKERRQ(ierr);
00943 if (!optionflg) {optionflg = PETSC_TRUE; option = 0;}
00944
00945 while (optionflg) {
00946 void *transform_context; PetscTruth didaniteration=PETSC_TRUE;
00947 NumericalProblem next_problem; NumericalSolution next_solution;
00948
00949 ierr = SysProTraceMessage
00950 ("Preprocessor <%s=%s,%d>\n",pclassname,type,option); CHKERRQ(ierr);
00951 ierr = RegisterPreprocessorSetting(pclassname,type,option); CHKERRQ(ierr);
00952
00953
00954
00955
00956 solution_is_allocated = PETSC_FALSE;
00957 ierr = SysproPreprocessorStartFunction
00958 (preprocessor,type,option,do_not_keep_original,problem,&next_problem,
00959 preprocessor_context,&transform_context,&success); CHKERRQ(ierr);
00960 if (!success) {
00961 ierr = SysProTraceMessage
00962 ("Preprocessor breakdown <%s=%s,%d>\n",pclassname,type,option); CHKERRQ(ierr);
00963 didaniteration = PETSC_FALSE; goto next;
00964 }
00965 ierr = SysProProblemCloneContext
00966 (pclassname,type,problem,next_problem); CHKERRQ(ierr);
00967 CHKMEMQ;
00968
00969
00970
00971
00972
00973 ierr = SuccessorPreprocessor(pclassname,&next_solver); CHKERRQ(ierr);
00974 preprocesslevel++;
00975 if (next_solver) {
00976 ierr = PreprocessedSolution
00977 (next_solver,next_problem,transform_context,
00978 &next_solution); CHKERRQ(ierr);
00979 } else if (GlobalInfo->problemsolver) {
00980 ierr = (GlobalInfo->problemsolver)
00981 (next_problem,transform_context,&next_solution); CHKERRQ(ierr);
00982 } else SETERRQ(1,"No problem solving routine set");
00983 preprocesslevel--;
00984 CHKMEMQ;
00985
00986
00987
00988
00989 ierr = SysProPreprocessorEndFunction
00990 (preprocessor,pclassname,type,
00991 do_not_keep_original,preprocessor_context,transform_context,
00992 next_problem,problem,next_solution,&solution); CHKERRQ(ierr);
00993 solution_is_allocated = (PetscTruth)(solution!=next_solution);
00994 next:
00995 CHKMEMQ;
00996 optionflg = optionsloop;
00997 if (optionflg) {
00998 ierr = TransformItemGetNextOption
00999 (pclassname,type,&option,&optionflg); CHKERRQ(ierr);
01000 }
01001 if (didaniteration && optionflg && solution) {
01002
01003
01004
01005 if (GlobalInfo->solutiondelete) {
01006 ierr = (GlobalInfo->solutiondelete)(solution); CHKERRQ(ierr);
01007 } else
01008 SETERRQ1
01009 (1,"Option looping (for %s) requires solution destructor",
01010 pclassname);
01011 if (GlobalInfo->solutioncontextdelete) {
01012
01013
01014
01015
01016 }
01017 }
01018 }
01019 CHKMEMQ;
01020
01021 if (exhaustive) {
01022 ierr = TransformGetNextUnmarkedItem
01023 (transform,type,&transformitem,&moretransform); CHKERRQ(ierr);
01024 if (moretransform) {
01025 ierr = TransformObjectGetName(transformitem,&type); CHKERRQ(ierr);
01026 }
01027 } else moretransform = PETSC_FALSE;
01028 if (solution && moretransform) {
01029
01030
01031
01032 if (GlobalInfo->solutiondelete) {
01033 ierr = (GlobalInfo->solutiondelete)(solution); CHKERRQ(ierr);
01034 } else
01035 SETERRQ1
01036 (1,"Preprocessor looping (for %s) requires solution destructor",
01037 preprocessor);
01038 if (GlobalInfo->solutioncontextdelete) {
01039 ierr = (GlobalInfo->solutioncontextdelete)(solution); CHKERRQ(ierr);
01040 }
01041 }
01042 }
01043
01044 if (preprocessor->unset) {
01045 ierr = (preprocessor->unset)(problem); CHKERRQ(ierr);
01046 }
01047 if (preprocessor->ctxdelete) {
01048 ierr = (preprocessor->ctxdelete)(preprocessor_context); CHKERRQ(ierr);
01049 }
01050 CHKMEMQ;
01051
01052 *rsolution = solution;
01053
01054 PetscFunctionReturn(0);
01055 }
01056
01057 #undef __FUNCT__
01058 #define __FUNCT__ "PreprocessedProblemSolving"
01059
01060
01061
01062 PetscErrorCode PreprocessedProblemSolving
01063 (NumericalProblem problem,NumericalSolution *solution)
01064 {
01065 char *preprocess; PetscErrorCode ierr;
01066 PetscFunctionBegin;
01067 preprocesslevel = 0;
01068 ierr = GetFirstPreprocessor(&preprocess); CHKERRQ(ierr);
01069 if (!preprocess) {
01070 if (GlobalInfo->problemsolver) {
01071 ierr = (GlobalInfo->problemsolver)(problem,NULL,solution); CHKERRQ(ierr);
01072 if (GlobalInfo->errortracer) {
01073 ierr = (GlobalInfo->errortracer)
01074 (problem,*solution,"After main solve"); CHKERRQ(ierr);
01075 }
01076 } else SETERRQ(1,"No problem solving routine set");
01077 } else {
01078 ierr = PreprocessedSolution
01079 (preprocess,problem,NULL,solution); CHKERRQ(ierr);
01080 }
01081 CHKMEMQ;
01082 PetscFunctionReturn(0);
01083 }