Files @ c131f8cd4b54
Branch filter:

Location: EI/VirtualLeaf/src/mesh.h

Roeland Merks
Changed auxin_growth example
  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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
 *
 *  $Id$
 *
 *  This file is part of the Virtual Leaf.
 *
 *  The Virtual Leaf is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  The Virtual Leaf 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with the Virtual Leaf.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Copyright 2010 Roeland Merks.
 *
 */


// Cell derives from Vector, where Vector is simply used as a Vertex

#ifndef _MESH_H_
#define _MESH_H_

#include <vector>
#include <algorithm>
#include <queue>
#include <iterator>
#include <functional>
#ifdef QTGRAPHICS
#include <QGraphicsScene>
#endif
#include "cell.h"
#include "node.h"
#include "simplugin.h"
#include <QVector>
#include <QPair>
#include <QDebug>

using namespace std;
// new queue which rejects duplicate elements
template<class T, class C = deque<T> > class unique_queue : public queue<T,C> {

 public:
 typedef typename C::value_type value_type;
 // reimplements push: reject element if it exists already
 void push(const value_type &x) {
   if (find (queue<T,C>::c.begin(),queue<T,C>::c.end(),x)==queue<T,C>::c.end()) {
     queue<T,C>::c.push_back(x);
   }
 }
 void clear(void) {
   queue<T,C>::c.clear();
 }
};

template<class P> P& deref_ptr ( P *obj) { return *obj; }


class Mesh {

  friend class Cell;
  friend class Node;
  friend class FigureEditor;

 public: 
  Mesh(void) {
    // Make sure the reserved value is large enough if a cell is added
    // in "Divide" when the capacity is insufficient, "cells" might be
    // relocated including the current Cell (i.e. the value of *this)
    // calling "Mesh::IncreaseCapacityIfNecessary" (from another
    // object than Cell, e.g. Mesh) before entering Divide will solve
    // this issue (solved now).
    cells.reserve(2);
    nodes.reserve(500);

    time = 0.;
    plugin = 0;
  };
  ~Mesh(void) {
    delete boundary_polygon;
  };

  void Clean(void);
  Cell &EllipticCell(double xc, double yc, double ra, double rb, int nnodes=10, double rotation=0);
  Cell &CircularCell(double xc, double yc, double r, int nnodes=10) {
    return EllipticCell(xc, yc, r, r, nnodes, 0);
  }
  Cell &LeafPrimordium(int n, double pet_length);
  Cell &LeafPrimordium2(int n);
  Cell *RectangularCell(const Vector ll, const Vector ur, double rotation = 0);
  void CellFiles(const Vector ll, const Vector ur);

  inline Cell &getCell(int i) {
    if ((unsigned)i<cells.size())
      return *cells[i];
    else {
#ifdef QDEBUG
      qDebug() << i << endl;
      qDebug() << "size is " << cells.size() << endl;
#endif
      abort();
    }
  }

  inline Node &getNode(int i) {
    return *nodes[i];    
  }

  //double Diffusion(void);
  inline int size(void) {
    return cells.size();
  }
  inline int nnodes(void) {
    return nodes.size();
  }

  template<class Op> void LoopCells(Op f) {
    for (vector <Cell *>::iterator i=cells.begin();
	 i!=cells.end();
	 i++) {
      f(**i);
    }
  }

  template<class Op> void LoopWalls(Op f) {
    for (list <Wall *>::iterator i=walls.begin();
	 i!=walls.end();
	 i++) {
      f(**i);
    }
  }

  // if the amount of cells might increase, during looping, use this template
  template<class Op> void LoopCurrentCells(Op f) {
    vector<Cell *> current_cells = cells;
    for (vector <Cell *>::iterator i=current_cells.begin();
	 i!=current_cells.end();
	 i++) {
      f(**i);

    }
  }

  template<class Op> void LoopNodes(Op f) {
    for (vector<Node *>::iterator i=nodes.begin();
	 i!=nodes.end();
	 i++) {
      f(**i); 
    }
  }

  template<class Op> void RandomlyLoopNodes(Op f) {

    MyUrand r(shuffled_nodes.size());
    random_shuffle(shuffled_nodes.begin(),shuffled_nodes.end(),r);

    for (vector<Node *>::const_iterator i=shuffled_nodes.begin();
	 i!=shuffled_nodes.end();
	 i++) {
      f(*shuffled_nodes[*i]);
    }
  }

  template<class Op> void RandomlyLoopCells(Op f) {

    MyUrand r(shuffled_cells.size());
    random_shuffle(shuffled_cells.begin(),shuffled_cells.end(),r);

    for (vector<Cell *>::const_iterator i=shuffled_cells.begin();
	 i!=shuffled_cells.end();
	 i++) {
      f(*shuffled_cells[*i]);
    }
  }

  template<class Op1, class Op2> void LoopCells(Op1 f, Op2 &g) {
    for (vector<Cell *>::iterator i=cells.begin();
	 i!=cells.end();
	 i++) {
      f(**i,g); 
    }
  }

  template<class Op1, class Op2, class Op3> void LoopCells(Op1 f, Op2 &g, Op3 &h) {
    for (vector<Cell *>::iterator i=cells.begin();
	 i!=cells.end();
	 i++) {
      f(**i,g,h); 
    }
  }

  void DoCellHouseKeeping(void) {
    vector<Cell *> current_cells = cells;
    for (vector<Cell *>::iterator i = current_cells.begin();
	 i != current_cells.end();
	 i ++) {
      plugin->CellHouseKeeping(*i);

      // Call functions of Cell that cannot be called from CellBase, including Division
      if ((*i)->flag_for_divide) {
	if ((*i)->division_axis) {
	  (*i)->DivideOverAxis(*(*i)->division_axis);
	  delete (*i)->division_axis;
	  (*i)->division_axis = 0;
	} else {
	  (*i)->Divide();
	}
	(*i)->flag_for_divide=false;
      }
    }
  }

  // Apply "f" to cell i
  // i.e. this is an adapter which allows you to call a function
  // operating on Cell on its numeric index index
  template<class Op> void cell_index_adapter(Op f,int i) {
    f(cells[i]);
  }

  double DisplaceNodes(void);

  void BoundingBox(Vector &LowerLeft, Vector &UpperRight);
  int NEqs(void) {     int nwalls = walls.size();
    int ncells =cells.size();
    int nchems = Cell::NChem();

    // two eqs per chemical for each walls, and one eq per chemical for each cell
    // This is for generality. For a specific model you may optimize
    // this by removing superfluous (empty) equations.
    int neqs = 2 * nwalls * nchems + ncells * nchems;

    return neqs;
  }
  void IncreaseCellCapacityIfNecessary(void) {

    return;
    // cerr << "Entering Mesh::IncreaseCellCapacityIfNecessary \n";
    // make sure we always have enough space 
    // to have each cell divide at least once
    //
    // Note that we must do this, because Cell::Divide pushes a new Cell
    // onto Mesh::cells. As a result, Mesh::cells might be relocated 
    // if we are _within_ a Cell object: i.e. pointer "this" will be changed!!
    // 
    // An alternative solution could be to make "Mesh::cells" a list,
    // but this won't work because we need random access for 
    // the Monte Carlo algorithm.

    if (2*cells.size()>cells.capacity()) {
      cerr << "Increasing capacity to "  << 2*cells.capacity() << endl;
      cerr << "Current capacity is " << cells.capacity() << endl;
      cells.reserve(cells.capacity()*2);
    }
  }

  void ReserveMoreCells(int n) {
    if (nodes.size()+n>nodes.capacity()) {
      nodes.reserve(size()+n);
    }
  }
  double Area(void);
  double MeanArea(void) {
    double sum=0.;
    for (vector<Cell *>::const_iterator i=cells.begin();
	 i!=cells.end();
	 i++) {
      sum+=(*i)->Area();
    }
    return sum/(double)NCells();
  }

  void SetBaseArea(void);
  int NCells(void) const {
    return cells.size();
  }
  inline int NNodes(void) const {
    return nodes.size();
  }
  void PrintQueue(ostream &os) {
    while (!node_insertion_queue.empty()) {
      os << node_insertion_queue.front() << endl;
      node_insertion_queue.pop();
    }
  }

  void InsertNodes(void) {
    // insert the nodes in the insertion queue
    while (!node_insertion_queue.empty()) {

      //cerr << node_insertion_queue.front() << endl;
      InsertNode(node_insertion_queue.front());
      node_insertion_queue.pop();
    }

  }

  void Clear(); 

  void ReactDiffuse( double delta_t = 1 );
  double SumChemical(int ch);
  void SetChemical(int ch, double value) {
    for (vector<Cell *>::iterator c=cells.begin();
	 c!=cells.end();
	 c++) {
      (*c)->chem[ch]=value;
    }
  }

  // used for interacing with ODE-solvers (e.g. NRCRungeKutta)
  void setValues(double x, double *y);
  double *getValues(int *neqs);
  void Derivatives(double *derivs);
#ifdef QTGRAPHICS
  inline void DrawBoundary(QGraphicsScene *c) {
    boundary_polygon->Draw(c);
  }
  void DrawNodes(QGraphicsScene *c) const;

#endif
  double max_chem;

  void XMLSave(const char *docname, xmlNode *settings=0) const;
  void XMLRead(const char *docname, xmlNode **settings=0, bool geometry = true, bool pars = true, bool simtime = true);
  void XMLReadPars(const xmlNode * root_node);
  void XMLReadGeometry(const xmlNode *root_node);
  void XMLReadSimtime(const xmlNode *root_node);
  void XMLReadNodes(xmlNode *cur);
  void XMLReadCells(xmlNode *cur);
  void XMLParseTree(const xmlNode * root_node);
  void XMLReadWalls(xmlNode *cur, vector<Wall *> *tmp_cells);
  void XMLReadWallsToCells(xmlNode *root, vector<Wall *> *tmp_walls);
  void XMLReadNodeSets(xmlNode *root);
  void XMLReadNodeSetsToNodes(xmlNode *root);
  void PerturbChem(int chemnum, double range);
  void CleanUpCellNodeLists(void);
  void CleanUpWalls(void);
  void CutAwayBelowLine( Vector startpoint, Vector endpoint );
  void CutAwaySAM(void);
  void RepairBoundaryPolygon(void);
  void Rotate(double angle, Vector center);
  void PrintWallList( void );
  void TestIllegalWalls(void);
  Vector FirstConcMoment(int chem);
  inline Vector Centroid(void) {
    return boundary_polygon->Centroid();
  }

  inline Vector Offset(void) {
    return boundary_polygon->Offset();
  }

  inline double Factor(void) {
    return boundary_polygon->Factor();
  }

  void DeleteLooseWalls(void);
  void FitLeafToCanvas(double width, double height);
  void AddNodeSet(NodeSet *node_set) {
    node_sets.push_back(node_set);
  }

  void CleanChemicals(const vector<double> &clean_chem);
  void CleanTransporters(const vector<double> &clean_transporters);
  void RandomizeChemicals(const vector<double> &max_chem, const vector<double> &max_transporters);
  inline double getTime(void) const { return time; }
  string getTimeHours(void) const; 
  inline void setTime(double t) { time = t; }
  double CalcProtCellsWalls(int ch) const;  
  void SettoInitVals(void);
  QVector<qreal> VertexAngles(void);
  QVector< QPair<qreal,int> > VertexAnglesValues(void);
  void SetSimPlugin(SimPluginInterface *new_plugin) {
    plugin=new_plugin;
  }
  QString ModelID(void) { return plugin?plugin->ModelID():QString("undefined"); }
  void StandardInit(void);	

  Node* findNextBoundaryNode(Node*);

 private:

  // Data members
  vector<Cell *> cells;
  vector<Node *> nodes;
  list<Wall *> walls; // we need to erase elements from this container frequently, hence a list.
 public:
  vector<NodeSet *> node_sets;
 private:
  vector<Node *> shuffled_nodes;
  vector<Cell *> shuffled_cells;
  unique_queue<Edge> node_insertion_queue;
  BoundaryPolygon *boundary_polygon;
  double time;
  SimPluginInterface *plugin;

  // Private member functions
  void AddNodeToCell(Cell *c, Node *n, Node *nb1 , Node *nb2);
  void AddNodeToCellAtIndex(Cell *c, Node *n, Node *nb1 , Node *nb2, list<Node *>::iterator ins_pos);
  void InsertNode(Edge &e);
  inline Node *AddNode(Node *n) {
    nodes.push_back(n);
    shuffled_nodes.push_back(n);
    n->m=this;
    return n;
  }

  inline Cell *AddCell(Cell *c) {
    cells.push_back(c);
    shuffled_cells.push_back(c);
    //cerr << "Shuffled cell indices:  ";
    /*copy(shuffled_cells.begin(),shuffled_cells.end(),ostream_iterator<int>(cerr," "));
      cerr << endl;*/
    c->m=this;
    return c;
  }

  void CircumCircle(double x1,double y1,double x2,double y2,double x3,double y3,
		    double *xc,double *yc,double *r);
};
#endif

/* finis */