1 /* ----------------------------------------------------------------------
2    SPARTA - Stochastic PArallel Rarefied-gas Time-accurate Analyzer
3    http://sparta.sandia.gov
4    Steve Plimpton, sjplimp@sandia.gov, Michael Gallis, magalli@sandia.gov
5    Sandia National Laboratories
6 
7    Copyright (2014) Sandia Corporation.  Under the terms of Contract
8    DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
9    certain rights in this software.  This software is distributed under
10    the GNU General Public License.
11 
12    See the README file in the top-level SPARTA directory.
13 ------------------------------------------------------------------------- */
14 
15 #include "string.h"
16 #include "fix_adapt.h"
17 #include "adapt_grid.h"
18 #include "grid.h"
19 #include "surf.h"
20 #include "comm.h"
21 #include "update.h"
22 #include "modify.h"
23 #include "compute.h"
24 #include "input.h"
25 #include "output.h"
26 #include "dump.h"
27 #include "error.h"
28 
29 using namespace SPARTA_NS;
30 
31 enum{NONE,REFINE,COARSEN};              // also in AdaptGrid
32 
33 /* ---------------------------------------------------------------------- */
34 
FixAdapt(SPARTA * sparta,int narg,char ** arg)35 FixAdapt::FixAdapt(SPARTA *sparta, int narg, char **arg) :
36   Fix(sparta, narg, arg)
37 {
38   if (narg < 5) error->all(FLERR,"Illegal fix adapt command");
39 
40   scalar_flag = 1;
41   vector_flag = 1;
42   size_vector = 2;
43   global_freq = 1;
44 
45   // create instance of AdaptGrid class
46 
47   me = comm->me;
48   nprocs = comm->nprocs;
49 
50   adapt = new AdaptGrid(sparta);
51   adapt->mode = 1;
52 
53   // parse and check arguments using AdaptGrid class
54 
55   nevery = input->inumeric(FLERR,arg[2]);
56   if (nevery < 0) error->all(FLERR,"Illegal fix adapt command");
57 
58   adapt->process_args(narg-3,&arg[3]);
59   adapt->check_args(nevery);
60 
61   action1 = adapt->action1;
62   action2 = adapt->action2;
63   file = adapt->file;
64 
65   if (file && strchr(file,'*') == NULL)
66     error->all(FLERR,"Fix adapt filename must contain '*' character");
67 
68   // compute initial outputs
69 
70   last_adapt = 0;
71   nrefine = ncoarsen = 0;
72 }
73 
74 /* ---------------------------------------------------------------------- */
75 
~FixAdapt()76 FixAdapt::~FixAdapt()
77 {
78   delete adapt;
79 }
80 
81 /* ---------------------------------------------------------------------- */
82 
setmask()83 int FixAdapt::setmask()
84 {
85   int mask = 0;
86   mask |= END_OF_STEP;
87   return mask;
88 }
89 
90 /* ---------------------------------------------------------------------- */
91 
init()92 void FixAdapt::init()
93 {
94   // re-check args in case computes or fixes changed
95 
96   adapt->check_args(nevery);
97 
98   // if any fix ave/grid exists, insure it comes before this fix
99   // so that its output values are up-to-date on timesteps adaptation occurs
100 
101   int fixme = modify->find_fix(id);
102   for (int i = 0; i < modify->nfix; i++) {
103     if (strcmp(modify->fix[i]->style,"ave/grid") == 0) {
104       if (i > fixme)
105         error->all(FLERR,"Fix adapt must come after fix ave/grid");
106     }
107   }
108 }
109 
110 /* ----------------------------------------------------------------------
111    perform grid adaptation via AdaptGrid class
112 ------------------------------------------------------------------------- */
113 
end_of_step()114 void FixAdapt::end_of_step()
115 {
116   // wrap adaptivity with clearstep/addstep since it may invoke computes
117 
118   modify->clearstep_compute();
119 
120   // same operations as in AdaptGrid single invocation
121 
122   grid->remove_ghosts();
123 
124   // memory allocation in AdaptGrid class
125 
126   adapt->setup(0);
127 
128   // perform adaptation
129 
130   if (action1 == REFINE || action2 == REFINE) grid->maxlevel++;
131 
132   if (action1 == REFINE) nrefine = adapt->refine();
133   else if (action1 == COARSEN) ncoarsen = adapt->coarsen();
134 
135   if (action2 == REFINE) nrefine = adapt->refine();
136   else if (action2 == COARSEN) ncoarsen = adapt->coarsen();
137 
138   grid->set_maxlevel();
139   grid->rehash();
140 
141   // if no refinement or coarsening, just reghost/reneighbor and return
142 
143   if (nrefine == 0 && ncoarsen == 0) {
144     last_adapt = 0;
145     adapt->cleanup();
146     grid->acquire_ghosts();
147     grid->find_neighbors();
148     return;
149   }
150 
151   // memory deallocation in AdaptGrid class
152 
153   adapt->cleanup();
154 
155   // reset all attributes of adapted grid
156   // same steps as in adapt_grid
157 
158   grid->setup_owned();
159   grid->acquire_ghosts();
160   grid->find_neighbors();
161   grid->check_uniform();
162   comm->reset_neighbors();
163 
164   if (surf->exist) {
165     grid->set_inout();
166     grid->type_check(0);
167   }
168 
169   // notify all classes that store per-grid data that grid may have changed
170 
171   grid->notify_changed();
172 
173   // write out new grid file
174 
175   if (file) adapt->write_file();
176 
177   // wrap adaptivity with clearstep/addstep since it may invoke computes
178 
179   modify->addstep_compute(update->ntimestep + nevery);
180 
181   // outputs
182 
183   last_adapt = 1;
184 }
185 
186 /* ----------------------------------------------------------------------
187    return 0/1 for whether last adaptation changed grid
188 ------------------------------------------------------------------------- */
189 
compute_scalar()190 double FixAdapt::compute_scalar()
191 {
192   return (double) last_adapt;
193 }
194 
195 /* ----------------------------------------------------------------------
196    return stats for last adaptation
197 ------------------------------------------------------------------------- */
198 
compute_vector(int i)199 double FixAdapt::compute_vector(int i)
200 {
201   if (i == 0) return (double) nrefine;
202   return (double) ncoarsen;
203 }
204