File: BackgroundSF.cc

package info (click to toggle)
madlib 1.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,196 kB
  • sloc: cpp: 39,851; sh: 10,041; makefile: 473
file content (324 lines) | stat: -rw-r--r-- 9,986 bytes parent folder | download | duplicates (6)
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
// -------------------------------------------------------------------
// MAdLib - Copyright (C) 2008-2009 Universite catholique de Louvain
//
// See the Copyright.txt and License.txt files for license information. 
// You should have received a copy of these files along with MAdLib. 
// If not, see <http://www.madlib.be/license/>
//
// Please report all bugs and problems to <contrib@madlib.be>
//
// Authors: Gaetan Compere, Jean-Francois Remacle
// -------------------------------------------------------------------

#include "BackgroundSF.h"
#include "IsoMeshSize.h"
#include "MAdDefines.h"

#include <math.h>
#include <cmath>
#include <string.h>
#include <map>

namespace MAd {

  // -------------------------------------------------------------------
  BackgroundSF::BackgroundSF(std::string _name): 
    SizeFieldBase(_name), bgModel(NULL), bgMesh(NULL)
  {
    pMSizeFieldId = MD_newMeshDataId("");
  }

  // -------------------------------------------------------------------
  BackgroundSF::~BackgroundSF()
  {
    MD_deleteMeshDataId(pMSizeFieldId);
    if ( bgMesh ) M_delete(bgMesh);
    if ( bgModel ) GM_delete(bgModel);
  }

  // -------------------------------------------------------------------
  void BackgroundSF::setSize(pEntity ent, double h)
  {
    pMSize pS = new IsoMeshSize(h);
    setSize(ent,pS);
  }

  // -------------------------------------------------------------------
  void BackgroundSF::setSize(pEntity pEnt, pMSize pS)
  {
    void * temp;
    if( EN_getDataPtr(pEnt,pMSizeFieldId,&temp) ) {
      delete (pMSize)temp;
      EN_modifyDataPtr(pEnt,pMSizeFieldId,pS);
    }
    else {
      EN_attachDataPtr(pEnt,pMSizeFieldId,pS);
    }
  }

  // -------------------------------------------------------------------
  // GC warning: very low implementation: should use octrees
  pMSize BackgroundSF::getSize(const pVertex pv) const
  {
    if ( M_dim(bgMesh) < 3 ) {
      MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                  "Background size fields not implemented for dim < 3");
    }

    double xyz[3];
    V_coord(pv,xyz);

    pRegion pr;
    pRegion rBest = NULL;
    {
      RIter rit = M_regionIter(bgMesh);
      while( ( pr = RIter_next(rit) ) )
        {
          if ( !R_inBox(pr,xyz,1.e-6) ) continue;
          if ( !R_contains(pr,xyz,1.e-6) ) continue;
          rBest = pr;
          break;
        }
      RIter_delete(rit);
    }
    if ( !rBest ) {
    
      double best = 1.e12;
      double tmp[4], tmp2;
      RIter rit = M_regionIter(bgMesh);
      while( ( pr = RIter_next(rit) ) )
        {
          R_linearParams(pr,xyz,tmp);
          tmp[3] = 1. - tmp[0] - tmp[1] - tmp[2];
          
          double mi = std::min(tmp[0],std::min(tmp[1],std::min(tmp[2],tmp[3])));
          double ma = std::max(tmp[0],std::max(tmp[1],std::max(tmp[2],tmp[3])));
        
          tmp2 = 0.;
          if ( mi < 0. ) tmp2 = std::abs(mi);
          if ( ma > 1. ) tmp2 = std::max(tmp2,ma-1.);

          if ( tmp2 < best ) {
            best = tmp2;
            rBest = pr;
          }
        }
      RIter_delete(rit);
    }

    void * temp;
    if( EN_getDataPtr((pEntity)rBest,pMSizeFieldId,&temp) ) {
      return MS_copy((pMSize)temp);
    }
    return NULL;
  }

  // -------------------------------------------------------------------
  void BackgroundSF::loadData(std::string mshName)
  {
    GM_create(&bgModel,"bgModel");
    bgMesh = M_new(bgModel);
    M_load(bgMesh,mshName.c_str());

    FILE * fp = fopen(mshName.c_str(),"r");
    if(!fp) {
      MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                  "Unknown file %s",mshName.c_str());
    }

#ifdef PARALLEL
    MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                "Set sizes from file not implemented in parallel"); 
#endif
    
    char str[256] = "XXX";
    double version = 1.0;
    
    std::map<int,pEntity> elId;

    while(1) {

      while(str[0] != '$') {
        if(!fgets(str, sizeof(str), fp) || feof(fp)) {
          MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                      "No data section found in file %s",
                                      mshName.c_str());
        }
      }

      if(!strncmp(&str[1], "MeshFormat", 10)) {
        if(!fgets(str, sizeof(str), fp)) throw;
        int format, size;
        if(sscanf(str, "%lf %d %d", &version, &format, &size) != 3) throw;
      }
      else if(!strncmp(&str[1], "ELM", 3) || !strncmp(&str[1], "Elements", 8)) {
        if(!fgets(str, sizeof(str), fp)) throw;
        int numElements;
        sscanf(str, "%d", &numElements);

        for(int i = 0; i < numElements; i++) {
          int num, type, physical = 0, elementary = 0, partition = 0, numV;
          if(version <= 1.0){
            fscanf(fp, "%d %d %d %d %d", &num, &type, &physical, &elementary, &numV);
          }
          else{
            int numTags;
            fscanf(fp, "%d %d %d", &num, &type, &numTags);
            //	  printf("%d %d %d\n", num, type, numTags);
            //	  getchar();
            for(int j = 0; j < numTags; j++){
              int tag;
              fscanf(fp, "%d", &tag);       
              if(j == 0)      physical = tag;
              else if(j == 1) elementary = tag;
              else if(j == 2) partition = tag;
              // ignore any other tags for now
            }
            if (type == 15) numV = 1;
            else if (type == 1) numV = 2;
            else if (type == 2) numV = 3;
            else if (type == 3) numV = 4;
            else if (type == 4) numV = 4;
            else throw;
          }

          int indices[60];
          for(int j = 0; j < numV; j++) fscanf(fp, "%d", &indices[j]);
	
          if ( M_dim(bgMesh) == 3 && type == 4 ){
            pRegion pr = M_region( bgMesh, indices );
            if ( !pr ) printf("didn't find element with nodes %d %d %d %d\n",indices[0],indices[1],indices[2],indices[3]);
            elId[num] = (pEntity)pr;
          }
          else if ( M_dim(bgMesh) == 2 && type == 2 ){
            pFace pf = M_face(  bgMesh, indices );
            elId[num] = (pEntity)pf;
          }
        }
      }
//       else if(!strncmp(&str[1], "NodeData", 8)) {

//         int numStringTags;
//         fscanf(fp,"%d\n",&numStringTags);
//         char dummy[256];
//         for (int i=0;i<numStringTags;i++){
//           fgets(dummy,256,fp);
//         }

//         int numDoubleTags;
//         fscanf(fp,"%d\n",&numDoubleTags);
//         for (int i=0;i<numDoubleTags;i++){
//           fgets(dummy,256,fp);
//         }

//         int numIntTags;
//         fscanf(fp,"%d\n",&numIntTags);
//         int numDataVertices;
//         for (int i=0;i<numIntTags;i++){	
//           if (i == 2)fscanf(fp,"%d",&numDataVertices);
//           else fgets(dummy,256,fp);
//         }

//         if ( M_numVertices(bgMesh) != numDataVertices ) {
//           MAdMsgSgl::instance().error(__LINE__,__FILE__,
//                                       "Data available for %d vertices in file %s while there are %d vertices in the mesh",
//                                       numDataVertices, mshName.c_str(),
//                                       M_numVertices(bgMesh));
//         }

//         for (int i=0; i<numDataVertices; i++) {
//           double dData;
//           int vId;
//           fscanf(fp,"%d %lf",&vId,&dData);
//           setSize(vId,dData);
//         }

//         break;
//       }
      
      else if(!strncmp(&str[1], "ElementData", 11)) {

        int numStringTags;
        fscanf(fp,"%d\n",&numStringTags);
        char dummy[256];
        for (int i=0;i<numStringTags;i++){
          fgets(dummy,256,fp);
        }

        int numDoubleTags;
        fscanf(fp,"%d\n",&numDoubleTags);
        for (int i=0;i<numDoubleTags;i++){
          fgets(dummy,256,fp);
        }

        int numIntTags;
        fscanf(fp,"%d\n",&numIntTags);
        int numDataEl;
        for (int i=0;i<numIntTags;i++){	
          if (i == 2)fscanf(fp,"%d",&numDataEl);
          else fgets(dummy,256,fp);
        }

        int nElem = M_numRegions(bgMesh);
        if ( !nElem ) nElem = M_numFaces(bgMesh);
        if ( nElem != numDataEl ) {
          MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                      "Data available for %d elements in file %s while there are %d elements in the mesh",
                                      numDataEl, mshName.c_str(), nElem );
        }

        for (int i=0; i<nElem; i++) {
          double dData;
          int eId;
          fscanf(fp,"%d %lf",&eId,&dData);
          pEntity pe = elId[eId];
          setSize(pe,dData);
        }

        break;
      }
    
      do {
        if(!fgets(str, sizeof(str), fp) || feof(fp)) {
          MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                      "No data section found in file %s",
                                      mshName.c_str());
        }
      } while(str[0] != '$');
    }
      
    fclose(fp);
    
  }

  // -------------------------------------------------------------------
  void BackgroundSF::redistributeToVertices()
  {
    VIter vIter = M_vertexIter(bgMesh);
    while( pVertex pV = VIter_next(vIter) )
      {
        pPList vRegs = V_regions(pV);
        void * tmp = NULL;
        pRegion pr;
        void * ms;
        double h = 0.;
        int count = 0;
        while ( ( pr = (pRegion)PList_next(vRegs,&tmp) ) ) {
          EN_getDataPtr( (pEntity)pr, pMSizeFieldId, &ms);
          if ( !ms ) {
            MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                        "No size defined for an entity");
          }
          h += ((pMSize)ms)->getMeanLength();
          count++;
        }
        PList_delete(vRegs);
        h /= (double)count;
        setSize((pEntity)pV,h);
      }
    VIter_delete(vIter);
    
  }

  // -------------------------------------------------------------------
}