~fluidity-core/fluidity/embedded_models

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* Copyright (C) 2006 Imperial College London and others.

 Please see the AUTHORS file in the main source directory for a full list
 of copyright holders.

 Dr Gerard J Gorman
 Applied Modelling and Computation Group
 Department of Earth Science and Engineering
 Imperial College London

 g.gorman@imperial.ac.uk

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 USA
*/
#include <mpi.h>
#include <string.h>
#include <vector>
#include <deque>
#include <string>
#include <map>
#include <set>
#include <assert.h>


#include "Mesh.h"
#include "samtypes.h"
#include "c++debug.h"

using namespace std;

void Mesh::export_fluidity(int int_memory[],         int int_memory_len,  int &IPT,
			   samfloat_t real_memory[], int real_memory_len, int &RPT,
			   const int MXNODS,
			   /* Based pointers into memory*/
			   int& NewPNodes, int& NewNNodes, int& NewNElems, int& NewNSElems, 
			   int& NewSCATER, int NewATOREC[],
			   int& NewGATHER, int NewATOSEN[],
			   int& NewENLIST, int& NewSNLIST, int& NewSURFID,
			   // Pressure stuff
			   int& NewPPressureNodes, int& NewNPressureNodes,
			   int& NewpSCATER,        int NewpATOREC[],
			   int& NewpGATHER,        int NewpATOSEN[],
			   int& NewpENLIST,
			   int& NewNODX, int& NewNODY, int& NewNODZ, samfloat_t Metric[]){ 
  ECHO("void Mesh::export_fluidity( ... )");
  CHECK(int_memory_len);
  CHECK(IPT);

  CHECK(real_memory_len);
  CHECK(RPT);

  //
  // Start exporting meshes
  //
  //  node_list.refresh_unn2gnn();

  NewPNodes = node_list.psize();
  NewNNodes = node_list.size();
  
  CHECK(NewPNodes);
  CHECK(NewNNodes);

  do_element_headcount();
  NewNElems = num_elements("volume");
  NewNSElems= num_elements("surface");

  // Start base pointer into the free integer memory.
  int bptr = IPT-1;
  
  {// Compress halo info..
    NewATOREC[0] = 1;
    for(int i=0; i<NProcs; i++){
      NewATOREC[i+1] =  NewATOREC[i] + halo_nodes[i].size();
    }
    
    NewSCATER = bptr + 1; // +1's is for bloody fortran
    for(int i=0; i<NProcs; i++){
      ECHO("Processor - " << i);
      for(set<unsigned>::iterator n = halo_nodes[i].begin(); n != halo_nodes[i].end(); ++n){
	int gnn = unn2gnn(*n);
	
	assert(gnn <  NewNNodes);
	assert(gnn >= NewPNodes);

	ECHO("SCATER = " << *n << " -> " << gnn + 1);
	
	int_memory[bptr++] = gnn + 1; // +1 is for the fortran offset.
      }
    }
  }
  
  {// Compress shared info...
    NewATOSEN[0] = 1;
    for(int i=0; i<NProcs; i++){
      NewATOSEN[i+1] = NewATOSEN[i] + shared_nodes[i].size();
    }
    
    NewGATHER = bptr + 1;
    for(int i = 0; i<NProcs; i++){
      ECHO("Processor - " << i);
      for(set<unsigned>::iterator n = shared_nodes[i].begin(); n != shared_nodes[i].end(); ++n){
	int gnn = unn2gnn(*n);
	
	assert(gnn <  NewPNodes);
	assert(gnn >= 0);

	ECHO("GATHER = " << *n << " -> " << gnn + 1);
	
	int_memory[bptr++] = gnn + 1; // +1 is for the fortran offset.
      }
    }
  } // end compressing shared
  
  { // Compress volume element-node lists
    NewENLIST = bptr + 1;
    for(deque<Element>::iterator et = element_list.begin(); et != element_list.end(); ++et){
      
      unsigned char type = (*et).get_flags();
      if( type & ELM_VOLUME ){
	vector<unn_t> enl( (*et).get_enlist() );
	for(vector<unn_t>::iterator it = enl.begin(); it != enl.end(); ++it){
	  int gnn = unn2gnn(*it);
	  
	  assert(bptr < int_memory_len );
	  assert(NewNNodes > gnn);
	  int_memory[bptr++] = gnn + 1;
	}
      }
    
    }
  }

  { 
    // Compress surface element-node list and write the surface id's.    
    vector<int> surfid( NewNSElems );
    unsigned pos=0;
    
    NewSNLIST = bptr + 1;
    for(deque<Element>::iterator et = element_list.begin(); et != element_list.end(); ++et){
      
      unsigned char type = (*et).get_flags();
      if( type & ELM_SURFACE ){
	
	assert(pos<NewNSElems);
	const vector<int>& ifields = (*et).get_ifields();
	surfid[pos++] = ifields[0];
	
	vector<unn_t> enl( (*et).get_enlist() );
	
	for(vector<unn_t>::iterator it = enl.begin(); it != enl.end(); ++it){
	  int gnn = unn2gnn(*it);
	  
	  assert(bptr < int_memory_len);
	  assert(NewNNodes > gnn);
	  int_memory[bptr++] = gnn + 1;
	}
      }
      
    }
    NewSURFID = bptr + 1;
    assert(NewNSElems == pos);
    for(size_t i=0; i<(size_t)NewNSElems; i++)
      int_memory[bptr++] = surfid[i];
    
  }

  // Export pressure nodes
  NewNPressureNodes = MFnode_list.size();
  NewPPressureNodes = MFnode_list.psize();
  CHECK(NewPPressureNodes);
  CHECK(NewNPressureNodes);
  
  if( mixed_formulation() ){
    ECHO("Doing mixed-formulation exporting thing.");
    
    {// Compress halo info..
      NewpATOREC[0]=1;
      for(int i = 0; i< NProcs; i++){
	NewpATOREC[i+1] = NewpATOREC[i] + halo_pnodes[i].size();
      }
      
      NewpSCATER = bptr + 1;
      for(int i=0; i<NProcs; i++){
	ECHO("Processor - " << i);
	for(set<unsigned>::iterator n = halo_pnodes[i].begin(); n != halo_pnodes[i].end(); ++n){
	  int gnn = MFunn2gnn(*n);
	  
	  assert(gnn <  NewNPressureNodes);
	  assert(gnn >= NewPPressureNodes);
	  ECHO("pSCATER = " << *n << " -> " << gnn + 1);
  
	  int_memory[bptr++] = gnn + 1; // +1 is for the fortran offset.
	}
      }
    } // finished compressing halo info.
    
    {// Compress shared info...
      NewpATOSEN[0]=1;
      for(int i=0; i<NProcs; i++){
	NewpATOSEN[i+1] = NewpATOSEN[i] + shared_pnodes[i].size();
      }
      
      NewpGATHER = bptr + 1;
      for(int i = 0; i<NProcs; i++){
	ECHO("Processor - " << i);
	for(set<unsigned>::iterator n = shared_pnodes[i].begin(); n != shared_pnodes[i].end(); ++n){
	  int gnn = MFunn2gnn(*n);
	  
	  assert(gnn <  NewPPressureNodes);
	  assert(gnn >= 0);
	  ECHO("pGATHER = " << *n << " -> " << gnn + 1);
	  
	  int_memory[bptr++] = gnn + 1; // +1 is for the fortran offset.
	}
      }
    } // end compressing shared
 
    ECHO("Writting pressure elements...");
    
    NewpENLIST = bptr + 1;
    for(ElementVector<Element>::iterator et = element_list.begin(); et != element_list.end(); ++et){
      if((*et).get_flags() & ELM_SURFACE)
	continue;
      
      const vector<unn_t>& enl = (*et).get_MFenlist();
      for(vector<unn_t>::const_iterator it = enl.begin(); it != enl.end(); ++it){
	int gnn = MFunn2gnn(*it);

	assert(bptr<int_memory_len);
	assert(NewNPressureNodes>gnn);
	int_memory[bptr++] = gnn + 1;
      }
    }
    ECHO("...done.");

  }

  IPT = bptr+1;

  // Start base pointer into the free real memory.
  { // Write coordinates, field data and metric.
    assert((3*MXNODS)<real_memory_len);
    bptr = RPT-1;
    RPT += MXNODS;
    NewNODX = bptr + 1;
    for(deque<Node>::iterator in=node_list.begin(); in != node_list.end(); ++in)
      real_memory[bptr++] = (*in).get_x();
    
    bptr = RPT-1;
    RPT += MXNODS;
    NewNODY = bptr + 1;  
    for(deque<Node>::iterator in=node_list.begin(); in != node_list.end(); ++in)
      real_memory[bptr++] = (*in).get_y();
    
    bptr = RPT-1;
    RPT += MXNODS;
    NewNODZ = bptr + 1;
    for(deque<Node>::iterator in=node_list.begin(); in != node_list.end(); ++in)
      real_memory[bptr++] = (*in).get_z();
    
    bptr = RPT-1;
    unsigned nfields = node_list[0].get_fields().size();
    RPT += (MXNODS*nfields);
    
    assert(RPT<real_memory_len);
    
    for(deque<Node>::iterator in=node_list.begin(); in != node_list.end(); ++in){
      CHECK(*in);
      
      const vector<samfloat_t>& flds = (*in).get_fields();
      assert(flds.size() == nfields);
      
      for(unsigned f=0; f<nfields; f++)
	real_memory[bptr + f*MXNODS] = flds[f];
      bptr++;
    }
    ECHO(RPT<<" should be the same as PYSFU2");

    int n=0;
    for(deque<Node>::iterator in=node_list.begin(); in != node_list.end(); in++,n++)
      memcpy(Metric+n*9, &(in->get_metric()[0]), 9*sizeof(samfloat_t));
    
    RPT = bptr+1;
  }

}

void Mesh::export_halo(int* colgat, int* atosen, int* scater, int* atorec, const int* ncolga, const int* nscate, const int* nprocs)
{
  // Input check
  assert(*ncolga == get_ncolga());
  assert(*nscate == get_nscate());
  assert(*nprocs == this->NProcs);
  
  atosen[0]= 0;
  int index = 0;
  for(int i = 0;i < this->NProcs;i++)
  {
    assert(i < *nprocs);
    atosen[i + 1] = atosen[i] + shared_nodes[i].size();
    for(set<unsigned>::iterator n = shared_nodes[i].begin(); n != shared_nodes[i].end(); ++n)
    {
      int gnn = unn2gnn(*n);	
      assert(index < *ncolga);
	    colgat[index++] = gnn + 1;
    }
  }

  atorec[0] = 0;
  index = 0;
  for(int i = 0;i < this->NProcs;i++)
  {
    assert(i < *nprocs);
    atorec[i + 1] = atorec[i] + halo_nodes[i].size();
    for(set<unsigned>::iterator n = halo_nodes[i].begin(); n != halo_nodes[i].end(); ++n)
    {
      int gnn = unn2gnn(*n);	
      assert(index < *nscate);
	    scater[index++] = gnn + 1;
    }
  }
  
  return;
}


void Mesh::export_phalo(int* pcolgat, int* patosen, int* pscater, int* patorec, const int* pncolga, const int* pnscate, const int* nprocs)
{
  // Input check
  assert(*pncolga == get_pncolga());
  assert(*pnscate == get_pnscate());
  assert(*nprocs == this->NProcs);
  
  patosen[0]= 0;
  int index = 0;
  for(int i = 0;i < this->NProcs;i++)
  {
    assert(i < *nprocs);
    patosen[i + 1] = patosen[i] + shared_pnodes[i].size();
    for(set<unsigned>::iterator n = shared_pnodes[i].begin(); n != shared_pnodes[i].end(); ++n)
    {
      int gnn = unn2gnn(*n);	
      assert(index < *pncolga);
	    pcolgat[index++] = gnn + 1;
    }
  }

  patorec[0] = 0;
  index = 0;
  for(int i = 0;i < this->NProcs;i++)
  {
    assert(i < *nprocs);
    patorec[i + 1] = patorec[i] + halo_pnodes[i].size();
    for(set<unsigned>::iterator n = halo_pnodes[i].begin(); n != halo_pnodes[i].end(); ++n)
    {
      int gnn = unn2gnn(*n);	
      assert(index < *pnscate);
	    pscater[index++] = gnn + 1;
    }
  }
  
  return;
}