FEI  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
InputData.hpp
1 #ifndef _InputData_h_
2 #define _InputData_h_
3 
4 /*--------------------------------------------------------------------*/
5 /* Copyright 2005 Sandia Corporation. */
6 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
7 /* non-exclusive license for use of this work by or on behalf */
8 /* of the U.S. Government. Export of this program may require */
9 /* a license from the United States Government. */
10 /*--------------------------------------------------------------------*/
11 
12 #include <fei_macros.hpp>
13 
14 #include <vector>
15 
16 class ElemContribution {
17  public:
18  ElemContribution(){}
19  ElemContribution(const ElemContribution& src)
20  {
21  rhsContributions = src.rhsContributions;
22  }
23 
24  ~ElemContribution(){}
25 
26  bool operator==(const ElemContribution& rhs)
27  {
28  if (matrixContributions != rhs.matrixContributions) {
29  cout << "matrixContributions don't match." << endl;
30  return(false);
31  }
32 
33  if ( rhsContributions != rhs.rhsContributions ) {
34  cout << "rhsContributions don't match." << endl;
35  return(false);
36  }
37 
38  return(true);
39  }
40 
41  bool operator!=(const ElemContribution& rhs)
42  {
43  return( !( *this == rhs) );
44  }
45 
46  std::vector<double> matrixContributions;
47  std::vector<double> rhsContributions;
48 };
49 
50 class InputData {
51  public:
52  InputData(){}
53  ~InputData()
54  {
55  for(int i=0; i<elemIDs.length(); i++) delete elemIDs[i];
56  }
57 
58  std::vector<int> elemBlockIDs;
59  std::vector<std::vector<int>*> elemIDs;
60  std::vector<std::vector<ElemContribution>*> elemContributions;
61 
62  bool operator==(const InputData& rhs)
63  {
64  if (elemBlockIDs != rhs.elemBlockIDs) {
65  cout << "elemBlockIDs don't match." << endl;
66  return(false);
67  }
68 
69  for(int i=0; i<elemIDs.length(); i++) {
70  std::vector<ElemContribution>& elems = *(elemContributions[i]);
71  std::vector<ElemContribution>& rhsElems = *(rhs.elemContributions[i]);
72 
73  for(int j=0; j<elemIDs[i]->length(); j++) {
74  int id1 = (*(elemIDs[i]))[j];
75  int id2 = (*(rhs.elemIDs[i]))[j];
76 
77  if ( id1 != id2 ) {
78  cout << "elemIDs don't match. element-block " << elemBlockIDs[i]
79  << ", elemID in position " << j << " is " << id1
80  << ", doesn't match " << id2 << "." << endl;
81  return(false);
82  }
83 
84  if (elems[j] != rhsElems[j]) {
85  cout << "element-block " << elemBlockIDs[i] << ", elemID " << id1
86  << "'s element-contributions don't match." << endl;
87  return(false);
88  }
89  }
90  }
91 
92  return(true);
93  }
94 
95  bool operator!=(const InputData& rhs)
96  {
97  return( !( (*this) == rhs) );
98  }
99 
100  int addElemID(int elemBlockID, int elemID)
101  {
102  //add and elemBlockID/elemID pair to the internal arrays if not already
103  //present.
104 
105  int err, insertPoint = -1;
106  int blkInd = elemBlockIDs.binarySearch(elemBlockID, insertPoint);
107  if (blkInd < 0) {
108  err = elemBlockIDs.insert(elemBlockIDs.begin()+insertPoint, elemBlockID);
109  err += elemIDs.insert(elemIDs.begin()+insertPoint, new std::vector<int>);
110  err += elemContributions.insert(elemContributions.begin()+insertPoint, new std::vector<ElemContribution>);
111  if (err != 0) return(err);
112  blkInd = insertPoint;
113  }
114 
115  std::vector<int>& IDs = *(elemIDs[blkInd]);
116  std::vector<ElemContribution>& ec = *(elemContributions[blkInd]);
117 
118  err = IDs.insertSorted(elemID);
119  if (err == -2) return(err);
120 
121  ElemContribution dummy;
122  if (err >= 0) err = ec.insert(ec.begin()+err, dummy);
123  if (err == -2) return(err);
124 
125  return(0);
126  }
127 
128  int addElemMatrix(int elemBlockID, int elemID, std::vector<double>& matrixData)
129  {
130  int insertPoint = -1;
131  int blkInd = elemBlockIDs.binarySearch(elemBlockID, insertPoint);
132  if (blkInd < 0) {
133  cerr << " addElemMatrix ERROR, elemBlockID " << (int)elemBlockID
134  << " not found" << endl;
135  return(-1);
136  }
137 
138  int elemIdx = elemIDs[blkInd]->binarySearch(elemID);
139  if (elemIdx < 0) {
140  cerr << "addElemMatrix ERROR, elemID " << (int)elemID << " not found."
141  <<endl;
142  return(-1);
143  }
144 
145  ElemContribution& elemContr = (*(elemContributions[blkInd]))[elemIdx];
146 
147  std::vector<double>& elemContrMatrix = elemContr.matrixContributions;
148  int len = matrixData.length();
149  int oldLen = elemContrMatrix.length();
150  if (oldLen < len) {
151  elemContrMatrix.resize(len);
152  for(int i=oldLen; i<len; i++) elemContrMatrix[i] = 0.0;
153  }
154 
155  for(int i=0; i<matrixData.length(); i++) {
156  elemContrMatrix[i] += matrixData[i];
157  }
158 
159  return(0);
160  }
161 
162  int addElemRHS(int elemBlockID, int elemID, std::vector<double>& rhsData)
163  {
164  int insertPoint = -1;
165  int blkInd = elemBlockIDs.binarySearch(elemBlockID, insertPoint);
166  if (blkInd < 0) {
167  cerr << " addElemRHS ERROR, elemBlockID " << (int)elemBlockID
168  << " not found" << endl;
169  return(-1);
170  }
171 
172  int elemIdx = elemIDs[blkInd]->binarySearch(elemID);
173  if (elemIdx < 0) {
174  cerr << "addElemRHS ERROR, elemID " << (int)elemID << " not found."<<endl;
175  return(-1);
176  }
177 
178  ElemContribution& elemContr = (*(elemContributions[blkInd]))[elemIdx];
179 
180  std::vector<double>& elemContrRHS = elemContr.rhsContributions;
181  int len = rhsData.length();
182  int oldLen = elemContrRHS.length();
183  if (oldLen < len) {
184  elemContrRHS.resize(len);
185  for(int i=oldLen; i<len; i++) elemContrRHS[i] = 0.0;
186  }
187 
188  for(int i=0; i<rhsData.length(); i++) {
189  elemContrRHS[i] += rhsData[i];
190  }
191 
192  return(0);
193  }
194 };
195 
196 #endif // _InputData_h_