11 #include <fei_fstream.hpp>
12 #include <fei_iostream.hpp>
14 #include <test_utils/BCNodeSet.hpp>
15 #include <test_utils/CRSet.hpp>
16 #include <test_utils/CommNodeSet.hpp>
17 #include <test_utils/ElemBlock.hpp>
18 #include <test_utils/AccessPattern.hpp>
19 #include <test_utils/DataReader.hpp>
22 DataReader::DataReader()
33 numCoefAccessPatterns_(0),
34 accessPatterns_(NULL),
45 numSharedNodeSets_(0),
46 sharedNodeSets_(NULL),
47 numFieldsRead_(false),
48 numElemBlocksRead_(false),
49 currentElemBlockIndex_(0),
58 DataReader::~DataReader() {
61 numElemBlocksRead_ =
false;
62 numFieldsRead_ =
false;
66 void DataReader::deleteMemory() {
67 for(
int i=0; i<numParams_; i++) {
68 delete [] paramStrings_[i];
70 delete [] paramStrings_;
73 delete [] accessPatterns_;
74 numCoefAccessPatterns_ = 0;
76 delete [] coefAccesses_;
79 delete [] elemBlocks_;
83 delete [] fieldSizes_;
86 delete [] sharedNodeSets_;
87 numSharedNodeSets_ = 0;
89 delete [] crMultSets_;
98 delete [] bcNodeSets_;
103 int DataReader::readData(
const char* fileName) {
105 FEI_IFSTREAM* instr =
new FEI_IFSTREAM(fileName);
108 fei::console_out() <<
"DataReader::readData: ERROR opening " << fileName << FEI_ENDL;
112 char* keyword = NULL;
114 int err = getKeyword(instr, keyword);
116 while (!instr->eof() && !err) {
117 readData(instr, keyword);
121 err = getKeyword(instr, keyword);
131 int DataReader::getKeyword(FEI_ISTREAM* instr,
char*& keyword) {
132 int err = skipWhite(instr);
133 if (err)
return(err);
135 char *temp =
new char[256];
136 for(
int i=0; i<256; i++) temp[i] =
'\0';
140 }
while ((std::strlen(temp) == 0) && (!instr->eof()));
142 keyword =
new char[std::strlen(temp)+1];
143 std::strcpy(keyword, temp);
151 int DataReader::is_reg_char(
char c) {
153 if (i<1 || i>126)
return(0);
159 int DataReader::skipWhite(FEI_ISTREAM* instr) {
163 if (!is_reg_char(c)) {
167 while(c ==
'#' || c ==
'\n' || c ==
' ') {
169 char* buf =
new char[128];
170 for(
int i=0; i<128; i++) buf[i] =
'\0';
171 instr->getline(buf, 128);
177 if (instr->eof())
return(1);
178 if ((
int)c == EOF)
return(1);
180 if (!is_reg_char(c)) {
190 void DataReader::readData(FEI_ISTREAM* instr,
char* keyword) {
192 if (!std::strcmp(
"solveType", keyword)) {
193 readData(instr, solveType_);
197 if (!std::strcmp(
"parameters", keyword)) {
199 readData(instr, tmp);
201 char** newParams =
new char*[numParams_ + tmp];
202 for(
int pp=0; pp<numParams_; pp++) newParams[pp] = paramStrings_[pp];
204 for(
int i=numParams_; i<numParams_+tmp; i++) {
205 char* buf =
new char[256];
206 for(
int j=0; j<256; j++) buf[j] =
'\0';
209 instr->getline(buf, 128);
211 newParams[i] =
new char[std::strlen(buf)+2];
212 std::strcpy(newParams[i], buf);
217 delete [] paramStrings_;
218 paramStrings_ = newParams;
224 if (!std::strcmp(
"numFields", keyword)) {
225 readData(instr, numFields_);
227 fieldSizes_ =
new int[numFields_];
228 fieldIDs_ =
new int[numFields_];
230 numFieldsRead_ =
true;
234 if (!std::strcmp(
"fieldIDs", keyword)) {
235 for(
int i=0; i<numFields_; i++) readData(instr, fieldIDs_[i]);
239 if (!std::strcmp(
"fieldSizes", keyword)) {
240 for(
int i=0; i<numFields_; i++) readData(instr, fieldSizes_[i]);
244 if (!std::strcmp(
"numElemBlocks", keyword)) {
245 if (numElemBlocks_ > 0) {
246 FEI_COUT <<
"DataReader: Caution, re-setting numElemBlocks." << FEI_ENDL;
247 delete [] elemBlocks_;
250 readData(instr, numElemBlocks_);
252 elemBlocks_ =
new ElemBlock[numElemBlocks_];
254 numElemBlocksRead_ =
true;
255 currentElemBlockIndex_ = -1;
260 if (!std::strcmp(
"blockID", keyword)) {
261 currentElemBlockIndex_++;
262 currentElemIndex_ = 0;
264 if (!numElemBlocksRead_) {
265 FEI_COUT <<
"DataReader: ERROR, numElemBlocks not read before blockID."
270 ElemBlock& eb1 = elemBlocks_[currentElemBlockIndex_];
273 readData(instr, tmp);
274 eb1.blockID_ = (GlobalID)tmp;
279 if (!std::strcmp(
"interleaveStrategy", keyword)) {
280 ElemBlock& eb2 = elemBlocks_[currentElemBlockIndex_];
283 readData(instr, interleave);
285 eb2.interleaveStrategy_ = interleave;
290 if (!std::strcmp(
"numElements", keyword)) {
291 ElemBlock& eb3 = elemBlocks_[currentElemBlockIndex_];
294 readData(instr, numElems);
296 eb3.numElements_ = numElems;
297 eb3.elemIDs_ =
new GlobalID[numElems];
298 eb3.elemConn_ =
new GlobalID*[numElems];
299 eb3.elemStiff_ =
new double**[numElems];
300 eb3.elemLoad_ =
new double*[numElems];
305 if (!std::strcmp(
"numNodesPerElement", keyword)) {
306 ElemBlock& eb4 = elemBlocks_[currentElemBlockIndex_];
309 readData(instr, numNodes);
311 eb4.numNodesPerElement_ = numNodes;
312 eb4.numFieldsPerNode_ =
new int[numNodes];
313 eb4.nodalFieldIDs_ =
new int*[numNodes];
315 for(
int i=0; i<eb4.numElements_; i++) {
316 eb4.elemConn_[i] =
new GlobalID[numNodes];
322 if (!std::strcmp(
"numElemDOF", keyword)) {
323 ElemBlock& eb5 = elemBlocks_[currentElemBlockIndex_];
325 readData(instr, edof);
326 eb5.numElemDOF_ = edof;
329 eb5.elemDOFFieldIDs_ =
new int[edof];
335 if (!std::strcmp(
"elemDOFFieldIDs", keyword)) {
336 ElemBlock& eb6 = elemBlocks_[currentElemBlockIndex_];
337 int edof = eb6.numElemDOF_;
339 for(
int i=0; i<edof; i++) {
341 readData(instr, eDofFieldID);
342 eb6.elemDOFFieldIDs_[i] = eDofFieldID;
347 if (!std::strcmp(
"elemFormat", keyword)) {
348 ElemBlock& eb7 = elemBlocks_[currentElemBlockIndex_];
352 eb7.elemFormat_ = ef;
356 if (!std::strcmp(
"numFieldsPerNode", keyword)) {
357 ElemBlock& eb8 = elemBlocks_[currentElemBlockIndex_];
360 for(i=0; i<eb8.numNodesPerElement_; i++) {
363 eb8.numFieldsPerNode_[i] = nf;
364 eb8.nodalFieldIDs_[i] =
new int[nf];
370 if (!std::strcmp(
"nodalFieldIDs", keyword)) {
371 ElemBlock& eb9 = elemBlocks_[currentElemBlockIndex_];
373 int i, numStiffRows = 0;
374 for(i=0; i<eb9.numNodesPerElement_; i++) {
375 for(
int j=0; j<eb9.numFieldsPerNode_[i]; j++) {
377 readData(instr, nfid);
378 eb9.nodalFieldIDs_[i][j] = nfid;
380 numStiffRows += getFieldSize(nfid);
384 numStiffRows += eb9.numElemDOF_;
386 eb9.numStiffRows_ = numStiffRows;
388 for(i=0; i<eb9.numElements_; i++) {
389 eb9.elemStiff_[i] =
new double*[numStiffRows];
390 eb9.elemLoad_[i] =
new double[numStiffRows];
391 for(
int j=0; j<numStiffRows; j++) {
392 eb9.elemStiff_[i][j] =
new double[numStiffRows];
399 if (!std::strcmp(
"elemID", keyword)) {
400 ElemBlock& eb10 = elemBlocks_[currentElemBlockIndex_];
403 readData(instr, tmp);
404 eb10.elemIDs_[currentElemIndex_] = (GlobalID)tmp;
406 int* conn = eb10.elemConn_[currentElemIndex_];
408 for(i=0; i<eb10.numNodesPerElement_; i++) {
409 readData(instr, conn[i]);
412 double** stiff = eb10.elemStiff_[currentElemIndex_];
414 for(i=0; i<eb10.numStiffRows_; i++) {
415 for(
int j=0; j<eb10.numStiffRows_; j++) {
416 readData(instr, stiff[i][j]);
420 double* load = eb10.elemLoad_[currentElemIndex_];
422 for(i=0; i<eb10.numStiffRows_; i++) {
423 readData(instr, load[i]);
431 if (!std::strcmp(
"numSharedNodeSets", keyword)) {
432 readData(instr, numSharedNodeSets_);
434 if (numSharedNodeSets_ == 0)
return;
436 sharedNodeSets_ =
new CommNodeSet[numSharedNodeSets_];
440 for(
int i=0; i<numSharedNodeSets_; i++) {
441 CommNodeSet& shSet = sharedNodeSets_[currentShIndex_++];
445 shSet.numNodes_ = nn;
447 shSet.nodeIDs_ =
new GlobalID[nn];
449 for(j=0; j<nn; j++) {
451 readData(instr, tmp);
452 shSet.nodeIDs_[j] = (GlobalID)tmp;
455 shSet.procsPerNode_ =
new int[nn];
457 for(j=0; j<nn; j++) {
459 readData(instr, tmp);
460 shSet.procsPerNode_[j] = tmp;
463 shSet.procs_ =
new int*[nn];
465 for(j=0; j<nn; j++) {
466 shSet.procs_[j] =
new int[shSet.procsPerNode_[j]];
467 for(
int k=0; k<shSet.procsPerNode_[j]; k++) {
468 readData(instr, shSet.procs_[j][k]);
476 if (!std::strcmp(
"numBCNodeSets", keyword)) {
477 readData(instr, numBCNodeSets_);
479 if (numBCNodeSets_ == 0)
return;
481 bcNodeSets_ =
new BCNodeSet[numBCNodeSets_];
485 for(
int i=0; i<numBCNodeSets_; i++) {
486 BCNodeSet& bcSet = bcNodeSets_[currentBCIndex_++];
490 bcSet.numNodes_ = nn;
492 readData(instr, bcSet.fieldID_);
495 readData(instr, field_offset);
497 bcSet.nodeIDs_ =
new GlobalID[nn];
498 bcSet.offsetsIntoField_ =
new int[nn];
499 bcSet.prescribed_values_ =
new double[nn];
501 for(j=0; j<nn; ++j) bcSet.offsetsIntoField_[j] = field_offset;
503 for(j=0; j<nn; j++) {
504 readData(instr, bcSet.nodeIDs_[j]);
505 readData(instr, bcSet.prescribed_values_[j]);
512 if (!std::strcmp(
"numCRMultSets", keyword)) {
513 readData(instr, numCRMultSets_);
515 if (numCRMultSets_ == 0)
return;
517 crMultSets_ =
new CRSet[numCRMultSets_];
519 for(
int i=0; i<numCRMultSets_; i++) {
520 CRSet& cr1 = crMultSets_[i];
523 readData(instr, dummy);
526 cr1.nodeIDs_ =
new GlobalID*[1];
531 cr1.nodeIDs_[j] =
new GlobalID[cr1.
numNodes_];
534 readData(instr, cr1.nodeIDs_[j][k]);
539 readData(instr, cr1.fieldIDs_[j]);
544 len += getFieldSize(cr1.fieldIDs_[j]);
546 cr1.weights_ =
new double[len];
550 int size = getFieldSize(cr1.fieldIDs_[j]);
552 for(
int k=0; k<size; k++) {
553 readData(instr, cr1.weights_[offset++]);
557 cr1.values_ =
new double[1];
559 readData(instr, cr1.values_[j]);
566 if (!std::strcmp(
"numCoefAccessPatterns", keyword)) {
567 readData(instr, numCoefAccessPatterns_);
569 if (numCoefAccessPatterns_ == 0)
return;
571 accessPatterns_ =
new AccessPattern[numCoefAccessPatterns_];
572 for(
int i=0; i<numCoefAccessPatterns_; i++) {
573 AccessPattern& patt = accessPatterns_[i];
575 readData(instr, patt.ID_);
576 readData(instr, patt.numRowIDs_);
577 if (patt.numRowIDs_ <= 0) {
582 patt.numFieldsPerRow_ =
new int[patt.numRowIDs_];
584 for(j=0; j<patt.numRowIDs_; j++) {
585 readData(instr, patt.numFieldsPerRow_[j]);
588 patt.rowFieldIDs_ =
new int*[patt.numRowIDs_];
589 for(j=0; j<patt.numRowIDs_; j++) {
590 patt.rowFieldIDs_[j] =
new int[patt.numFieldsPerRow_[j]];
593 for(
int r=0; r<patt.numRowIDs_; r++) {
594 for(
int c=0; c<patt.numFieldsPerRow_[r]; c++) {
595 readData(instr, patt.rowFieldIDs_[r][c]);
599 readData(instr, patt.numColIDsPerRow_);
600 if (patt.numColIDsPerRow_ <= 0) {
605 patt.numFieldsPerCol_ =
new int[patt.numColIDsPerRow_];
606 for(j=0; j<patt.numColIDsPerRow_; j++) {
607 readData(instr, patt.numFieldsPerCol_[j]);
610 patt.colFieldIDs_ =
new int*[patt.numColIDsPerRow_];
611 for(j=0; j<patt.numColIDsPerRow_; j++) {
612 patt.colFieldIDs_[j] =
new int[patt.numFieldsPerCol_[j]];
615 for(
int rr=0; rr<patt.numColIDsPerRow_; rr++) {
616 for(
int c=0; c<patt.numFieldsPerCol_[rr]; c++) {
617 readData(instr, patt.colFieldIDs_[rr][c]);
621 readData(instr, patt.interleaveStrategy_);
627 if (!std::strcmp(
"coefAccess", keyword)) {
628 int i, patternID = -1;
629 readData(instr, patternID);
633 for(i=0; i<numCoefAccessPatterns_; i++) {
634 if (accessPatterns_[i].ID_ == patternID) index = i;
638 fei::console_out() <<
"DataReader ERROR, patternID " << patternID <<
" not found."<<FEI_ENDL;
642 AccessPattern& patt = accessPatterns_[index];
645 CoefAccess* newAccesses =
new CoefAccess[numCoefAccesses_+1];
646 for(i=0; i<numCoefAccesses_; i++) newAccesses[i] = coefAccesses_[i];
648 delete [] coefAccesses_;
649 coefAccesses_ = newAccesses;
651 CoefAccess& coefAcc = coefAccesses_[numCoefAccesses_];
653 coefAcc.patternID_ = patternID;
655 coefAcc.numRowIDs_ = patt.numRowIDs_;
656 coefAcc.numColIDsPerRow_ = patt.numColIDsPerRow_;
658 if (coefAcc.numRowIDs_ <= 0 || coefAcc.numColIDsPerRow_ <= 0) {
659 fei::console_out() <<
"DataReader ERROR, coef-access has 0 rows or cols." << FEI_ENDL;
663 coefAcc.rowIDs_ =
new GlobalID[coefAcc.numRowIDs_];
664 for(i=0; i<coefAcc.numRowIDs_; i++) {
665 readData(instr, coefAcc.rowIDs_[i]);
668 int len = coefAcc.numRowIDs_ * coefAcc.numColIDsPerRow_;
669 coefAcc.colIDs_ =
new GlobalID[len];
670 for(i=0; i<len; i++) {
671 readData(instr, coefAcc.colIDs_[i]);
675 coefAcc.numRowCoefs_ = 0;
676 for(i=0; i<coefAcc.numRowIDs_; i++) {
677 for(
int j=0; j<patt.numFieldsPerRow_[i]; j++) {
678 coefAcc.numRowCoefs_ += getFieldSize(patt.rowFieldIDs_[i][j]);
683 coefAcc.numColCoefs_ = 0;
684 for(i=0; i<coefAcc.numColIDsPerRow_; i++) {
685 for(
int j=0; j<patt.numFieldsPerCol_[i]; j++) {
686 coefAcc.numColCoefs_ += getFieldSize(patt.colFieldIDs_[i][j]);
690 len = coefAcc.numRowCoefs_*coefAcc.numColCoefs_;
691 coefAcc.coefs_ =
new double[len];
694 for(i=0; i<len; i++) {
695 readData(instr, coefAcc.coefs_[offset++]);
702 if (!std::strcmp(
"numSlaveVariables", keyword)) {
703 readData(instr, numSlaveVars_);
705 if (numSlaveVars_ == 0)
return;
707 slaveVars_ =
new CRSet[numSlaveVars_];
709 for(
int i=0; i<numSlaveVars_; i++) {
710 CRSet& cr2 = slaveVars_[i];
717 cr2.nodeIDs_ =
new GlobalID*[1];
718 cr2.nodeIDs_[0] =
new GlobalID[cr2.
numNodes_];
723 readData(instr, cr2.nodeIDs_[0][k]);
727 readData(instr, cr2.fieldIDs_[j]);
732 len += getFieldSize(cr2.fieldIDs_[j]);
734 cr2.weights_ =
new double[len];
738 int size = getFieldSize(cr2.fieldIDs_[j]);
740 for(
int k=0; k<size; k++) {
741 readData(instr, cr2.weights_[offset++]);
745 cr2.values_ =
new double[1];
746 readData(instr, cr2.values_[0]);
752 if (!std::strcmp(
"numCRPenSets", keyword)) {
753 readData(instr, numCRPenSets_);
755 if (numCRPenSets_ == 0)
return;
757 crPenSets_ =
new CRSet[numCRPenSets_];
759 for(
int i=0; i<numCRPenSets_; i++) {
760 CRSet& cr3 = crPenSets_[i];
763 readData(instr, dummy);
766 cr3.nodeIDs_ =
new GlobalID*[1];
771 cr3.nodeIDs_[j] =
new GlobalID[cr3.
numNodes_];
774 readData(instr, cr3.nodeIDs_[j][k]);
779 readData(instr, cr3.fieldIDs_[j]);
784 len3 += getFieldSize(cr3.fieldIDs_[j]);
786 cr3.weights_ =
new double[len3];
790 int size3 = getFieldSize(cr3.fieldIDs_[j]);
792 for(
int k=0; k<size3; k++) {
794 readData(instr, dummy3);
795 cr3.weights_[offset3++] = dummy3;
799 cr3.values_ =
new double[1];
801 readData(instr, cr3.values_[j]);
804 cr3.penValues_ =
new double[1];
806 readData(instr, cr3.penValues_[j]);
815 int DataReader::getFieldSize(
int fieldID) {
816 for(
int i=0; i<numFields_; i++) {
817 if (fieldID == fieldIDs_[i])
return(fieldSizes_[i]);
820 fei::console_out() <<
"DataReader: ERROR, trying to find size of non-existent field."
826 void DataReader::readData(FEI_ISTREAM* instr,
int& n) {
827 int err = skipWhite(instr);
833 void DataReader::readData(FEI_ISTREAM* instr,
double& val) {
834 int err = skipWhite(instr);
std::ostream & console_out()