00001 #include <stdlib.h>
00002 #include "string.h"
00003 #include "anamod.h"
00004 #include "petsc.h"
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 #define NALLOC 25
00058 #define FSETCOOKIE 9876
00059 #define CHECKVALIDFSET(i) {ANAMODCHECKVALID(i,FSETCOOKIE,"feature set");}
00060 struct FeatureSet_ {
00061 int cookie;
00062 AnalysisDataType *types; IntArray IDs; StringArray features;
00063 };
00064 #define FVALCOOKIE 9877
00065 #define CHECKVALIDFVAL(i) {ANAMODCHECKVALID(i,FVALCOOKIE,"feature values");}
00066 struct FeatureValues_ {
00067 int cookie;
00068 AnalysisItemArray values; AnalysisDataTypeArray types;
00069 };
00070
00071 #undef __FUNCT__
00072 #define __FUNCT__ "NewFeatureSet"
00073
00074 PetscErrorCode NewFeatureSet(FeatureSet *set)
00075 {
00076 FeatureSet fnew; PetscErrorCode ierr;
00077 PetscFunctionBegin;
00078 ierr = PetscMalloc(sizeof(struct FeatureSet_),&fnew); CHKERRQ(ierr);
00079 ierr = PetscMemzero(fnew,sizeof(struct FeatureSet_)); CHKERRQ(ierr);
00080 fnew->cookie = FSETCOOKIE;
00081 ierr = CreateIntArray("IDs",50,&(fnew->IDs)); CHKERRQ(ierr);
00082 ierr = CreateStringArray("features",50,&(fnew->features)); CHKERRQ(ierr);
00083 *set = fnew;
00084 PetscFunctionReturn(0);
00085 }
00086
00087 #undef __FUNCT__
00088 #define __FUNCT__ "DeleteFeatureSet"
00089
00090 PetscErrorCode DeleteFeatureSet(FeatureSet set)
00091 {
00092 PetscErrorCode ierr;
00093 PetscFunctionBegin;
00094 CHECKVALIDFSET(set);
00095 ierr = DeleteIntArray(set->IDs); CHKERRQ(ierr);
00096 ierr = DeleteStringArray(set->features); CHKERRQ(ierr);
00097 ierr = PetscFree(set); CHKERRQ(ierr);
00098 PetscFunctionReturn(0);
00099 }
00100
00101 #undef __FUNCT__
00102 #define __FUNCT__ "AddToFeatureSet"
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 PetscErrorCode AddToFeatureSet(FeatureSet set,char *cat,char *cmp,int *idx)
00115 {
00116 PetscErrorCode ierr;
00117 char *name; int l1,l2;
00118 PetscFunctionBegin;
00119 CHECKVALIDFSET(set);
00120
00121 l1 = strlen(cat); l2 = strlen(cmp);
00122 ierr = PetscMalloc((l1+l2+1)*sizeof(char),&name); CHKERRQ(ierr);
00123 sprintf(name,"%s/%s",cat,cmp);
00124 ierr = StringArrayAdd(set->features,name,idx); CHKERRQ(ierr);
00125 ierr = PetscFree(name); CHKERRQ(ierr);
00126 PetscFunctionReturn(0);
00127 }
00128
00129 #undef __FUNCT__
00130 #define __FUNCT__ "NewFeatureValues"
00131
00132 PetscErrorCode NewFeatureValues(FeatureValues *values)
00133 {
00134 FeatureValues fnew; PetscErrorCode ierr;
00135 PetscFunctionBegin;
00136 ierr = PetscMalloc(sizeof(struct FeatureValues_),&fnew); CHKERRQ(ierr);
00137 ierr = PetscMemzero(fnew,sizeof(struct FeatureValues_)); CHKERRQ(ierr);
00138 fnew->cookie = FVALCOOKIE;
00139 ierr = CreateAnalysisItemArray("values",50,&fnew->values); CHKERRQ(ierr);
00140 ierr = CreateAnalysisDataTypeArray("values",50,&fnew->types); CHKERRQ(ierr);
00141 *values = fnew;
00142 PetscFunctionReturn(0);
00143 }
00144
00145 #undef __FUNCT__
00146 #define __FUNCT__ "DeleteFeatureValues"
00147
00148 PetscErrorCode DeleteFeatureValues(FeatureValues values)
00149 {
00150 PetscErrorCode ierr;
00151 PetscFunctionBegin;
00152 CHECKVALIDFVAL(values);
00153 ierr = DeleteAnalysisItemArray(values->values); CHKERRQ(ierr);
00154 ierr = DeleteAnalysisDataTypeArray(values->types); CHKERRQ(ierr);
00155 ierr = PetscFree(values); CHKERRQ(ierr);
00156 PetscFunctionReturn(0);
00157 }
00158
00159 #undef __FUNCT__
00160 #define __FUNCT__ "InstantiateFeatureSet"
00161
00162 PetscErrorCode InstantiateFeatureSet
00163 (AnaModNumericalProblem prob,FeatureSet set,FeatureValues values)
00164 {
00165 PetscErrorCode (*retriever)
00166 (void*,char*,char*,AnalysisItem*,AnalysisDataType*,PetscTruth*);
00167 int nval,ival; PetscTruth flg; PetscErrorCode ierr;
00168 PetscFunctionBegin;
00169 CHECKVALIDFSET(set);
00170 CHECKVALIDFVAL(values);
00171 ierr = AnaModGetRetrievalFunction(&retriever,&flg); CHKERRQ(ierr);
00172 if (!flg) PetscFunctionReturn(0);
00173 ierr = StringArrayGetFill(set->features,&nval); CHKERRQ(ierr);
00174 for (ival=0; ival<nval; ival++) {
00175 char *feature,*cat,*cmp; int l;
00176 AnalysisItem res; AnalysisDataType t;
00177 ierr = StringArrayGetAt(set->features,ival,&feature); CHKERRQ(ierr);
00178 for (l=0; l<strlen(feature); l++) {
00179 if (feature[l]=='/') {
00180 feature[l] = 0; cat = feature; cmp = feature+l+1;
00181 ierr = (*retriever)(prob,cat,cmp,&res,&t,&flg); CHKERRQ(ierr);
00182 if (flg) {
00183 ierr = AnalysisItemArraySetAt(values->values,ival,res); CHKERRQ(ierr);
00184 ierr = AnalysisDataTypeArraySetAt(values->types,ival,t); CHKERRQ(ierr);
00185 }
00186 feature[l] = '/'; break;
00187 }
00188 }
00189 }
00190 PetscFunctionReturn(0);
00191 }
00192
00193 #undef __FUNCT__
00194 #define __FUNCT__ "GetFeatureValue"
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 PetscErrorCode GetFeatureValue
00206 (FeatureValues values,int index,AnalysisItem *val,PetscTruth *f)
00207 {
00208 PetscErrorCode ierr;
00209 PetscFunctionBegin;
00210 CHECKVALIDFVAL(values);
00211 ierr = AnalysisItemArrayTryGetAt(values->values,index,val,f); CHKERRQ(ierr);
00212 PetscFunctionReturn(0);
00213 }
00214
00215 static PetscErrorCode(*retriever)(void*,char*,char*,AnalysisItem*,AnalysisDataType*,PetscTruth*) = NULL;
00216 #undef __FUNCT__
00217 #define __FUNCT__ "AnaModSetRetrievalFunction"
00218 PetscErrorCode AnaModSetRetrievalFunction
00219 (PetscErrorCode(*fun)(void*,char*,char*,AnalysisItem*,AnalysisDataType*,PetscTruth*))
00220 {
00221 PetscFunctionBegin;
00222 retriever = fun;
00223 PetscFunctionReturn(0);
00224 }
00225
00226 #undef __FUNCT__
00227 #define __FUNCT__ "AnaModGetRetrievalFunction"
00228 PetscErrorCode AnaModGetRetrievalFunction
00229 (PetscErrorCode(**fun)(void*,char*,char*,AnalysisItem*,AnalysisDataType*,PetscTruth*),PetscTruth *flg)
00230 {
00231 PetscTruth has;
00232 PetscFunctionBegin;
00233 has = (PetscTruth)(retriever!=NULL);
00234 if (flg) *flg = has;
00235 if (has &&fun) *fun = retriever;
00236 PetscFunctionReturn(0);
00237 }