~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to src/fem/GlobalField.C

  • Committer: logg
  • Date: 2002-09-13 12:55:37 UTC
  • Revision ID: devnull@localhost-20020913125537-gz6ry1id9xsvu6np
Tailorized "2002-09-13 07:55:37 by logg"
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2002 Johan Hoffman and Anders Logg.
 
2
// Licensed under the GNU GPL Version 2.
 
3
 
 
4
#include "GlobalField.hh"
 
5
#include <Display.hh>
 
6
#include <Settings.hh>
 
7
#include <Grid.hh>
 
8
#include <Vector.hh>
 
9
#include <Output.hh>
 
10
 
 
11
//-----------------------------------------------------------------------------
 
12
GlobalField::GlobalField(Grid *grid, real constant)
 
13
{
 
14
  InitCommon(grid);
 
15
 
 
16
  representation = CONSTANT;
 
17
  this->constant = constant;
 
18
  
 
19
  no_dof = 1;
 
20
}
 
21
//-----------------------------------------------------------------------------
 
22
GlobalField::GlobalField(Grid *grid, Vector *x, int nvd = 1)
 
23
{
 
24
  InitCommon(grid);
 
25
 
 
26
  representation = NODAL;
 
27
  dof_values     = x;
 
28
  
 
29
  no_dof = x->Size()/nvd;
 
30
  this->nvd = nvd;
 
31
 
 
32
  if ( x->Size() % nvd != 0 )
 
33
         display->InternalError("GlobalField:GlobalField",
 
34
                                                                        "Vector dimension (%d) does not match length of vector (%d).",nvd,x->Size());
 
35
}
 
36
//-----------------------------------------------------------------------------
 
37
GlobalField::GlobalField(Grid *grid, const char *field)
 
38
{
 
39
  InitCommon(grid);
 
40
 
 
41
  representation = FUNCTION;
 
42
  function       = settings->GetFunction(field);
 
43
}
 
44
//-----------------------------------------------------------------------------
 
45
GlobalField::GlobalField(GlobalField *f1, GlobalField *f2)
 
46
{
 
47
  InitCommon(0);
 
48
 
 
49
  if ( f1->grid != f2->grid )
 
50
         display->InternalError("GlobalField:GlobalField()",
 
51
                                                                        "Collection of global fields must have the same grid.");
 
52
 
 
53
  representation = LIST;
 
54
  
 
55
  grid = f1->grid;
 
56
  list = new (GlobalField *)[2];
 
57
  listsize = 2;
 
58
 
 
59
  list[0]  = f1;
 
60
  list[1]  = f2;
 
61
}
 
62
//-----------------------------------------------------------------------------
 
63
GlobalField::GlobalField(GlobalField *f1, GlobalField *f2, GlobalField *f3)
 
64
{
 
65
  InitCommon(0);
 
66
 
 
67
  if ( (f1->grid != f2->grid) | (f2->grid != f3->grid) )
 
68
         display->InternalError("GlobalField:GlobalField()",
 
69
                                                                        "Collection of global fields must have the same grid.");
 
70
  
 
71
  representation = LIST;
 
72
  
 
73
  grid = f1->grid;
 
74
  list = new (GlobalField *)[3];
 
75
  listsize = 3;
 
76
  
 
77
  list[0] = f1;
 
78
  list[1] = f2;
 
79
  list[2] = f3;
 
80
}
 
81
//-----------------------------------------------------------------------------
 
82
GlobalField::GlobalField(GlobalField **f, int n)
 
83
{
 
84
  InitCommon(0);
 
85
 
 
86
  if ( n <= 0 )
 
87
         display->InternalError("GlobalField::GlobalField()",
 
88
                                                                        "The number of fields in a collection of fields must be positive.");
 
89
  for (int i=0;i<(n-1);i++)
 
90
  if ( f[i]->grid != f[i+1]->grid )
 
91
         display->InternalError("GlobalField:GlobalField()",
 
92
                                                                        "Collection of global fields must have the same grid.");
 
93
 
 
94
  representation = LIST;
 
95
  
 
96
  grid = f[0]->grid;
 
97
  list = new (GlobalField *)[n];
 
98
  listsize = n;
 
99
  
 
100
  for (int i=0;i<n;i++)
 
101
         list[i] = f[i];
 
102
 
 
103
  int dimensions[n];
 
104
  for (int i=0;i<n;i++)
 
105
         dimensions[i] = f[i]->nvd;
 
106
}
 
107
//-----------------------------------------------------------------------------
 
108
GlobalField::~GlobalField()
 
109
{
 
110
  if ( output )
 
111
         delete output;
 
112
  output = 0;
 
113
 
 
114
  if ( list )
 
115
         delete list;
 
116
  list = 0;
 
117
}
 
118
//-----------------------------------------------------------------------------
 
119
void GlobalField::SetSize(int no_data, ...)
 
120
{
 
121
  va_list aptr;
 
122
  va_start(aptr,no_data);
 
123
 
 
124
  if ( output )
 
125
         delete output;
 
126
  output = new Output(no_data,aptr);
 
127
  
 
128
  va_end(aptr);
 
129
}
 
130
//-----------------------------------------------------------------------------
 
131
void GlobalField::SetLabel(const char *name, const char *label, int i = 0)
 
132
{
 
133
  if ( !output )
 
134
         output = new Output(1,nvd);
 
135
  
 
136
  output->SetLabel(i,name,label);
 
137
}
 
138
//-----------------------------------------------------------------------------
 
139
int GlobalField::GetNoDof()
 
140
{
 
141
  return no_dof;
 
142
}
 
143
//-----------------------------------------------------------------------------
 
144
int GlobalField::GetVectorDim()
 
145
{
 
146
  return nvd;
 
147
}
 
148
//-----------------------------------------------------------------------------
 
149
int GlobalField::GetDim(int cell)
 
150
{
 
151
  // FIXME: no shapefunctions does not have to be same as no nodes 
 
152
  return ( grid->GetCell(cell)->GetSize() );
 
153
}
 
154
//-----------------------------------------------------------------------------
 
155
void GlobalField::GetLocalDof(int cell, real t, real *local_dof, int component = 0)
 
156
{
 
157
  // FIXME: no shapefunctions does not have to be same as no nodes 
 
158
  Cell *c = grid->GetCell(cell);
 
159
  int local_dim = c->GetSize();
 
160
 
 
161
  switch( representation ){ 
 
162
  case NODAL: 
 
163
    for (int i=0;i<local_dim;i++)
 
164
      local_dof[i] = dof_values->Get(grid->GetCell(cell)->GetNode(i)*nvd + component); 
 
165
    break;
 
166
  case CONSTANT:
 
167
    for (int i=0;i<local_dim;i++) 
 
168
      local_dof[i] = constant;
 
169
    break;
 
170
  case FUNCTION:
 
171
 
 
172
         // Return zero if the function is not specified
 
173
         if ( !function ){
 
174
                for (int i=0;i<local_dim;i++)
 
175
                  local_dof[i] = 0.0;
 
176
                return;
 
177
         }
 
178
 
 
179
         // Get the value from the function
 
180
         Point p;
 
181
         for (int i=0;i<local_dim;i++){
 
182
                p = grid->GetNode(c->GetNode(i))->GetCoord();
 
183
                local_dof[i] = function(p.x,p.y,p.z,t);
 
184
         }
 
185
         
 
186
    break;
 
187
  case LIST:
 
188
         display->InternalError("GlobalField::GetLocalDof()","Not available for collection of fields.");
 
189
         break;
 
190
  case NONE:
 
191
    display->InternalError("GlobalField::GetLocalDof()","GlobalField is not initialized");
 
192
         break;
 
193
  default:
 
194
    display->InternalError("GlobalField::GetLocalDof()","Unknown representation for GlobalField: %d",representation);
 
195
  }
 
196
  
 
197
}
 
198
//-----------------------------------------------------------------------------
 
199
void GlobalField::Save()
 
200
{
 
201
  Save(0.0);
 
202
}
 
203
//-----------------------------------------------------------------------------
 
204
void GlobalField::Save(real t)
 
205
{
 
206
  // If Output is not initialised, assume the simplest possible
 
207
  if ( !output )
 
208
         output = new Output(1,nvd);
 
209
 
 
210
  Vector **vectors = 0;
 
211
  
 
212
  switch ( representation ){
 
213
  case NODAL:
 
214
         output->AddFrame(grid,&dof_values,t);
 
215
         break;
 
216
  case LIST:
 
217
         vectors = new (Vector *)[listsize];
 
218
         for (int i=0;i<listsize;i++){
 
219
                if ( list[i]->representation != NODAL )
 
220
                  display->InternalError("GlobalField:Save()","Output for collection of fields only implemented for nodal representation.");
 
221
                vectors[i] = list[i]->dof_values;
 
222
         }
 
223
         output->AddFrame(grid,vectors,t,listsize);
 
224
         delete vectors;
 
225
         break;
 
226
  default:
 
227
         int *a;
 
228
         a[12313123] = 1;
 
229
         
 
230
         display->Message(0,"representation = %d",representation);
 
231
         display->InternalError("GlobalField:Save()",
 
232
                                                                        "Output only implemented for nodal representation (possibly collections).");
 
233
  }
 
234
  
 
235
}
 
236
//-----------------------------------------------------------------------------
 
237
void GlobalField::InitCommon(Grid *grid)
 
238
{
 
239
  this->grid = grid;
 
240
 
 
241
  representation = NONE;
 
242
  no_dof         = 0;
 
243
  nvd            = 1;
 
244
  output         = 0;
 
245
  listsize       = 0;
 
246
  
 
247
  dof_values     = 0;
 
248
  constant       = 0.0;
 
249
  function       = 0;
 
250
  list           = 0;
 
251
 
 
252
  sprintf(name,"u");
 
253
  sprintf(label,"unknown field");
 
254
}
 
255
//-----------------------------------------------------------------------------