amesh.cpp 49.1 KB
Newer Older
1
#include "amesh.h"
SilverLife's avatar
SilverLife committed
2
#include <iomanip>
3
#include <set>
Kirill Terekhov's avatar
Kirill Terekhov committed
4
#include "../../Source/Misc/base64.h"
Kirill Terekhov's avatar
Kirill Terekhov committed
5
//using namespace std;
6

Kirill Terekhov's avatar
Kirill Terekhov committed
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
//from inmost
//std::string base64_encode(unsigned char const* buf, unsigned int bufLen);
	std::string base64_encode_(unsigned char const* buf, unsigned int bufLen)
	{
		static const std::string base64_chars =
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		"abcdefghijklmnopqrstuvwxyz"
		"0123456789+/";
		std::string ret;
		int i = 0;
		int j = 0;
		unsigned char char_array_3[3];
		unsigned char char_array_4[4];
		
		while (bufLen--)
		{
			char_array_3[i++] = *(buf++);
			if (i == 3)
			{
				char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
				char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
				char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
				char_array_4[3] = char_array_3[2] & 0x3f;
				for(i = 0; (i <4) ; i++)
					ret += base64_chars[char_array_4[i]];
				i = 0;
			}
		}
		
		if (i)
		{
			for(j = i; j < 3; j++)
				char_array_3[j] = '\0';
	  
			char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
			char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
			char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
			char_array_4[3] = char_array_3[2] & 0x3f;
	  
			for (j = 0; (j < i + 1); j++)
				ret += base64_chars[char_array_4[j]];
	  
			while((i++ < 3))
				ret += '=';
		}
		
		return ret;
	}
55
56
57
/// todo:
/// 1. coarsment
/// 2. strategy for faces/edges with faults
Kirill Terekhov's avatar
Kirill Terekhov committed
58
/// 3. geom model supportbn
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
/// 4. make class abstract virtual for user implementation of refinement and coarsment indicators
/// see in code todo:
namespace INMOST
{
	void CleanupSets(ElementSet set)
	{
		ElementSet::iterator it = set.Begin();
		while(it != set.End())
		{
			if( it->isValid() ) ++it;
			else it = set.Erase(it);
		}
		for(ElementSet child = set.GetChild(); child.isValid(); child = child.GetSibling())
			CleanupSets(child);
	}
	
	void ReduceMax(const Tag & tag, const Element & element, const INMOST_DATA_BULK_TYPE * data, INMOST_DATA_ENUM_TYPE size)
	{
		(void) size;
		element->Integer(tag) = std::max(element->Integer(tag),*((const INMOST_DATA_INTEGER_TYPE *)data));
	}
	
	void ReduceMin(const Tag & tag, const Element & element, const INMOST_DATA_BULK_TYPE * data, INMOST_DATA_ENUM_TYPE size)
	{
		(void) size;
		element->Integer(tag) = std::min(element->Integer(tag),*((const INMOST_DATA_INTEGER_TYPE *)data));
	}
SilverLife's avatar
SilverLife committed
86
87


Kirill Terekhov's avatar
Kirill Terekhov committed
88
    void AdaptiveMesh::PrintSetLocal(std::string offset, ElementSet it, std::stringstream& ss)
SilverLife's avatar
SilverLife committed
89
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
90
91
        std::stringstream ss1;
        ss1 << offset << rank << ": Set : " << std::setw(5) << it.GetName() << " ";
SilverLife's avatar
SilverLife committed
92
93
        for (int i = ss1.str().length(); i < 23; i++) ss1 << " ";
        ss << ss1.str();
Kirill Terekhov's avatar
Kirill Terekhov committed
94
        ss << std::setw(6);
SilverLife's avatar
SilverLife committed
95
96
97
98
99
        if (it.GetStatus() == Element::Shared) ss << "shared";
        else if (it.GetStatus() == Element::Ghost) ss << "ghost";
        else if (it.GetStatus() == Element::Owned) ss << "owned";
        else ss << "none";

100
        ss << " tag_owner (" << it.IntegerDF(m->OwnerTag()) << ")";
SilverLife's avatar
SilverLife committed
101
102
103

        //ss << "   level (" << level[it.self()] << ")  ";
        ss << " tag_processors (";
Kirill Terekhov's avatar
Kirill Terekhov committed
104
        std::stringstream ss2;
105
        Storage::integer_array arr = it.IntegerArrayDV(m->ProcessorsTag());
SilverLife's avatar
SilverLife committed
106
107
        for (int i = 0; i < arr.size(); i++)
            ss2 << arr[i] << " ";
Kirill Terekhov's avatar
Kirill Terekhov committed
108
        ss << std::setw(5) << ss2.str() <<")";
SilverLife's avatar
SilverLife committed
109
110
111
112
113
114
115
116
        

        ElementSet::iterator p = it.Begin();
        ss << "     | Refs: ";
        int first = 0;
        while(p != it.End())
        {
        //    if (first++ == 0) ss << endl << offset << "   ";
Kirill Terekhov's avatar
Kirill Terekhov committed
117
            std::string type = "unknw";
SilverLife's avatar
SilverLife committed
118
119
120
121
            if (p->GetElementType() == CELL) type = "cell";
            if (p->GetElementType() == FACE) type = "face";
            if (p->GetElementType() == EDGE) type = "edge";
            if (p->GetElementType() == NODE) type = "node";
Kirill Terekhov's avatar
Kirill Terekhov committed
122
            ss << type << "-" << std::setw(2) << p->GlobalID() << " ";
SilverLife's avatar
SilverLife committed
123
124
            p++;
        }
Kirill Terekhov's avatar
Kirill Terekhov committed
125
        ss << std::endl;
SilverLife's avatar
SilverLife committed
126
127
128
129
130
131
132
133
134
135

        for(ElementSet child = it.GetChild(); child.isValid(); child = child.GetSibling())
        {
            PrintSetLocal(offset + "   ",child,ss);
        }
    }


    void AdaptiveMesh::PrintSet()
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
136
        std::stringstream ss;
137
        for(Mesh::iteratorSet it = m->BeginSet(); it != m->EndSet(); ++it) 
SilverLife's avatar
SilverLife committed
138
        {
Kirill Terekhov's avatar
Kirill Terekhov committed
139
            //if (it->HaveParent()) continue;
140
            PrintSetLocal("",ElementSet(m,*it),ss);
SilverLife's avatar
SilverLife committed
141
        }
Kirill Terekhov's avatar
Kirill Terekhov committed
142
        std::cout << ss.str() << std::endl;
SilverLife's avatar
SilverLife committed
143
144
    }

Kirill Terekhov's avatar
Kirill Terekhov committed
145
    void PrintRefs(std::ostream& os, Storage::reference_array refs)
146
147
148
    {
        for(Storage::reference_array::size_type i = 0; i < refs.size(); ++i)
        {
Kirill Terekhov's avatar
Kirill Terekhov committed
149
            std::string type = "unknw";
150
151
152
153
154
155
156
            if (refs[i].GetElementType() == CELL) type = "cell";
            if (refs[i].GetElementType() == FACE) type = "face";
            if (refs[i].GetElementType() == EDGE) type = "edge";
            if (refs[i].GetElementType() == NODE) type = "node";
            os << "(" << type << "," << refs[i]->GlobalID() << ") ";
			Storage::real xyz[3] = {0,0,0};
            refs[i]->Centroid(xyz);
Kirill Terekhov's avatar
Kirill Terekhov committed
157
            os << "(" << xyz[0] << "," << xyz[1] << "," << xyz[2] <<")" <<  std::endl;
158
159
160
161
162
        }


    }

Kirill Terekhov's avatar
Kirill Terekhov committed
163
    void AdaptiveMesh::PrintMesh(std::ostream& os, int cell, int face, int edge, int node)
SilverLife's avatar
SilverLife committed
164
165
    {
        if (cell + face + edge + node == 0) return;
Kirill Terekhov's avatar
Kirill Terekhov committed
166
167
        std::stringstream ss;
        ss << "================= " << rank << " =====================" << std::endl;
SilverLife's avatar
SilverLife committed
168
169
        if (cell)
        {
Kirill Terekhov's avatar
Kirill Terekhov committed
170
            ss << "Cells: " << m->NumberOfCells() <<  std::endl;
171
            for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); ++it) 
SilverLife's avatar
SilverLife committed
172
            {
173
                ss << rank << ": " << it->GlobalID() << " - " << it->LocalID() << " - ";            
SilverLife's avatar
SilverLife committed
174
175
176
                if (it->GetStatus() == Element::Shared) ss << "shared";
                else if (it->GetStatus() == Element::Ghost) ss << "ghost";
                else ss << "none";
177
178
179
180
181
182
183
184
185
186
                
                /*
                ss << " (";
                Storage::integer_array arr = it->IntegerArrayDV(tag_processors);
                for (int i = 0; i < arr.size(); i++) ss << arr[i] << " ";
                ss << ") ";
*/
                /*
                Storage::reference_array refs = hanging_nodes[it->self()];
                if (refs.size() > 0)
SilverLife's avatar
SilverLife committed
187
                {
Kirill Terekhov's avatar
Kirill Terekhov committed
188
                    ss << std::endl << "   Hanging nodes: ";
189
                    PrintRefs(ss,refs);
SilverLife's avatar
SilverLife committed
190
                }
Kirill Terekhov's avatar
Kirill Terekhov committed
191
                ss << std::endl;
192
193
194
195
196
197
                ss << "   ParentSet: " << ElementSet(this,parent_set[*it]).GetName();
                */
/*
                orage::reference_array refs = ref_tag[it->self()];
                PrintRefs(refs);
                */
Kirill Terekhov's avatar
Kirill Terekhov committed
198
                    ss << std::endl;
SilverLife's avatar
SilverLife committed
199
200
201
202
203
            }
        }

        if (face)
        {
Kirill Terekhov's avatar
Kirill Terekhov committed
204
            ss << "Faces: " << m->NumberOfFaces() <<  std::endl;
205
            for(Mesh::iteratorFace it = m->BeginFace(); it != m->EndFace(); ++it) 
SilverLife's avatar
SilverLife committed
206
            {
Kirill Terekhov's avatar
Kirill Terekhov committed
207
208
                ss << rank << ": " << std::setw(2) << it->LocalID() << " " << std::setw(2) << it->GlobalID() << " - " ;            
                ss << std::setw(6);
SilverLife's avatar
SilverLife committed
209
210
211
212
                if (it->GetStatus() == Element::Shared) ss << "shared";
                else if (it->GetStatus() == Element::Ghost) ss << "ghost";
                else ss << "none";

213
214
215

                double xyz[3];
                it->Centroid(xyz);
Kirill Terekhov's avatar
Kirill Terekhov committed
216
                ss << "   (" << std::setw(5) << xyz[0] << " " << std::setw(5) << xyz[1] << " " << std::setw(5) << xyz[2] << ")";
217

218
                ss << "  " << m->GetMarker(*it,m->NewMarker());
219
220
221
222

                ss << " nc(" << it->getNodes().size() << ": "; 
                ElementArray<Node> nodes = it->getNodes();
                for (ElementArray<Node>::iterator node = nodes.begin(); node != nodes.end(); node++)
Kirill Terekhov's avatar
Kirill Terekhov committed
223
                    ss << std::setw(2) << node->GlobalID() << " ";
224
225
226
                ss << ")";

                /*
SilverLife's avatar
SilverLife committed
227
228
229
230
231
232
233
234
235
236
237
238
                Storage::reference_array refs = ref_tag[it->self()];
                if (refs.size() > 0) ss << ". Ref: ";
                for(Storage::reference_array::size_type i = 0; i < refs.size(); ++i)
                {
                    string type = "unknw";
                    if (refs[i].GetElementType() == CELL) type = "cell";
                    if (refs[i].GetElementType() == FACE) type = "face";
                    if (refs[i].GetElementType() == EDGE) type = "edge";
                    if (refs[i].GetElementType() == NODE) type = "node";
                    ss << "(" << type << "," << refs[i]->GlobalID() << ") ";
                }

Kirill Terekhov's avatar
Kirill Terekhov committed
239
                ss << std::endl;
240
241
242
243
244

                {
                    Storage::reference_array refs = hanging_nodes[it->self()];
                    if (refs.size() > 0)
                    {
Kirill Terekhov's avatar
Kirill Terekhov committed
245
                        ss << std::endl << "   Hanging nodes: ";
246
247
                        PrintRefs(ss,refs);
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
248
                    ss << std::endl;
249
                }
250
                */
Kirill Terekhov's avatar
Kirill Terekhov committed
251
                ss << std::endl;
SilverLife's avatar
SilverLife committed
252
253
254
255
256
            }
        }

        if (edge)
        {
Kirill Terekhov's avatar
Kirill Terekhov committed
257
            ss << "Edges: " << m->NumberOfEdges() <<  std::endl;
258
            for(Mesh::iteratorEdge it = m->BeginEdge(); it != m->EndEdge(); ++it) 
SilverLife's avatar
SilverLife committed
259
260
261
262
263
            {
                ss << rank << ": " << it->GlobalID() << " - " ;            
                if (it->GetStatus() == Element::Shared) ss << "shared";
                else if (it->GetStatus() == Element::Ghost) ss << "ghost";
                else ss << "none";
Kirill Terekhov's avatar
Kirill Terekhov committed
264
				/*
SilverLife's avatar
SilverLife committed
265
266
267
268
                Storage::reference_array refs = ref_tag[it->self()];
                if (refs.size() > 0) ss << ". Ref: ";
                for(Storage::reference_array::size_type i = 0; i < refs.size(); ++i)
                {
Kirill Terekhov's avatar
Kirill Terekhov committed
269
                    std::string type = "unknw";
SilverLife's avatar
SilverLife committed
270
271
272
273
274
275
                    if (refs[i].GetElementType() == CELL) type = "cell";
                    if (refs[i].GetElementType() == FACE) type = "face";
                    if (refs[i].GetElementType() == EDGE) type = "edge";
                    if (refs[i].GetElementType() == NODE) type = "node";
                    ss << "(" << type << "," << refs[i]->GlobalID() << ") ";
                }
Kirill Terekhov's avatar
Kirill Terekhov committed
276
277
                */
                ss << std::endl;
SilverLife's avatar
SilverLife committed
278
279
280
281
282
            }
        }

        if (node)
        {
Kirill Terekhov's avatar
Kirill Terekhov committed
283
            ss << "Nodes:" << std::endl;
284
            for(Mesh::iteratorNode it = m->BeginNode(); it != m->EndNode(); ++it) 
SilverLife's avatar
SilverLife committed
285
            {
Kirill Terekhov's avatar
Kirill Terekhov committed
286
287
                ss << rank << ": " << std::setw(2) << it->GlobalID() << " - " ;            
                ss << std::setw(6);
SilverLife's avatar
SilverLife committed
288
289
290
291
292
                if (it->GetStatus() == Element::Shared) ss << "shared";
                else if (it->GetStatus() == Element::Ghost) ss << "ghost";
                else ss << "none";

                {
Kirill Terekhov's avatar
Kirill Terekhov committed
293
					/*
SilverLife's avatar
SilverLife committed
294
295
296
297
                    Storage::reference_array refs = ref_tag[it->self()];
                    if (refs.size() > 0) ss << ". Ref: ";
                    for(Storage::reference_array::size_type i = 0; i < refs.size(); ++i)
                    {
Kirill Terekhov's avatar
Kirill Terekhov committed
298
                        std::string type = "unknw";
SilverLife's avatar
SilverLife committed
299
                        if (refs[i].GetElementType() == CELL) type = "cell";
300
301
						if (refs[i].GetElementType() == FACE) type = "face";
						if (refs[i].GetElementType() == EDGE) type = "edge";
SilverLife's avatar
SilverLife committed
302
303
304
305
                        if (refs[i].GetElementType() == NODE) type = "node";
                        ss << "(" << type << "," << refs[i]->GlobalID() << ") ";
                        
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
306
					*/
307
                    ss << "  " << m->GetMarker(*it,m->NewMarker());
308
309

                    ss << "(" << 
Kirill Terekhov's avatar
Kirill Terekhov committed
310
311
312
                        std::setw(3) << it->RealArray(m->CoordsTag())[0] << " " << 
                        std::setw(3) << it->RealArray(m->CoordsTag())[1] << " " << 
                        std::setw(3) << it->RealArray(m->CoordsTag())[2] << ")";
313

SilverLife's avatar
SilverLife committed
314
                }
Kirill Terekhov's avatar
Kirill Terekhov committed
315
                ss << std::endl;
SilverLife's avatar
SilverLife committed
316
317
318
            }
        }

Kirill Terekhov's avatar
Kirill Terekhov committed
319
320
        ss << "=========================================" << std::endl;
        os << ss.str() << std::endl;
SilverLife's avatar
SilverLife committed
321
322
323
324
325
326
327
    }

    void AdaptiveMesh::UpdateStatus()
    {

        for(ElementType mask = CELL; mask >= NODE; mask = PrevElementType(mask))
        {
328
            for(Mesh::iteratorElement it = m->BeginElement(mask); it != m->EndElement(); it++)
SilverLife's avatar
SilverLife committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
            {
                int stat = 0;
                if (it->GetStatus() == Element::Shared) stat = 1;
                else if (it->GetStatus() == Element::Ghost)  stat = 2;

                tag_status[it->self()] = stat;
            }
        }
    }
    
    void AdaptiveMesh::PrintSet(ElementSet set, std::string offset)
    {
        std::cout << offset << "Set: " << std::endl;

		ElementSet::iterator it = set.Begin();
		while(it != set.End())
        {
346
		    ElementSet parent(m,parent_set[*it]);
SilverLife's avatar
SilverLife committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
            std::cout << offset << it->GlobalID() << " - " << level[*it] << " : ";
		    
            ElementSet::iterator p = parent.Begin();
    		while(p != parent.End())
            {
                std::cout << p->GlobalID() << " ";
                p++;
            }
            std::cout << std::endl;
            it++;
        }

		for(ElementSet child = set.GetChild(); child.isValid(); child = child.GetSibling())
        {
            PrintSet(child,offset + "  ");
        }

    }

366
367
368
    void AdaptiveMesh::SynchronizeSet(ElementSet set)
    {
    #ifdef USE_MPI
369
370
        int size = m->GetProcessorsNumber();
        int rank = m->GetProcessorRank();
371
372
        for (int i = 0; i < size; i++)
        {
373
374
            set.IntegerArray(m->SendtoTag()).push_back(i);
            m->ExchangeMarked();
375
376
377
378
        }
    #endif
    }

SilverLife's avatar
SilverLife committed
379
380
    void AdaptiveMesh::Test()
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
381
        std::cout << rank << ": ================" << std::endl;
SilverLife's avatar
SilverLife committed
382
383
        PrintSet(root,"");
    }
384
385
386
387
	
	
	void AdaptiveMesh::ClearData()
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
388
389
390
		level         = m->DeleteTag(level);
		hanging_nodes = m->DeleteTag(hanging_nodes);
		parent_set    = m->DeleteTag(parent_set);
391
392
393
394
395
396
397
398
		root.DeleteSetTree();
	}
	
	void AdaptiveMesh::PrepareSet()
	{
		//retrive set for coarsening, initialize set if is not present
		if( !root.isValid() )
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
399
			root = m->GetSet("ROOT_SET");
400
401
			if( root == InvalidElement() )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
402
				root = m->CreateSetUnique("ROOT_SET").first;
Kirill Terekhov's avatar
Kirill Terekhov committed
403
				root.SetExchange(ElementSet::SYNC_ELEMENTS_SHARED);
404
				level[root] = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
405
				for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); ++it)
406
407
408
409
410
				{
					root.PutElement(it->self());
					parent_set[it->self()] = root.GetHandle();
				}
			}
SilverLife's avatar
SilverLife committed
411
        }
Kirill Terekhov's avatar
Kirill Terekhov committed
412
		if( !m->HaveGlobalID(CELL) ) m->AssignGlobalID(CELL); //for unique set names
Kirill Terekhov's avatar
Kirill Terekhov committed
413
		m->ResolveSets();
414
415
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
416
	AdaptiveMesh::AdaptiveMesh(Mesh & _m) : m(&_m)
417
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
418
		model = NULL;
419
		//create a tag that stores maximal refinement level of each element
Kirill Terekhov's avatar
Kirill Terekhov committed
420
		level = m->CreateTag("REFINEMENT_LEVEL",DATA_INTEGER,CELL|FACE|EDGE|NODE|ESET,NONE,1);
421
		tag_status = m->CreateTag("TAG_STATUS",DATA_INTEGER,CELL|FACE|EDGE|NODE,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
422
423
		//tag_an = m->CreateTag("TAG_AN",DATA_INTEGER,CELL|FACE|EDGE|NODE,NONE,1);
		//ref_tag = m->CreateTag("REF",DATA_REFERENCE,CELL|FACE|EDGE|NODE,NONE);
424
		//create a tag that stores links to all the hanging nodes of the cell
Kirill Terekhov's avatar
Kirill Terekhov committed
425
		hanging_nodes = m->CreateTag("HANGING_NODES",DATA_REFERENCE,CELL|FACE,NONE);
426
		//create a tag that stores links to sets
Kirill Terekhov's avatar
Kirill Terekhov committed
427
		parent_set = m->CreateTag("PARENT_SET",DATA_REFERENCE,CELL,NONE,1);
428
429
	    size = m->GetProcessorsNumber();
    	rank = m->GetProcessorRank();
430
431
432
433
434
435
436
437
	}
	
	AdaptiveMesh::~AdaptiveMesh()
	{
		//do not delete tags, user may want to repetitively use this class
		//as extension of class mesh in limited code span
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
	void AdaptiveMesh::CheckParentSet()
	{
		std::cout << rank << " enter " << __FUNCTION__ << std::endl;
		int err = 0;
		for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); ++it)
		{
			if( parent_set[*it] == InvalidHandle() )
			{
				std::cout << m->GetProcessorRank() << " parent set not valid on CELL:" << it->LocalID() << " " << Element::StatusName(it->GetStatus()) << " " << level[*it] << std::endl;
				err++;
			}
			else if( GetHandleElementType(parent_set[*it]) != ESET )
			{
				std::cout << m->GetProcessorRank() << " parent set is something else " << ElementTypeName(GetHandleElementType(parent_set[*it])) << ":" << GetHandleID(parent_set[*it]) << " on CELL:" << it->LocalID() << " " << Element::StatusName(it->GetStatus()) << " " << level[*it] << std::endl;
				err++;
			}
		}
		err = m->Integrate(err);
		if( err ) 
		{
			std::cout << rank << " error in " << __FUNCTION__ << std::endl;
			exit(-1);
		}
		std::cout << rank << " exit " << __FUNCTION__ << std::endl;
		
	}
	
465
466
	bool AdaptiveMesh::Refine(TagInteger & indicator)
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
467
        std::cout << rank << " enter " << __FUNCTION__ << std::endl;
468
469
470
		static int call_counter = 0;
		int ret = 0; //return number of refined cells
		//initialize tree structure
Kirill Terekhov's avatar
Kirill Terekhov committed
471
		m->CheckCentroids(__FILE__,__LINE__);
472
		PrepareSet();
Kirill Terekhov's avatar
Kirill Terekhov committed
473
		CheckParentSet();
474
475
476
		int schedule_counter = 1; //indicates order in which refinement will be scheduled
		int scheduled = 1; //indicates that at least one element was scheduled on current sweep
		//0. Extend indicator for edges and faces
Kirill Terekhov's avatar
Kirill Terekhov committed
477
		indicator = m->CreateTag(indicator.GetTagName(),DATA_INTEGER,FACE|EDGE,NONE,1);
478
479
480
		while(scheduled)
		{
			//1.Communicate indicator - it may be not synced
Kirill Terekhov's avatar
Kirill Terekhov committed
481
			m->ExchangeData(indicator,CELL,0);
482
483
			//2.Propogate indicator down to the faces,edges
			//  select schedule for them
Kirill Terekhov's avatar
Kirill Terekhov committed
484
			for(Storage::integer it = 0; it < m->CellLastLocalID(); ++it) if( m->isValidCell(it) )
485
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
486
				Cell c = m->CellByLocalID(it);
487
488
489
490
491
492
493
494
495
496
497
				if( indicator[c] == schedule_counter )
				{
					ElementArray<Element> adj = c.getAdjElements(FACE|EDGE);
					for(ElementArray<Element>::size_type kt = 0; kt < adj.size(); ++kt)
					{
						if( level[adj[kt]] == level[c] ) //do not schedule finer or coarser elements
							indicator[adj[kt]] = schedule_counter; //refine together with current cell
					}
				}
			}
			//3.Communicate indicator on faces and edges
Kirill Terekhov's avatar
Kirill Terekhov committed
498
			m->ExchangeData(indicator,FACE|EDGE,0);
499
500
501
502
			//4.Check for each cell if there is
			//  any hanging node with adjacent in a need to refine,
			//  schedule for refinement earlier.
			scheduled = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
503
			for(Storage::integer it = 0; it < m->CellLastLocalID(); ++it) if( m->isValidCell(it) )
504
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
505
				Cell c = m->CellByLocalID(it);
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
				//already scheduled cells may be required to be refined first
				//if( indicator[c] == 0 ) //some optimization
				{
					bool scheduled_c = false;
					//any finer level edge is scheduled to be refined first
					ElementArray<Edge> edges = c->getEdges();
					for(ElementArray<Edge>::size_type kt = 0; kt < edges.size() && !scheduled_c; ++kt)
					{
						//if a finer edge is scheduled
						//then this cell should be refined first
						if( indicator[edges[kt]] != 0 &&
							level[edges[kt]] > level[c] &&
							indicator[edges[kt]] >= indicator[c] )
						{
							indicator[c] = schedule_counter+1;
							scheduled++;
							scheduled_c = true;
						}
					}
				}
			}
			//5.Go back to 1 until no new elements scheduled
Kirill Terekhov's avatar
Kirill Terekhov committed
528
			scheduled = m->Integrate(scheduled);
529
530
			if( scheduled ) schedule_counter++;
		}
531
		m->ExchangeData(indicator,CELL | FACE | EDGE,0);
Kirill Terekhov's avatar
Kirill Terekhov committed
532
		//m->Save("indicator.pmf");
533
		//6.Refine
Kirill Terekhov's avatar
Kirill Terekhov committed
534
		m->BeginModification();
535
536
537
538
		while(schedule_counter)
		{
			Storage::real xyz[3] = {0,0,0};
			//7.split all edges of the current schedule
Kirill Terekhov's avatar
Kirill Terekhov committed
539
			for(Storage::integer it = 0; it < m->EdgeLastLocalID(); ++it) if( m->isValidEdge(it) )
540
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
541
				Edge e = m->EdgeByLocalID(it);
542
543
544
545
546
				if( !e.Hidden() && indicator[e] == schedule_counter )
				{
					//remember adjacent faces that should get information about new hanging node
					ElementArray<Face> edge_faces = e.getFaces();
					//location on the center of the edge
Kirill Terekhov's avatar
Kirill Terekhov committed
547
					for(Storage::integer d = 0; d < m->GetDimensions(); ++d)
548
549
550
						xyz[d] = (e.getBeg().Coords()[d]+e.getEnd().Coords()[d])*0.5;
					//todo: request transformation of node location according to geometrical model
					//create middle node
Kirill Terekhov's avatar
Kirill Terekhov committed
551
					Node n = m->CreateNode(xyz);
552
					//set increased level for new node
553
					level[n] = level[e.getBeg()] = level[e.getEnd()] = level[e]+1;
554
555
556
557
					//for each face provide link to a new hanging node
					for(ElementArray<Face>::size_type kt = 0; kt < edge_faces.size(); ++kt)
						hanging_nodes[edge_faces[kt]].push_back(n);
					//split the edge by the middle node
Kirill Terekhov's avatar
Kirill Terekhov committed
558
					ElementArray<Edge> new_edges = Edge::SplitEdge(e,ElementArray<Node>(m,1,n.GetHandle()),0);
559
560
561
562
563
					//set increased level for new edges
					level[new_edges[0]] = level[new_edges[1]] = level[e]+1;
				}
			}
			//8.split all faces of the current schedule, using hanging nodes on edges
Kirill Terekhov's avatar
Kirill Terekhov committed
564
			for(Storage::integer it = 0; it < m->FaceLastLocalID(); ++it) if( m->isValidFace(it) )
565
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
566
				Face f = m->FaceByLocalID(it);
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
				if( !f.Hidden() && indicator[f] == schedule_counter )
				{
					//connect face center to hanging nodes of the face
					Storage::reference_array face_hanging_nodes = hanging_nodes[f];
					//remember adjacent cells that should get information about new hanging node
					//and new hanging edges
					ElementArray<Cell> face_cells = f.getCells();
					//create node at face center
					//f->Centroid(xyz);
					for(int d = 0; d < 3; ++d) xyz[d] = 0.0;
					for(Storage::reference_array::size_type kt = 0; kt < face_hanging_nodes.size(); ++kt)
						for(int d = 0; d < 3; ++d) xyz[d] += face_hanging_nodes[kt].getAsNode().Coords()[d];
					for(int d = 0; d < 3; ++d) xyz[d] /= (Storage::real)face_hanging_nodes.size();
					//todo: request transformation of node location according to geometrical model
					//create middle node
Kirill Terekhov's avatar
Kirill Terekhov committed
582
					Node n = m->CreateNode(xyz);
583
					//set increased level for the new node
584
					level[n] = level[f]+1;
585
586
587
					//for each cell provide link to new hanging node
					for(ElementArray<Face>::size_type kt = 0; kt < face_cells.size(); ++kt)
						hanging_nodes[face_cells[kt]].push_back(n);
Kirill Terekhov's avatar
Kirill Terekhov committed
588
589
					ElementArray<Node> edge_nodes(m,2); //to create new edges
					ElementArray<Edge> hanging_edges(m,face_hanging_nodes.size());
590
591
592
593
					edge_nodes[0] = n;
					for(Storage::reference_array::size_type kt = 0; kt < face_hanging_nodes.size(); ++kt)
					{
						edge_nodes[1] = face_hanging_nodes[kt].getAsNode();
Kirill Terekhov's avatar
Kirill Terekhov committed
594
						hanging_edges[kt] = m->CreateEdge(edge_nodes).first;
595
596
597
598
599
600
601
602
603
604
605
						//set increased level for new edges
						level[hanging_edges[kt]] = level[f]+1;
					}
					//split the face by these edges
					ElementArray<Face> new_faces = Face::SplitFace(f,hanging_edges,0);
					//set increased level to new faces
					for(ElementArray<Face>::size_type kt = 0; kt < new_faces.size(); ++kt)
						level[new_faces[kt]] = level[f]+1;
				}
			}
			//this tag helps recreate internal face
Kirill Terekhov's avatar
Kirill Terekhov committed
606
			TagReferenceArray internal_face_edges = m->CreateTag("INTERNAL_FACE_EDGES",DATA_REFERENCE,NODE,NODE,4);
607
			//this marker helps detect edges of current cell only
Kirill Terekhov's avatar
Kirill Terekhov committed
608
			MarkerType mark_cell_edges = m->CreateMarker();
609
			//this marker helps detect nodes hanging on edges of unrefined cell
Kirill Terekhov's avatar
Kirill Terekhov committed
610
			MarkerType mark_hanging_nodes = m->CreateMarker();
611
			//9.split all cells of the current schedule
Kirill Terekhov's avatar
Kirill Terekhov committed
612
			for(Storage::integer it = 0; it < m->CellLastLocalID(); ++it) if( m->isValidCell(it) )
613
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
614
				Cell c = m->CellByLocalID(it);
615
616
617
618
619
620
621
622
623
624
625
				if( !c.Hidden() && indicator[c] == schedule_counter )
				{
					Storage::reference_array cell_hanging_nodes = hanging_nodes[c]; //nodes to be connected
					//create node at cell center
					for(int d = 0; d < 3; ++d) xyz[d] = 0.0;
					for(Storage::reference_array::size_type kt = 0; kt < cell_hanging_nodes.size(); ++kt)
						for(int d = 0; d < 3; ++d) xyz[d] += cell_hanging_nodes[kt].getAsNode().Coords()[d];
					for(int d = 0; d < 3; ++d) xyz[d] /= (Storage::real)cell_hanging_nodes.size();
					//c->Centroid(xyz);
					//todo: request transformation of node location according to geometrical model
					//create middle node
Kirill Terekhov's avatar
Kirill Terekhov committed
626
					Node n = m->CreateNode(xyz);
627
					//set increased level for the new node
628
					level[n] = level[c]+1;
629
630
631
632
633
					//retrive all edges of current face to mark them
					ElementArray<Edge> cell_edges = c.getEdges();
					//mark all edges so that we can retive them later
					cell_edges.SetMarker(mark_cell_edges);
					//connect face center to centers of faces by edges
Kirill Terekhov's avatar
Kirill Terekhov committed
634
635
					ElementArray<Node> edge_nodes(m,2);
					ElementArray<Edge> edges_to_faces(m,cell_hanging_nodes.size());
636
637
638
639
640
641
					edge_nodes[0] = n;
					for(Storage::reference_array::size_type kt = 0; kt < cell_hanging_nodes.size(); ++kt)
					{
						assert(cell_hanging_nodes[kt].isValid());
						//todo: unmark hanging node on edge if no more cells depend on it
						edge_nodes[1] = cell_hanging_nodes[kt].getAsNode();
Kirill Terekhov's avatar
Kirill Terekhov committed
642
						edges_to_faces[kt] = m->CreateEdge(edge_nodes).first;
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
						//set increased level for new edges
						level[edges_to_faces[kt]] = level[c]+1;
						//for each node other then the hanging node of the face
						//(this is hanging node on the edge)
						//we record a pair of edges to reconstruct internal faces
						ElementArray<Edge> hanging_edges = cell_hanging_nodes[kt].getEdges(mark_cell_edges,0);
						for(ElementArray<Edge>::size_type lt = 0; lt < hanging_edges.size(); ++lt)
						{
							//get hanging node on the edge
							assert(hanging_edges[lt].getBeg() == cell_hanging_nodes[kt] || hanging_edges[lt].getEnd() == cell_hanging_nodes[kt]);
							Node v = hanging_edges[lt].getBeg() == cell_hanging_nodes[kt]? hanging_edges[lt].getEnd() : hanging_edges[lt].getBeg();
							//mark so that we can collect all of them
							v.SetMarker(mark_hanging_nodes);
							//fill the edges
							Storage::reference_array face_edges = internal_face_edges[v];
							//fill first two in forward order
							//this way we make a closed loop
							assert(face_edges[0] == InvalidElement() || face_edges[2] == InvalidElement());
							if( face_edges[0] == InvalidElement() )
							{
								face_edges[0] = edges_to_faces[kt];
								face_edges[1] = hanging_edges[lt];
							}
							else //last two in reverse
							{
								assert(face_edges[2] ==InvalidElement());
								face_edges[2] = hanging_edges[lt];
								face_edges[3] = edges_to_faces[kt];
							}
						}
					}
					//remove marker from cell edges
					cell_edges.RemMarker(mark_cell_edges);
					//now we have to create internal faces
					ElementArray<Node> edge_hanging_nodes = c.getNodes(mark_hanging_nodes,0);
Kirill Terekhov's avatar
Kirill Terekhov committed
678
					ElementArray<Face> internal_faces(m,edge_hanging_nodes.size());
679
680
681
682
683
684
685
686
687
688
					//unmark hanging nodes on edges
					edge_hanging_nodes.RemMarker(mark_hanging_nodes);
					for(ElementArray<Node>::size_type kt = 0; kt < edge_hanging_nodes.size(); ++kt)
					{
						//create a face based on collected edges
						Storage::reference_array face_edges = internal_face_edges[edge_hanging_nodes[kt]];
						assert(face_edges[0].isValid());
						assert(face_edges[1].isValid());
						assert(face_edges[2].isValid());
						assert(face_edges[3].isValid());
Kirill Terekhov's avatar
Kirill Terekhov committed
689
						internal_faces[kt] = m->CreateFace(ElementArray<Edge>(m,face_edges.begin(),face_edges.end())).first;
690
691
692
693
694
						//set increased level
						level[internal_faces[kt]] = level[c]+1;
						//clean up structure, so that other cells can use it
						edge_hanging_nodes[kt].DelData(internal_face_edges);
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
695
696
697
698
699
700
701
					//if( c.GlobalID() == 228 )
					//{
					//	double cnt[3];
					//	c.Centroid(cnt);
					//	std::cout << "Split CELL:" << c.LocalID() << " " << c.GlobalID() << " " << Element::StatusName(c.GetStatus()) << " " << cnt[0] << " " << cnt[1] << " " << cnt[2] << std::endl;
					//	
					//}
702
703
704
					//split the cell
					ElementArray<Cell> new_cells = Cell::SplitCell(c,internal_faces,0);
					//retrive parent set
Kirill Terekhov's avatar
Kirill Terekhov committed
705
					ElementSet parent(m,parent_set[c]);
706
					//create set corresponding to old coarse cell
Kirill Terekhov's avatar
Kirill Terekhov committed
707
708
					Storage::real cnt[3];
					c.Centroid(cnt);
709
					std::stringstream set_name;
Kirill Terekhov's avatar
Kirill Terekhov committed
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
					//set_name << parent.GetName() << "_C" << c.GlobalID(); //rand may be unsafe
					set_name << base64_encode_((unsigned char *)cnt,3*sizeof(double)/sizeof(unsigned char));
					/*
					ElementSet check_set = m->GetSet(set_name.str());
					if( check_set.isValid() )
					{
						std::cout << rank << " set " << set_name.str() << " for cell " << c.GlobalID() << " " << Element::StatusName(c.GetStatus()) << " already exists" << std::endl;
						if( check_set->HaveParent() )
							std::cout << rank << " parent is " << check_set->GetParent()->GetName() << " cell parent is " << parent.GetName() << std::endl;
						std::cout << rank << " Elements: ";
						for(ElementSet::iterator it = check_set.Begin(); it != check_set.End(); ++it)
							std::cout << ElementTypeName(it->GetElementType()) << ":" << it->LocalID() << "," << it->GlobalID() << "," << Element::StatusName(c.GetStatus()) << "," << level[*it] << " ";
						std::cout << std::endl;
					}
					*/
Kirill Terekhov's avatar
Kirill Terekhov committed
725
					ElementSet cell_set = m->CreateSetUnique(set_name.str()).first;
Kirill Terekhov's avatar
Kirill Terekhov committed
726
					cell_set->SetExchange(ElementSet::SYNC_ELEMENTS_ALL);
727
728
729
730
731
732
733
734
					level[cell_set] = level[c]+1;
					//set up increased level for the new cells
					for(ElementArray<Cell>::size_type kt = 0; kt < new_cells.size(); ++kt)
					{
						level[new_cells[kt]] = level[c]+1;
						cell_set.PutElement(new_cells[kt]);
						parent_set[new_cells[kt]] = cell_set.GetHandle();
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
735
736
737
738
739
740
741
742
743
744
					/*
					if( check_set.isValid() )
					{
						std::cout << rank << " Elements: ";
						for(ElementSet::iterator it = check_set.Begin(); it != check_set.End(); ++it)
							std::cout << ElementTypeName(it->GetElementType()) << ":" << it->LocalID() << "," << it->GlobalID() << "," << Element::StatusName(c.GetStatus()) << "," << level[*it] << " ";
						std::cout << std::endl;
					}
					*/
					//if( !cell_set->HaveParent() )
745
					parent.AddChild(cell_set);
Kirill Terekhov's avatar
Kirill Terekhov committed
746
					//else assert(cell_set->GetParent() == parent);
747
748
749
750
					//increment number of refined cells
					ret++;
				}
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
751
752
753
			m->ReleaseMarker(mark_hanging_nodes);
			m->ReleaseMarker(mark_cell_edges);
			m->DeleteTag(internal_face_edges);
754
755
756
			//10.jump to later schedule, and go to 7.
			schedule_counter--;
		}
SilverLife's avatar
SilverLife committed
757

758
		//free created tag
Kirill Terekhov's avatar
Kirill Terekhov committed
759
		m->DeleteTag(indicator,FACE|EDGE);
SilverLife's avatar
SilverLife committed
760

761
		/*
762
763
        MarkerType marker_new = m->CreateMarker();
        for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); ++it) 
SilverLife's avatar
SilverLife committed
764
        {
765
            if (it->GetMarker(m->NewMarker()) == false) continue;
766
767
768
769
            it->SetMarker(marker_new);

        }

770
        for(Mesh::iteratorFace it = m->BeginFace(); it != m->EndFace(); ++it) 
771
        {
772
            if (it->GetMarker(m->NewMarker()) == false) continue;
773
774
            it->SetMarker(marker_new);
        }
SilverLife's avatar
SilverLife committed
775

776
        for(Mesh::iteratorEdge it = m->BeginEdge(); it != m->EndEdge(); ++it) 
777
        {
778
            if (it->GetMarker(m->NewMarker()) == false) continue;
779
            it->SetMarker(marker_new);
SilverLife's avatar
SilverLife committed
780
        }
781

782
        for(Mesh::iteratorNode it = m->BeginNode(); it != m->EndNode(); ++it) 
SilverLife's avatar
SilverLife committed
783
        {
784
            if (it->GetMarker(m->NewMarker()) == false) continue;
785
            it->SetMarker(marker_new);
SilverLife's avatar
SilverLife committed
786
        }
787
        */
SilverLife's avatar
SilverLife committed
788
789


790
		//11. Restore parallel connectivity, global ids
Kirill Terekhov's avatar
Kirill Terekhov committed
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
		{
			std::fstream fout;
			fout.open("out"+std::to_string(m->GetProcessorRank())+".txt",std::ios::out);
			for(ElementType etype = NODE; etype <= ESET; etype = NextElementType(etype) )
			{
				int cnt = 0;
				for(Mesh::iteratorElement it = m->BeginElement(etype); it != m->EndElement(); ++it)
				{
					if( it->New() )
					{
						fout << "new element " << ElementTypeName(etype) << ":" << it->LocalID() <<std::endl;//<< " gid " << it->GlobalID() << std::endl;
						cnt++;
					}
				}
				fout << ElementTypeName(etype) << " count: " << cnt << std::endl;
			}
			
			fout.close();
		}
SilverLife's avatar
SilverLife committed
810
        //if (call_counter == 0)
Kirill Terekhov's avatar
Kirill Terekhov committed
811
		m->ResolveModification();
Kirill Terekhov's avatar
Kirill Terekhov committed
812
		//m->SynchronizeMarker(m->NewMarker(),CELL|FACE|EDGE|NODE,SYNC_BIT_OR);
813
        //ExchangeGhost(3,NODE); // Construct Ghost cells in 2 layers connected via nodes
814
815
		//12. Let the user update their data
		//todo: call back function for New() cells
Kirill Terekhov's avatar
Kirill Terekhov committed
816
		if( model ) model->Adaptation(*m);
817
		//13. Delete old elements of the mesh
Kirill Terekhov's avatar
Kirill Terekhov committed
818
		m->ApplyModification();
Kirill Terekhov's avatar
Kirill Terekhov committed
819
		
Kirill Terekhov's avatar
Kirill Terekhov committed
820
		//m->ExchangeGhost(1,NODE,m->NewMarker());
821
		//14. Done
Kirill Terekhov's avatar
Kirill Terekhov committed
822
        //cout << rank << ": Before end " << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
823
		m->EndModification();
Kirill Terekhov's avatar
Kirill Terekhov committed
824
825
		
		
826
		//ExchangeData(hanging_nodes,CELL | FACE,0);
Kirill Terekhov's avatar
Kirill Terekhov committed
827
        //m->ResolveSets();
828

Kirill Terekhov's avatar
Kirill Terekhov committed
829
830
831
832
833
834
        //m->BeginModification();
        //    m->ExchangeGhost(1,NODE,marker_new); // Construct Ghost cells in 2 layers connected via nodes
        //    m->ReleaseMarker(marker_new,CELL|FACE|EDGE|NODE);
		//m->ApplyModification();
    	//m->EndModification();
    	//PrintSet();
Kirill Terekhov's avatar
Kirill Terekhov committed
835
836
837
838
839
840
841
842
    	//m->ResolveSets();
		m->CheckCentroids(__FILE__,__LINE__);
		m->ExchangeData(parent_set,CELL,0);
		m->CheckCentroids(__FILE__,__LINE__);
		m->ResolveSets();
		m->CheckCentroids(__FILE__,__LINE__);
		m->ExchangeData(parent_set,CELL,0);
		m->CheckCentroids(__FILE__,__LINE__);
Kirill Terekhov's avatar
Kirill Terekhov committed
843
844
		m->ExchangeData(hanging_nodes,CELL | FACE,0);
		m->CheckCentroids(__FILE__,__LINE__);
Kirill Terekhov's avatar
Kirill Terekhov committed
845
		//restore face orientation
Kirill Terekhov's avatar
Kirill Terekhov committed
846
847
848
849
850
851
852
853
854
		//BUG: bad orientation not fixed automatically
		int nfixed = 0;
		for(Mesh::iteratorFace it = m->BeginFace(); it != m->EndFace(); ++it) 
			if( !it->CheckNormalOrientation() )
			{
				it->FixNormalOrientation();
				nfixed++;
			}
			//std::cout << "Face " << it->LocalID() << " oriented incorrectly " << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
855
856
857
858
859
860
861
862
863
864
865
866
		if( nfixed ) std::cout << rank << " fixed " << nfixed << " faces" << std::endl;
		/*
		for(Mesh::iteratorCell it = m->BeginCell(); it != m->EndCell(); ++it)
		{
			if( parent_set[*it] == InvalidHandle() )
				std::cout << m->GetProcessorRank() << " parent set not valid on CELL:" << it->LocalID() << " " << Element::StatusName(it->GetStatus()) << std::endl;
			else if( GetHandleElementType(parent_set[*it]) != ESET )
				std::cout << m->GetProcessorRank() << " parent set is something else " << ElementTypeName(GetHandleElementType(parent_set[*it])) << ":" << GetHandleID(parent_set[*it]) << " on CELL:" << it->LocalID() << " " << Element::StatusName(it->GetStatus()) << std::endl;
		}
		*/
		std::cout << rank << " check parent_set at end" << std::endl;
		CheckParentSet();
867
868

        //ExchangeData(hanging_nodes,CELL | FACE,0);
Kirill Terekhov's avatar
Kirill Terekhov committed
869
        //cout << rank << ": After end " << std::endl;
870
		//reorder element's data to free up space
Kirill Terekhov's avatar
Kirill Terekhov committed
871
		m->ReorderEmpty(CELL|FACE|EDGE|NODE);
872
873
		//return number of refined cells
		call_counter++;
Kirill Terekhov's avatar
Kirill Terekhov committed
874
        std::cout << rank << " exit " << __FUNCTION__ << std::endl;
875
876
		return ret != 0;
	}
877
878
879
880
881
882
883
884
885
886
887
888

    void OperationMin(const Tag & tag, const Element & element, const INMOST_DATA_BULK_TYPE * data, INMOST_DATA_ENUM_TYPE size)
    {
        int value  =  (int)*((int*)data);


        if (value < element->Integer(tag))
        {
            element->Integer(tag) = value;
        }
        (void)size;
    }
889
	
890
891
    void AdaptiveMesh::SynchronizeIndicated(TagInteger& indicator)
    {
892
        if (m->GetProcessorsNumber() == 1) return;
Kirill Terekhov's avatar
Kirill Terekhov committed
893
        std::cout << rank << " enter " << __FUNCTION__ << std::endl;
894
        int rank = m->GetProcessorRank();
895
896

        // Check all sets. All elements in sets must be indicated. At first we check indicator in local processor, and second integrate data
897
898
        TagInteger tag_indicated = m->CreateTag("INDICATED",DATA_INTEGER,ESET,NONE,1);
        for(Mesh::iteratorSet it = m->BeginSet(); it != m->EndSet(); ++it) 
899
        {
900
            ElementSet set = ElementSet(m,*it);
901
902
903
904
905
906
907
            set.Integer(tag_indicated) = 1;
            ElementSet::iterator p = set.Begin();
            while(p != set.End())
            {
                if (indicator[*p] == 0)
                {
                    tag_indicated[set] = 0;
908
                    //std::cout << rank << ": Set " << set.GetName() << " not all indicated" << endl;
909
910
911
912
913
914
915
                    break;
                }

                p++;
            }
        }

916
917
        m->ReduceData(tag_indicated,ESET,0,OperationMin);
        m->ExchangeData(tag_indicated,ESET,0);
918

919
        for(Mesh::iteratorSet it = m->BeginSet(); it != m->EndSet(); ++it) 
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
            if (it->Integer(tag_indicated) == 0)
            {
                ElementSet::iterator p = it->Begin();
                while(p != it->End())
                {
                    p->Integer(indicator) = 0;
                    p++;
                }
            }
        /*
                stringstream ss;
        for(Mesh::iteratorSet it = BeginSet(); it != EndSet(); ++it) 
        {
            ss << it->GetName() << " - " << it->Integer(tag_indicated) << endl;;
        }
935
        cout << rank << " Sets: \n" << ss.str() << endl;
936
        */
Kirill Terekhov's avatar
Kirill Terekhov committed
937
        std::cout << rank << " exit " << __FUNCTION__ << std::endl;
938
939
    }

940
941
	bool AdaptiveMesh::Coarse(TagInteger & indicator)
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
942
        std::cout << rank << " enter " << __FUNCTION__ << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
943
        //return false;
Kirill Terekhov's avatar
Kirill Terekhov committed
944
		m->CheckCentroids(__FILE__,__LINE__);
945
946
947
948
949
		static int call_counter = 0;
		//return number of coarsened cells
		int ret = 0;
		//initialize tree structure
		PrepareSet();
Kirill Terekhov's avatar
Kirill Terekhov committed
950
951
		SynchronizeIndicated(indicator);
		CheckParentSet();
952
953
954
		int schedule_counter = 1; //indicates order in which refinement will be scheduled
		int scheduled = 1, unscheduled = 0; //indicates that at least one element was scheduled on current sweep
		//TagInteger coarsened = CreateTag("COARSENED",DATA_INTEGER,CELL,NONE,1);
Kirill Terekhov's avatar
Kirill Terekhov committed
955
		TagInteger coarse_indicator = m->CreateTag("COARSE_INDICATOR",DATA_INTEGER,EDGE,NONE,1); //used to find on fine cells indicator on coarse cells
956
		//0. Extend indicator for sets, edges and faces
Kirill Terekhov's avatar
Kirill Terekhov committed
957
		indicator = m->CreateTag(indicator.GetTagName(),DATA_INTEGER,FACE|EDGE,NONE,1);
958
959
960
961
962
963
964
965
		while(scheduled || unscheduled)
		{
			// rules
			// a) If there is adjacent finer edge that is not marked for coarsening
			// then this cell should not be coarsened
			// b) If there is adjacent coarser cell, then this cell should be coarsened
			// first
			//0.Communicate indicator - it may be not synced
Kirill Terekhov's avatar
Kirill Terekhov committed
966
			m->ExchangeData(indicator,CELL,0);
967
968
969
970
971
972
973
			//1. Mark each adjacent face/edge for coarsement schedule
			// problem: should mark so that if every adjacent cell is coarsened
			// then adjacent face/edge are also coarsened
			for(ElementType etype = EDGE; etype <= FACE; etype = NextElementType(etype))
			{
				//for(Storage::integer it = 0; it < LastLocalID(etype); ++it) if( isValidElement(etype,it) )
				//	indicator[ElementByLocalID(etype,it)] = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
974
				for(Storage::integer it = 0; it < m->LastLocalID(etype); ++it) if( m->isValidElement(etype,it) )
975
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
976
					Element e = m->ElementByLocalID(etype,it);
977
978
979
980
					ElementArray<Cell> adj = e.getCells();
					indicator[e] = INT_MAX;
					for(ElementArray<Element>::size_type kt = 0; kt < adj.size(); ++kt)
						if( level[e] == level[adj[kt]]) indicator[e] = std::min(indicator[e],indicator[adj[kt]]);
981
					//if (indicator[e] == INT_MAX) cout << rank << ": " << ElementTypeName(e.GetElementType()) << e.GlobalID() << endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
982
					//assert(indicator[e] != INT_MAX);
983
984
985
				}
			}
			//2.Communicate indicator on faces and edges
Kirill Terekhov's avatar
Kirill Terekhov committed
986
			m->ReduceData(indicator,FACE|EDGE,0,ReduceMin);
987
			m->ExchangeData(indicator,FACE|EDGE,0);
988
989
990
			//3.If there is adjacent finer edge that are not marked for coarsening
			// then this cell should not be coarsened
			unscheduled = scheduled = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
991
			for(Storage::integer it = 0; it < m->CellLastLocalID(); ++it) if( m->isValidCell(it) )
992
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
993
				Cell c = m->CellByLocalID(it);
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
				if( indicator[c] )
				{
					ElementArray<Edge> edges = c.getEdges();
					for(ElementArray<Edge>::size_type kt = 0; kt < edges.size(); ++kt)
					{
						if( level[edges[kt]] > level[c] && indicator[edges[kt]] == 0 )
						{
							indicator[c] = 0;
							unscheduled++;
						}
					}
				}
			}
			//4. Propogate coarsement info over set tree to detect valid coarsenings.
			// go down over sets, if set does not have children and all of the cells
			// of the set are marked for coarsening, then mark the set for coarsement
			// otherwise unmark.
			// Unmark all cells that are not to be coarsened
Kirill Terekhov's avatar
Kirill Terekhov committed
1012
			for(Storage::integer it = 0; it < m->CellLastLocalID(); ++it) if( m->isValidCell(it) )
1013
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1014
				Cell c = m->CellByLocalID(it);
1015
                //if (!isValidElement(c.GetHandle())) continue;
1016
1017
				if( indicator[c] )
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
1018
					ElementSet parent(m,parent_set[c]);
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
					//intermediate cell may not be coarsened
					//root set may not have coarsening cells
					if( parent.HaveChild() || !parent.HaveParent() )
					{
						indicator[c] = 0;
						unscheduled++;
					}
					else
					{
						Storage::integer schedule_first = 0;
						bool check = true;
						//check that all elements of the set are to be coarsened
						for(ElementSet::iterator it = parent.Begin(); it != parent.End(); ++it)
						{
							check &= (indicator[it->self()] != 0);
							schedule_first = std::max(schedule_first,indicator[it->self()]);
						}
						if(!check)
						{
							indicator[c] = 0;
							unscheduled++;
						}
						else if( indicator[c] != schedule_first )
						{
							indicator[c] = schedule_first;
							unscheduled++;
						}
					}
				}
			}
			//5.If there is an adjacent coarser element to be refined, then
			//   this one should be scheduled to be refined first
			//a) clean up coarse indicator tag
Kirill Terekhov's avatar
Kirill Terekhov committed
1052
1053
			for(Storage::integer it = 0; it < m->EdgeLastLocalID(); ++it) if( m->isValidEdge(it) )
				coarse_indicator[m->EdgeByLocalID(it)] = 0;
1054
			//b) each cell mark it's finer edges with cell's schedule
Kirill Terekhov's avatar
Kirill Terekhov committed
1055
			for(Storage::integer it = 0; it < m->CellLastLocalID(); ++it) if( m->isValidCell(it) )
1056
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1057
				Cell c = m->CellByLocalID(it);
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
				if( indicator[c] )
				{
					ElementArray<Element> adj = c.getAdjElements(EDGE);
					for(ElementArray<Element>::size_type kt = 0; kt < adj.size(); ++kt)
					{
						if( level[adj[kt]] > level[c] ) //only finer edges
							coarse_indicator[adj[kt]] = std::max(coarse_indicator[adj[kt]],indicator[c]);
					}
				}
			}
			//c) data reduction to get maximum over mesh partition
Kirill Terekhov's avatar
Kirill Terekhov committed
1069
			m->ReduceData(coarse_indicator,EDGE,0,ReduceMax);
1070
			m->ExchangeData(coarse_indicator,EDGE,0);
1071
			//d) look from cells if any edge is coarsened earlier
Kirill Terekhov's avatar
Kirill Terekhov committed
1072
			for(Storage::integer it = 0; it < m->CellLastLocalID(); ++it) if( m->isValidCell(it) )
1073
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1074
				Cell c = m->CellByLocalID(it);
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
				if( indicator[c] )
				{
					ElementArray<Element> adj = c.getAdjElements(EDGE);
					for(ElementArray<Element>::size_type kt = 0; kt < adj.size(); ++kt)
					{
						if( level[c] == level[adj[kt]] && //do not look from coarse cell onto finer edge
						    indicator[c] <= coarse_indicator[adj[kt]])
						{
							indicator[c] = coarse_indicator[adj[kt]]+1;
							scheduled++;
						}
					}
				}
			}
1089
			m->ExchangeData(indicator,CELL|FACE|EDGE,0);
1090
			//5.Go back to 1 until no new elements scheduled
Kirill Terekhov's avatar
Kirill Terekhov committed
1091
1092
			scheduled = m->Integrate(scheduled);
			unscheduled = m->Integrate(unscheduled);
1093
1094
1095
			if( scheduled ) schedule_counter++;
		}
		//cleanup
Kirill Terekhov's avatar
Kirill Terekhov committed
1096
		coarse_indicator = m->DeleteTag(coarse_indicator);
1097
		//Make schedule which elements should be refined earlier.
Kirill Terekhov's avatar
Kirill Terekhov committed
1098
		m->BeginModification();
1099
1100
1101
1102
1103
1104
		while(schedule_counter)
		{
			//unite cells
			//should find and set hanging nodes on faces
			//find single node at the center, all other nodes,
			//adjacent over edge are hanging nodes
Kirill Terekhov's avatar
Kirill Terekhov committed
1105
			for(Storage::integer it = 0; it < m->CellLastLocalID(); ++it) if( m->isValidCell(it) )
1106
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1107
				Cell c = m->CellByLocalID(it);
1108
1109
1110
				if( !c.Hidden() && indicator[c] == schedule_counter )
				{
					//this set contains all the cells to be united
Kirill Terekhov's avatar
Kirill Terekhov committed
1111
1112
					ElementSet parent(m,parent_set[c]);
					ElementArray<Cell> unite_cells(m,parent.Size());
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
					//unmark indicator to prevent coarsement with next element
					Storage::integer kt = 0;
					for(ElementSet::iterator jt = parent.Begin(); jt != parent.End(); ++jt)
					{
						unite_cells[kt++] = jt->getAsCell();