File: ConcentrationRegulator.cpp

package info (click to toggle)
lammps 20220106.git7586adbb6a%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348,064 kB
  • sloc: cpp: 831,421; python: 24,896; xml: 14,949; f90: 10,845; ansic: 7,967; sh: 4,226; perl: 4,064; fortran: 2,424; makefile: 1,501; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (635 lines) | stat: -rw-r--r-- 23,055 bytes parent folder | download
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
429
430
431
432
433
434
435
436
437
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#include "ConcentrationRegulator.h"
#include "LammpsInterface.h"
#include "ATC_Coupling.h"
#include "ATC_Error.h"

using ATC_Utility::to_string;
using ATC_Utility::rnd;
using std::map;
using std::string;
using std::pair;
using std::min;
using std::max;

namespace ATC {

const double kMinScale_ = 10000.;

  //========================================================
  //  Class ConcentrationRegulator
  //========================================================

  //--------------------------------------------------------
  //  Constructor
  //--------------------------------------------------------
  ConcentrationRegulator::ConcentrationRegulator(ATC_Coupling * atc) :
    AtomicRegulator(atc)
  {
    // do nothing
  }
  //--------------------------------------------------------
  // Destructor
  //--------------------------------------------------------
  ConcentrationRegulator::~ConcentrationRegulator()
  {
    if (regulators_.size()) {
      map<string,ConcentrationRegulatorMethod *>::iterator it;
      for (it = regulators_.begin(); it != regulators_.end(); ++it) {
        delete it->second;
      }
      regulators_.clear();
    }
    if (parameters_.size()) parameters_.clear();
  }

  //--------------------------------------------------------
  //  modify:
  //    parses and adjusts charge regulator state based on
  //    user input, in the style of LAMMPS user input
  //--------------------------------------------------------
  bool ConcentrationRegulator::modify(int /* narg */, char ** /* arg */)
  {
    bool foundMatch = false;
    return foundMatch;
  }

  //--------------------------------------------------------
  //  construct_methods:
  //--------------------------------------------------------
  void ConcentrationRegulator::construct_methods()
  {
    AtomicRegulator::construct_methods();

    if (atc_->reset_methods()) {
      // eliminate existing methods
      delete_method();
      // consruct new ones
      map<string, ConcentrationRegulatorParameters>::iterator itr;
      for (itr = parameters_.begin();
           itr != parameters_.end(); itr++) {
        string tag = itr->first;
        if (regulators_.find(tag) != regulators_.end()) delete regulators_[tag];
        ConcentrationRegulatorParameters & p = itr->second;
        switch (p.method) {
        case NONE: {
          regulators_[tag] = new ConcentrationRegulatorMethod(this);
          break;
        }
        case TRANSITION: {
          p.type = atc_->tag_to_type(tag);
          p.groupbit = LammpsInterface::instance()->type_to_groupbit(p.type);
          p.transitionType = atc_->tag_to_type(p.transitionTag);
          regulators_[tag] = new ConcentrationRegulatorMethodTransition(this,p);
          break;
        }
        default:
          throw ATC_Error("ConcentrationRegulator::initialize unknown concentration regulator type");
        }
      }
    }
  }
  //--------------------------------------------------------
  //  initialize:
  //--------------------------------------------------------
  void ConcentrationRegulator::initialize()
  {

    map<string, ConcentrationRegulatorMethod *>::iterator itr;
    for (itr = regulators_.begin();
         itr != regulators_.end(); itr++) { itr->second->initialize(); }

    atc_->set_boundary_integration_type(boundaryIntegrationType_);
    AtomicRegulator::reset_nlocal();
    AtomicRegulator::delete_unused_data();
    needReset_ = false;
  }

  //--------------------------------------------------------
  //  pre_exchange
  //--------------------------------------------------------
  void ConcentrationRegulator::pre_exchange()
  {
    map<string, ConcentrationRegulatorMethod *>::iterator itr;
    for (itr = regulators_.begin();
         itr != regulators_.end(); itr++) { itr->second->pre_exchange();}
  }

  //--------------------------------------------------------
  //  pre_force
  //--------------------------------------------------------
  void ConcentrationRegulator::pre_force()
  {
    map<string, ConcentrationRegulatorMethod *>::iterator itr;
    for (itr = regulators_.begin();
         itr != regulators_.end(); itr++) { itr->second->pre_force();}
  }

  //--------------------------------------------------------
  //  finish
  //--------------------------------------------------------
  void ConcentrationRegulator::finish()
  {
    map<string, ConcentrationRegulatorMethod *>::iterator itr;
    for (itr = regulators_.begin();
         itr != regulators_.end(); itr++) { itr->second->finish();}
  }

  //--------------------------------------------------------
  //  output
  //--------------------------------------------------------
  void ConcentrationRegulator::output(OUTPUT_LIST & outputData) const
  {
    map<string, ConcentrationRegulatorMethod *>::const_iterator itr;
    for (itr = regulators_.begin();
         itr != regulators_.end(); itr++) { itr->second->output(outputData);}
  }

  //--------------------------------------------------------
  //  compute vector
  //--------------------------------------------------------
  double ConcentrationRegulator::compute_vector(int n) const
  {
    int s = regulators_.size();
    if (s == 0) return 0;
    int m = n / s;
    n     = n % s;
    int c = 0;

    map<string, ConcentrationRegulatorMethod *>::const_iterator itr;
    for (itr = regulators_.begin();
         itr != regulators_.end(); itr++) {
      if (c++ == n) { return itr->second->compute_vector(m); }
    }
    return 0.;

  }
  //--------------------------------------------------------
  //  size vector
  //--------------------------------------------------------
  int ConcentrationRegulator::size_vector(int /* i */) const
  {
    int n = (regulators_.size())*5;
    if (n==0) n = 20;
    return n;
  }

  //========================================================
  //  Class ConcentrationRegulatorMethodTransition
  //========================================================

  //--------------------------------------------------------
  //  Constructor
  //         Grab references to ATC and ConcentrationRegulator
  //--------------------------------------------------------
  ConcentrationRegulatorMethodTransition::ConcentrationRegulatorMethodTransition
    (ConcentrationRegulator *concReg,
     ConcentrationRegulator::ConcentrationRegulatorParameters & p)
      : ConcentrationRegulatorMethod(concReg),
        concentrationRegulator_(concReg),
        interscaleManager_(nullptr),
        lammpsInterface_(LammpsInterface::instance()),
        list_(nullptr),
        targetConcentration_(p.value),
        targetCount_(0),
        elemset_(p.elemset),
        p_(nullptr),
        randomNumberGenerator_(nullptr),
        q0_(0),
        controlType_(p.type),
        controlIndex_(0),
        transitionType_(p.transitionType),
        transitionInterval_(p.transitionInterval),
        transitionCounter_(0),
        nInTransition_(0),
        transitionFactor_(0),
        controlMask_(p.groupbit),
        frequency_(p.frequency),
        maxEnergy_(p.maxEnergy),
        maxExchanges_(p.maxExchanges),
        maxAttempts_(p.maxAttempts),
        nexchanges_(0),
        initialized_(false),
        _rngUniformCounter_(0),
        _rngNormalCounter_(0)
  {
    controlIndex_ = atc_->type_index(controlType_);
    LammpsInterface * li = LammpsInterface::instance();
    q0_ = li->type_to_charge(controlType_);
    double kB = (li->boltz())/(li->mvv2e()); // E/T*m*v^2/E = m v^2/T
    double m = li->atom_mass(controlType_);
    sigma_ = sqrt(kB/m); // v / sqrt(T)
    randomNumberGenerator_ = li->random_number_generator();
  }

  //--------------------------------------------------------
  //  Initialize
  //--------------------------------------------------------
  void ConcentrationRegulatorMethodTransition::initialize(void)
  {
#ifdef ATC_VERBOSE
    lammpsInterface_->print_msg_once(
      "\ncontrol type: "+to_string(controlType_)+
      "\ntransistion type:"+to_string(transitionType_)+
      "\ncontrol mask:"+to_string(controlMask_)+
      "\nfrequency:"+to_string(frequency_)+
      "\nmax exchanges:"+to_string(maxExchanges_)+
      "\nmax attempts:"+to_string(maxAttempts_)+
      "\nmax energy:"+to_string(maxEnergy_)
    );
#endif
    interscaleManager_ = &(atc_->interscale_manager());

    PerAtomQuantity<int> * a2el = atc_->atom_to_element_map();
    list_ = new AtomInElementSet(atc_,a2el,elemset_,controlType_);

    nNodes_ = atc_->num_nodes();
    DENS_MAT conc(nNodes_,1); conc = targetConcentration_;
    DENS_VEC integral = atc_->fe_engine()->integrate(conc,elemset_);
    targetCount_ = rnd(integral(0)) ;

    volumes_.resize(elemset_.size());
    ESET::const_iterator itr;
    int i = 0;
    DENS_MAT c(nNodes_,1); c = 1;
    V_ = 0.;
    for (itr = elemset_.begin(); itr != elemset_.end(); itr++, i++) {
      ESET e; e.insert(*itr);
      DENS_VEC v = atc_->fe_engine()->integrate(c,e);
      volumes_(i) = v(0);
      V_ += v(0);
    }
    volumes_ *= 1./V_;
    for (int i = 1; i < volumes_.size(); i++) {
      volumes_(i) += volumes_(i-1);
    }

    // record original energetic properties
    int ntypes = lammpsInterface_->ntypes();
    epsilon0_.reset(ntypes);
    p_ = lammpsInterface_->potential();
    lammpsInterface_->epsilons(controlType_,p_,epsilon0_.ptr());

#ifdef ATC_VERBOSE
    string msg = "type "+to_string(controlType_)+" target count " + to_string(targetCount_);
    msg += " volume " + to_string(V_);
    msg += " current count " + to_string(count());
    ATC::LammpsInterface::instance()->print_msg_once(msg);
    msg = "WARNING: ensure neighboring happens at least every "+to_string(frequency_);
    ATC::LammpsInterface::instance()->print_msg_once(msg);
#endif
  }
  double ConcentrationRegulatorMethodTransition::uniform() const {
    _rngUniformCounter_++;
    return lammpsInterface_->random_uniform(randomNumberGenerator_);
  }
  double ConcentrationRegulatorMethodTransition::normal() const {
    _rngNormalCounter_++;
    return lammpsInterface_->random_normal(randomNumberGenerator_);
  }
  //--------------------------------------------------------
  //  pre exchange
  //--------------------------------------------------------
  void ConcentrationRegulatorMethodTransition::pre_exchange(void)
  {
    // return if should not be called on this timestep
    if ( ! lammpsInterface_->now(frequency_)) return;
    nexchanges_ = excess();
    int  n = abs(nexchanges_);
    bool success = false;
    if      (nexchanges_ > 0) { success = delete_atoms(n); }
    else if (nexchanges_ < 0) { success = insert_atoms(n); }
    else return;
    if (!success) throw ATC_Error("insertions/deletions did not succeed");

    if (nexchanges_ !=0)  {
      nInTransition_ = -nexchanges_;
      lammpsInterface_->reset_ghosts(-nexchanges_);
      atc_->reset_atoms();
    }
    transitionCounter_=0;
    transition();
  }
  //--------------------------------------------------------
  //  pre force
  //--------------------------------------------------------
  void ConcentrationRegulatorMethodTransition::pre_force(void)
  {
    transition();
  }
  //--------------------------------------------------------
  //  accept
  //--------------------------------------------------------
  bool ConcentrationRegulatorMethodTransition::accept(double energy, double /* T */) const
  {
#ifdef ATC_VERBOSE2
    if (energy < maxEnergy_) lammpsInterface_->print_msg(" energy "+to_string(energy)+" "+to_string(rngCounter_));
#endif
    return (energy < maxEnergy_);
  }
  //--------------------------------------------------------
  //  energy
  //--------------------------------------------------------
  double ConcentrationRegulatorMethodTransition::energy(int id) const
  {
    double e = lammpsInterface_->shortrange_energy(id,maxEnergy_);
#ifdef ATC_VERBOSE
{
    int * tag = lammpsInterface_->atom_tag();
    lammpsInterface_->print_msg(to_string(controlType_)+" deletion energy "+to_string(e)+" id "+to_string(tag[id])+" "+to_string(_rngUniformCounter_)+":"+to_string(_rngNormalCounter_));
}
#endif
    return e;
  }
  double ConcentrationRegulatorMethodTransition::energy(double * x) const
  {
    double e = lammpsInterface_->shortrange_energy(x,controlType_,maxEnergy_);
#ifdef ATC_VERBOSE
{
    lammpsInterface_->print_msg(to_string(controlType_)+" insertion energy "+to_string(e)+" x "+to_string(x[0])+","+to_string(x[1])+","+to_string(x[2])+" "+to_string(_rngUniformCounter_)+":"+to_string(_rngNormalCounter_));
}
#endif
    return e;
  }
  //--------------------------------------------------------
  //  excess
  //--------------------------------------------------------
  int ConcentrationRegulatorMethodTransition::excess(void) const
  {
     int nexcess = count()-targetCount_;
     nexcess = max(min(nexcess,maxExchanges_),-maxExchanges_);
     return nexcess;
  }
  //--------------------------------------------------------
  //  count
  //--------------------------------------------------------
  int ConcentrationRegulatorMethodTransition::count(void) const
  {
    // integrate concentration over region
    const DENS_MAT & c = (atc_->field(SPECIES_CONCENTRATION)).quantity();
    DENS_VEC integral = atc_->fe_engine()->integrate(c,elemset_);
    return rnd(integral(controlIndex_)) ;
  }
  //--------------------------------------------------------
  //  delete atoms
  //--------------------------------------------------------
  bool ConcentrationRegulatorMethodTransition::delete_atoms(int n)
  {
    ID_PAIR idPair;

    deletionIds_.clear();
    int deletions = 0;
    int attempts = 0;
    while(deletions < n && attempts < maxAttempts_){
      if(accept(deletion_id(idPair))) {
        deletionIds_.push_back(idPair);
        deletions += 1;
      }
      deletions = lammpsInterface_->int_allmax(deletions);
      attempts++;
    }
    ID_LIST::iterator itr;
    for (itr = deletionIds_.begin(); itr != deletionIds_.end(); itr++) {
      lammpsInterface_->delete_atom(itr->second);
    }
#ifdef ATC_VERBOSE
      string c = to_string(controlType_);
      lammpsInterface_->all_print(attempts, c+"-attempts  ");
      lammpsInterface_->all_print(deletions,c+" deletions ");
      lammpsInterface_->all_print(_rngUniformCounter_,c+" RNG-uniform  ");
      lammpsInterface_->all_print(_rngNormalCounter_,c+" RNG-normal  ");
//    lammpsInterface_->all_print(uniform()," RANDOM ");
#endif
    return (n == deletions); // success
  }
  //--------------------------------------------------------
  //  pick id
  //--------------------------------------------------------
  double ConcentrationRegulatorMethodTransition::deletion_id(ID_PAIR & id) const
  {
    if (atc_->parallel_consistency()) return deletion_id_consistent(id);
    else                              return deletion_id_free(id);
  }
  double ConcentrationRegulatorMethodTransition::deletion_id_consistent(ID_PAIR & id) const
  {
    id.first  = -1;
    id.second = -1;
    int ntotal = lammpsInterface_->natoms();
    double r = uniform();
    r *= ntotal;
    const ID_LIST & list = list_->quantity();
    ID_LIST::const_iterator itr;
    int i=0, idx = -1;
    double min = ntotal;
    int * tag = lammpsInterface_->atom_tag();
    for (itr = list.begin(); itr != list.end(); itr++) {
      int atag = tag[itr->second];
      double d = fabs(atag-r);
      if (d < min) {
        min = d;
        idx = i;
      }
      i++;
    }
    int imin = kMinScale_*min;
    if(imin == lammpsInterface_->int_allmin(imin)) {
      if (idx < 0) throw ATC_Error("deletion_id failed to find a suitable atom");
      id = list_->item(idx);
      // avoid repeats
      ID_LIST & l = list_->set_quantity();
      l.erase(l.begin()+idx);
      return energy(id.second);
    }
    else {
      return maxEnergy_;
    }
  }
  double ConcentrationRegulatorMethodTransition::deletion_id_free(ID_PAIR & id) const
  {
    id.first  = -1;
    id.second = -1;
    int n = list_->size();
    double nrank = lammpsInterface_->int_scansum(n);
    int   ntotal = lammpsInterface_->int_allsum(n);
    if (ntotal == 0) throw ATC_Error("control type "+to_string(controlType_)+" is depleted");
    double r = uniform();
    r *= ntotal;
    if ( (r >= nrank-n) && (r < nrank)) { // pick processor

      r = uniform();
      int idx = rnd(r*(n-1));
      id = list_->item(idx);
      // avoid repeats
      ID_LIST & l = list_->set_quantity();
      l.erase(l.begin()+idx);
      return energy(id.second);
    }
    else {
      return maxEnergy_;
    }
  }
  //--------------------------------------------------------
  //  insert atoms
  //--------------------------------------------------------
  bool ConcentrationRegulatorMethodTransition::insert_atoms(int n)
  {

    insertionIds_.clear();
    DENS_VEC x(3); x = 0;
    DENS_VEC v(3); v = 0;
    const DENS_MAN & T = atc_->field(TEMPERATURE);
    int additions = 0;
    int attempts = 0;
    while(additions < n && attempts < maxAttempts_){
      if(accept(insertion_location(x))) {
        DENS_VEC Tv = atc_->fe_engine()->interpolate_field(x,T);
Tv(0) = 300.;
        pick_velocity(v,Tv(0)); // 3 normal
        int nlocal = lammpsInterface_->insert_atom(transitionType_,controlMask_,x.ptr(),v.ptr()); // no charge
        insertionIds_.push_back(pair<int,int>(-1,nlocal)); // atc id unknown
        additions += 1;
#ifdef ATC_VERBOSE2
        lammpsInterface_->print_msg(">>> insert x:"+to_string(x(0))+" "+to_string(x(1))+" "+to_string(x(2))+" v:"+to_string(v(0))+" "+to_string(v(1))+" "+to_string(v(2))+" "+to_string(rngCounter_));
#endif
      }
      attempts++;
      //lammpsInterface_->barrier();
      additions = lammpsInterface_->int_allmax(additions);
#ifdef ATC_VERBOSE
{
      string c = to_string(controlType_);
      lammpsInterface_->all_print(_rngUniformCounter_,c+" rng-uniform  ");
      lammpsInterface_->all_print(_rngNormalCounter_,c+" rng-normal  ");
//    lammpsInterface_->all_print(uniform()," random ");
}
#endif
      if (atc_->parallel_consistency()) { sync_random_number_generators(); }
#ifdef ATC_VERBOSE2
        lammpsInterface_->print_msg("attempts: "+to_string(attempts)+" additions "+to_string(additions)+" : "+to_string(rngCounter_));
#endif
#ifdef ATC_VERBOSE
{
      string c = to_string(controlType_);
      lammpsInterface_->all_print(attempts, c+"+attempts  ");
      lammpsInterface_->all_print(additions,c+" additions ");
      lammpsInterface_->all_print(_rngUniformCounter_,c+" RNG-uniform  ");
      lammpsInterface_->all_print(_rngNormalCounter_,c+" RNG-normal  ");
//    lammpsInterface_->all_print(uniform()," RANDOM ");
}
#endif
    }
    return (n == additions); // success
  }
  //--------------------------------------------------------
  //  sync random number generators
  //--------------------------------------------------------
  void ConcentrationRegulatorMethodTransition::sync_random_number_generators() const
  {
    // normal
    int n = lammpsInterface_->int_allmax(_rngNormalCounter_);
    int dn = n - _rngNormalCounter_;
    lammpsInterface_->advance_random_normal(randomNumberGenerator_,dn);
    _rngNormalCounter_ = n;
    // uniform
    int u = lammpsInterface_->int_allmax(_rngUniformCounter_);
    int du = u - _rngUniformCounter_;
    lammpsInterface_->advance_random_uniform(randomNumberGenerator_,du);
    _rngUniformCounter_ = u;
  }
  //--------------------------------------------------------
  //  pick location
  //--------------------------------------------------------
  double ConcentrationRegulatorMethodTransition::insertion_location(DENS_VEC & x) const
  {
     // pick random element
     int elem = pick_element(); // 1 uniform
     // pick random local coordinate
     DENS_VEC xi(3);
     pick_coordinates(elem,xi,x); // 3 uniform
//   if (! lammpsInterface_->in_box(x.ptr())) { throw ATC_Error("new atom is not in box");}
     if (lammpsInterface_->in_my_processor_box(x.ptr())) {
#ifdef ATC_VERBOSE2
       lammpsInterface_->print_msg(">>> insertion_location e:" +to_string(elem)+" xi:"+to_string(xi(0))+" "+to_string(xi(1))+" "+to_string(xi(2))+" x:"+to_string(x(0))+" "+to_string(x(1))+" "+to_string(x(2))+ " energy "+to_string(energy(x.ptr()))+" "+true_false(accept(energy(x.ptr())))+" "+to_string(rngUniformCounter_));
#endif
       return energy(x.ptr());
     }
     else {
       return maxEnergy_;
     }
  }
  //--------------------------------------------------------
  //  pick element
  //--------------------------------------------------------
  int ConcentrationRegulatorMethodTransition::pick_element() const
  {
    double r = uniform();
    ESET::const_iterator itr = elemset_.begin(); // global?
    for (int i = 0; i < volumes_.size() ; ++i) {
      if (r < volumes_(i)) return *itr;
      itr++;
    }
    return *itr;
  }
  //--------------------------------------------------------
  //  pick coordinates
  //--------------------------------------------------------
  void ConcentrationRegulatorMethodTransition::pick_coordinates(const int elem,
                                                     DENS_VEC & xi,
                                                     DENS_VEC & x) const
  {
    xi.reset(3);
    xi(0) = 2.*uniform()-1.;
    xi(1) = 2.*uniform()-1.;
    xi(2) = 2.*uniform()-1.;
    atc_->fe_engine()->fe_mesh()->position(elem,xi,x);

  }
  //--------------------------------------------------------
  //  pick velocity
  //--------------------------------------------------------
  void ConcentrationRegulatorMethodTransition::pick_velocity(DENS_VEC & v,double T) const
  {
    double s = sigma_*sqrt(T);
    v(0) = s*normal();
    v(1) = s*normal();
    v(2) = s*normal();
//v = 0;
  }
  //--------------------------------------------------------
  //  transition
  //--------------------------------------------------------
  void ConcentrationRegulatorMethodTransition::transition()
  {
    transitionCounter_++;
    //if (insertionIds_.size() == 0) return; //
    if      (transitionCounter_> transitionInterval_) {
      nInTransition_ = 0;
      return;
    }
    else if (transitionCounter_==transitionInterval_) {
      nInTransition_ -= lammpsInterface_->change_type(transitionType_,controlType_);
    }
    else {
      transitionFactor_ = insertion_factor(transitionCounter_);
      if (nInTransition_ < 0) transitionFactor_ = 1-transitionFactor_;
      double q = 0;
      lammpsInterface_->set_charge(transitionType_,q);
      DENS_VEC eps = epsilon0_;

      lammpsInterface_->set_epsilons(transitionType_,p_,eps.ptr());
      lammpsInterface_->pair_reinit(); // epsilon
    }
  }
  //--------------------------------------------------------
  //  diagnostics
  //--------------------------------------------------------
  double ConcentrationRegulatorMethodTransition::compute_vector(int n) const
  {
    if      (n==0) return count() - targetCount_;
    else if (n==1) return count()/V_;
    else if (n==2) return (1.-transitionFactor_)*nInTransition_;
    else if (n==3) return _rngUniformCounter_;
    else if (n==4) return _rngNormalCounter_;
    else if (n==5) return lammpsInterface_->random_state(randomNumberGenerator_);
    else return 0;
  }
}; // end namespace