FEI  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Poisson_Elem.cpp
1 /*--------------------------------------------------------------------*/
2 /* Copyright 2005 Sandia Corporation. */
3 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
4 /* non-exclusive license for use of this work by or on behalf */
5 /* of the U.S. Government. Export of this program may require */
6 /* a license from the United States Government. */
7 /*--------------------------------------------------------------------*/
8 
9 #include <fei_iostream.hpp>
10 #include <fei_defs.h>
11 #include <test_utils/Poisson_Elem.hpp>
12 
13 #include <cmath>
14 #include <cstdlib>
15 #include <assert.h>
16 
17 //==============================================================================
18 Poisson_Elem::Poisson_Elem() {
19 
20  globalElemID_ = (GlobalID)(-1);
21  ID_IsSet_ = false;
22  numElemNodes_ = 4;
23  numElemRows_ = 4;
24 
25  nodeList_ = NULL;
26  nodalX_ = NULL;
27  nodalY_ = NULL;
28 
29  elemStiff_ = NULL;
30  elemLoad_ = NULL;
31  internalsAllocated_ = false;
32 
33  elemLength_ = 0.0;
34  elemLengthIsSet_ = false;
35  totalLength_ = 0.0;
36  totalLengthIsSet_ = false;
37 
38  loadAllocated_ = false;
39  stiffAllocated_ = false;
40 }
41 
42 //==============================================================================
43 Poisson_Elem::~Poisson_Elem() {
44  deleteMemory();
45 }
46 
47 //==============================================================================
48 void Poisson_Elem::deleteMemory()
49 {
50  if (!internalsAllocated_) return;
51 
52  delete [] nodeList_;
53  delete [] nodalX_;
54  delete [] nodalY_;
55 
56  internalsAllocated_ = false;
57 
58  if (loadAllocated_) {
59  delete [] elemLoad_;
60  loadAllocated_ = false;
61  }
62 
63  if (stiffAllocated_) {
64  delete [] elemStiff_[0];
65  delete [] elemStiff_;
66  stiffAllocated_ = false;
67  }
68 }
69 
70 //==============================================================================
71 int Poisson_Elem::allocateInternals(int DOF) {
72 
73  assert(DOF==1);
74 
75  nodeList_ = new GlobalID[numElemNodes_];
76  if (!nodeList_) return(1);
77 
78  nodalX_ = new double[numElemNodes_];
79  if (!nodalX_) return(1);
80 
81  nodalY_ = new double[numElemNodes_];
82  if (!nodalY_) return(1);
83 
84  for(int i = 0; i < numElemNodes_; i++) {
85  nodeList_[i] = (GlobalID)0;
86  nodalX_[i] = 0.0;
87  nodalY_[i] = 0.0;
88  }
89 
90  internalsAllocated_ = true;
91  return(0);
92 }
93 
94 //==============================================================================
95 int Poisson_Elem::allocateLoad(int DOF)
96 {
97  assert(DOF==1);
98 
99  elemLoad_ = new double[numElemNodes_];
100  if (!elemLoad_) return(1);
101 
102  loadAllocated_ = true;
103  return(0);
104 }
105 
106 //==============================================================================
107 int Poisson_Elem::allocateStiffness(int DOF)
108 {
109  assert(DOF==1);
110 
111  elemStiff_ = new double*[numElemNodes_];
112  if (!elemStiff_) return(1);
113 
114  elemStiff_[0] = NULL;
115  elemStiff_[0] = new double[numElemNodes_*numElemNodes_];
116  if (!elemStiff_[0]) return(1);
117 
118  int i;
119  for(i=0; i<numElemNodes_*numElemNodes_; i++) elemStiff_[0][i] = 0.0;
120 
121  for(i=1; i<numElemNodes_; i++) {
122  elemStiff_[i] = elemStiff_[i-1] + numElemNodes_;
123  }
124 
125  stiffAllocated_ = true;
126  return(0);
127 }
128 
129 //==============================================================================
130 GlobalID* Poisson_Elem::getElemConnPtr(int& size) {
131 
132  if (internalsAllocated_) {
133  size = numElemNodes_;
134  return(nodeList_);
135  }
136  else {
137  size = 0;
138  return(NULL);
139  }
140 }
141 
142 //==============================================================================
143 double* Poisson_Elem::getElemLoad(int& size) {
144 
145  if (internalsAllocated_) {
146  size = numElemNodes_;
147  return(elemLoad_);
148  }
149  else {
150  size = 0;
151  return(NULL);
152  }
153 }
154 
155 //==============================================================================
156 double** Poisson_Elem::getElemStiff(int& size) {
157 
158  if (internalsAllocated_) {
159  size = numElemNodes_*numElemNodes_;
160  return(elemStiff_);
161  }
162  else {
163  size = 0;
164  return(NULL);
165  }
166 }
167 
168 //==============================================================================
169 void Poisson_Elem::calculateCoords() {
170 //
171 //This function calculates nodal x- and y-coordinates for this element.
172 //NOTE: element IDs are assumed to be 1-based.
173 //
174  if (!internalsAllocated_)
175  messageAbort("calculateCoords: internals not allocated.");
176  if (!elemLengthIsSet_)
177  messageAbort("calculateCoords: elemLength not set.");
178  if (!totalLengthIsSet_)
179  messageAbort("calculateCoords: totalLength not set.");
180  if (!ID_IsSet_)
181  messageAbort("calculateCoords: elemID not set.");
182  if (std::abs(elemLength_) < 1.e-49)
183  messageAbort("calculateCoords: elemLength == 0.");
184 
185  int lowLeft = 0;
186  int lowRight = 1;
187  int upperRight = 2;
188  int upperLeft = 3;
189 
190  int elemsPerSide = (int)std::ceil(totalLength_/elemLength_);
191 
192  int elemX = (int)globalElemID_%elemsPerSide;
193  if (elemX==0) elemX = elemsPerSide;
194 
195  int elemY = ((int)globalElemID_ - elemX)/elemsPerSide + 1;
196 
197  //elemX and elemY are 1-based coordinates of this element in
198  //the global square of elements. The origin is position (1,1),
199  //which is at the bottom left of the square.
200 
201  nodalX_[lowLeft] = (elemX-1)*elemLength_;
202  nodalX_[upperLeft] = nodalX_[lowLeft];
203  nodalX_[lowRight] = elemX*elemLength_;
204  nodalX_[upperRight] = nodalX_[lowRight];
205 
206  nodalY_[lowLeft] = (elemY-1)*elemLength_;
207  nodalY_[lowRight] = nodalY_[lowLeft];
208  nodalY_[upperLeft] = elemY*elemLength_;
209  nodalY_[upperRight] = nodalY_[upperLeft];
210 }
211 
212 //==============================================================================
213 void Poisson_Elem::messageAbort(const char* str) {
214  fei::console_out() << "Poisson_Elem: ERROR: " << str << " Aborting." << FEI_ENDL;
215  std::abort();
216 }
217 
218 //==============================================================================
219 void Poisson_Elem::calculateLoad() {
220 
221  assert (numElemRows_ == 4);
222 
223  elemLoad_[0] = -2.0*elemLength_*elemLength_;
224  elemLoad_[1] = -2.0*elemLength_*elemLength_;
225  elemLoad_[2] = -2.0*elemLength_*elemLength_;
226  elemLoad_[3] = -2.0*elemLength_*elemLength_;
227 }
228 
229 //==============================================================================
230 void Poisson_Elem::calculateStiffness() {
231 
232  assert (numElemRows_ == 4);
233 
234  elemStiff_[0][0] = 2.0;
235  elemStiff_[0][1] = -1.0;
236  elemStiff_[0][2] = 0.0;
237  elemStiff_[0][3] = -1.0;
238 
239  elemStiff_[1][0] = -1.0;
240  elemStiff_[1][1] = 2.0;
241  elemStiff_[1][2] = -1.0;
242  elemStiff_[1][3] = 0.0;
243 
244  elemStiff_[2][0] = 0.0;
245  elemStiff_[2][1] = -1.0;
246  elemStiff_[2][2] = 2.0;
247  elemStiff_[2][3] = -1.0;
248 
249  elemStiff_[3][0] = -1.0;
250  elemStiff_[3][1] = 0.0;
251  elemStiff_[3][2] = -1.0;
252  elemStiff_[3][3] = 2.0;
253 }
254 
255 
256 
257 //-----------------------------------------------------------------------
258 
259 void Poisson_Elem::dumpToScreen() {
260 
261  int i;
262 
263  FEI_COUT << " globalElemID_ = " << (int)globalElemID_ << FEI_ENDL;
264  FEI_COUT << " numElemRows_ = " << numElemRows_ << FEI_ENDL;
265  FEI_COUT << " elemLength_ = " << elemLength_ << FEI_ENDL;
266  FEI_COUT << " the " << numElemNodes_ << " nodes: ";
267  for (i = 0; i < numElemNodes_; ++i) {
268  FEI_COUT << " " << (int)nodeList_[i];
269  }
270  FEI_COUT << FEI_ENDL;
271  FEI_COUT << " the " << numElemRows_ << " load vector terms: ";
272  for (i = 0; i < numElemRows_; ++i) {
273  FEI_COUT << " " << elemLoad_[i];
274  }
275  FEI_COUT << FEI_ENDL << FEI_ENDL;
276  return;
277 }
278 
std::ostream & console_out()