aboutsummaryrefslogtreecommitdiff
path: root/src/punc_track.c
blob: 69cb754b3182cc1bea372f66dae41edae4d5abdc (plain)
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
/* $Header$ */
 
#include <assert.h>
#include <stdio.h>

#include "cctk.h"
#include "cctk_Arguments.h"
#include "cctk_Parameters.h"
#include "util_Table.h"
#include <math.h>
#define REFLEVEL round(log10((CCTK_REAL)(cctkGH->cctk_levfac[0]))/log10(2.0))

#define ABS(x) ((x)>=0?x:-x)

double pt_loc_old_x[10];
double pt_loc_old_y[10];
double pt_loc_old_z[10];

void PunctureTracker (CCTK_ARGUMENTS);
void PunctureTracker_init (CCTK_ARGUMENTS);


void PunctureTracker_init (CCTK_ARGUMENTS)
{
  DECLARE_CCTK_PARAMETERS;
  DECLARE_CCTK_ARGUMENTS;
  int i;

  if(pt_verbose>0)
    CCTK_INFO("Initializing PunctureTracker");

  if((pt_initial_x[0]!=0.0)||(pt_initial_y[0]!=0.0)||(pt_initial_z[0]!=0.0))
    for (i=0;i<pt_num_tracked;i++){
      pt_loc_x_p[i] = pt_initial_x[i];
      pt_loc_y_p[i] = pt_initial_y[i];
      pt_loc_z_p[i] = pt_initial_z[i];
    }
  else{
    pt_loc_x_p[0] = par_b;
    pt_loc_x_p[1] =-par_b;
    pt_loc_y_p[0] = 0.0;
    pt_loc_y_p[1] = 0.0;
    pt_loc_z_p[0] = 0.0;
    pt_loc_z_p[1] = 0.0;
  }
  for (i=0; i< pt_num_tracked; i++) {
    pt_loc_old_x[i] = pt_loc_x_p[i];
    pt_loc_old_y[i] = pt_loc_y_p[i];
    pt_loc_old_z[i] = pt_loc_z_p[i];
  
    sf_centroid_x[pt_which_surface_to_take[i]]=pt_loc_old_x[i];
    sf_centroid_y[pt_which_surface_to_take[i]]=pt_loc_old_y[i];      
    sf_centroid_z[pt_which_surface_to_take[i]]=pt_loc_old_z[i];      

    if(pt_verbose>0){
      printf("set puncture %d to location=(%g,%g,%g)\n",i,pt_loc_old_x[i],pt_loc_old_y[i],pt_loc_old_z[i]);
    }
  }
      CCTK_INFO("done initializing PunctureTracker");
}


void PunctureTracker (CCTK_ARGUMENTS)
{
  DECLARE_CCTK_PARAMETERS;
  DECLARE_CCTK_ARGUMENTS;
  
  int n,ierror,i,numpoints;
  // portland doesnt like that
  //CCTK_REAL  pt_betax[pt_num_tracked], pt_betay[pt_num_tracked], pt_betaz[pt_num_tracked];
  //so I do that

  CCTK_REAL pt_betax[10];
  CCTK_REAL pt_betay[10];
  CCTK_REAL pt_betaz[10];

  int         operator_handle, param_table_handle, coordsys_handle;
  int         input_array_indices[3];
  const void *interp_coords[3];
  void       *output_arrays[3];

  const CCTK_INT input_array_type_codes[3] = { CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL};   
  const CCTK_INT output_array_type_codes[3]= { CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL, CCTK_VARIABLE_REAL};
   
  if(pt_verbose >0)
    CCTK_INFO("tracking Punctures");
  
  interp_coords[0] = (const void *) pt_loc_x_p;
  interp_coords[1] = (const void *) pt_loc_y_p;
  interp_coords[2] = (const void *) pt_loc_z_p;
  
  operator_handle     = -1;
  coordsys_handle     = -1;
  param_table_handle  = -1;
  
  // set interpolation handles
  operator_handle = CCTK_InterpHandle("Lagrange polynomial interpolation");
  if (operator_handle < 0) CCTK_WARN(0, "can’t get interpolation handle!");

  param_table_handle = Util_TableCreateFromString("order=4");
  if (param_table_handle < 0) CCTK_WARN(0, "can't ge parameter table!");
  
  coordsys_handle = CCTK_CoordSystemHandle("cart3d");
  if (coordsys_handle < 0) CCTK_WARN(0,"can't get coordsys handle!");

  output_arrays[0] = (void *) pt_betax;
  output_arrays[1] = (void *) pt_betay;
  output_arrays[2] = (void *) pt_betaz;

  input_array_indices[0] = CCTK_VarIndex ("ADMBase::betax");
  input_array_indices[1] = CCTK_VarIndex ("ADMBase::betay");
  input_array_indices[2] = CCTK_VarIndex ("ADMBase::betaz");

  //only proc 0 does interpolation
  numpoints = 0;
  if (CCTK_MyProc(cctkGH) == 0)
    numpoints = pt_num_tracked;

  ierror = CCTK_InterpGridArrays(cctkGH,3,
                                 operator_handle, param_table_handle,coordsys_handle,
                                 numpoints,
                                 CCTK_VARIABLE_REAL,
                                 interp_coords,
                                 3,input_array_indices,
                                 3,output_array_type_codes,
                                  output_arrays);
  if (ierror<0)
    CCTK_VWarn(CCTK_WARN_ABORT, __LINE__, __FILE__, CCTK_THORNSTRING,
               "error return from interpolator ierror=%d",(int)ierror);
  

  for (n=0; n< pt_num_tracked; n++) {
    if(pt_verbose>4)
      printf("at location of puncture %i is \
              (beta,betay,betaz) = (%f,%f,%f)\n",\
             n,pt_betax[n],pt_betay[n],pt_betaz[n]);
    pt_shiftx[n] = pt_betax[n];
    pt_shifty[n] = pt_betay[n];
    pt_shiftz[n] = pt_betaz[n];
  }

}

void update_punc_loc(CCTK_ARGUMENTS){

  DECLARE_CCTK_PARAMETERS;
  DECLARE_CCTK_ARGUMENTS;

  CCTK_REAL pt_dt,pt_x_diff,pt_y_diff,pt_z_diff;
  int n,ierr;
  int regrid_now = 0;
  char parname[200];
  char value[200];
  
  pt_dt = CCTK_DELTA_TIME;

  for (n=0; n< pt_num_tracked; n++) {
    
    //correct small errors
    if (pt_shiftx[n] < 10e-14) pt_shiftx[n] = 0.0;
    if (pt_shifty[n] < 10e-14) pt_shifty[n] = 0.0;
    if (pt_shiftz[n] < 10e-14) pt_shiftz[n] = 0.0;    
  
    //update
    pt_loc_x[n] = pt_loc_x_p[n] - pt_shiftx[n] * pt_dt;
    pt_loc_y[n] = pt_loc_y_p[n] - pt_shifty[n] * pt_dt;
    pt_loc_z[n] = pt_loc_z_p[n] - pt_shiftz[n] * pt_dt;

    //info
    if(pt_verbose>0) CCTK_VInfo(CCTK_THORNSTRING,
      "Puncture %d is at (x,y,z)=(%f,%f,%f)",n,pt_loc_x[n],pt_loc_y[n],pt_loc_z[n]);
    if(pt_verbose > 2){
      printf("     pt_dt      is %f\n",pt_dt);
      printf("     pt_loc_x_p is %f\n",pt_loc_x_p[n]);
      printf("     pt_shiftx  is %f\n",pt_shiftx[n]);
      fflush(stdout);
    }
  

    //have we moved enough to regrid now?
    printf("regrid_now is %i",regrid_now);
    if((pt_regrid)&&(cctkGH->cctk_iteration>1)){
      if(!regrid_now){
        pt_x_diff = ABS(pt_loc_old_x[n] - pt_loc_x[n]);
        pt_y_diff = ABS(pt_loc_old_y[n] - pt_loc_y[n]);
        pt_z_diff = ABS(pt_loc_old_z[n] - pt_loc_z[n]);

        if(pt_verbose > 2){
          printf("     pt_x_diff      is %f\n",pt_x_diff);
          printf("     pt_y_diff      is %f\n",pt_y_diff);
          printf("     pt_z_diff      is %f\n",pt_z_diff);
          printf("     pt_reg_buf : %f\n",pt_regrid_buffer);
        }
        
        if((pt_x_diff > pt_regrid_buffer)||
           (pt_y_diff > pt_regrid_buffer)||
           (pt_z_diff > pt_regrid_buffer)){
          regrid_now = 1;
        }
      }
    }
  }
  
  if(regrid_now){
    regrid_now = 0;

    if(pt_verbose>0) CCTK_INFO("regridding now");
    for (n=0; n< pt_num_tracked; n++) {
      ierr = CCTK_ParameterSet ("regrid_every","CarpetReGrid2","1");
      assert (ierr>=0);

      ierr = sprintf (parname, "position_x_%d", n+1);
      assert (ierr>=0);
      ierr = sprintf (value, "%f", pt_loc_x[n]);
      assert (ierr>=0);
      ierr = CCTK_ParameterSet(parname,"CarpetRegrid2",value);

      //info
      if(pt_verbose>3) printf("%c was set to %c",parname,value);

      ierr = sprintf (parname, "position_y_%d", n+1);
      assert (ierr>=0 && ierr<sizeof parname);
      ierr = sprintf (value, "%f", pt_loc_y[n]);
      assert (ierr>=0 && ierr<sizeof value);
      ierr = CCTK_ParameterSet(parname,"CarpetRegrid2",value);

      //info
      if(pt_verbose>3) printf("%c was set to %c",parname,value);

      ierr = sprintf (parname, "position_z_%d", n+1);
      assert (ierr>=0 && ierr<sizeof parname);
      ierr = sprintf (value, "%f", pt_loc_z[n]);
      assert (ierr>=0 && ierr<sizeof value);
      ierr = CCTK_ParameterSet(parname,"CarpetRegrid2",value);

      //info
      if(pt_verbose>3) printf("%c was set to %c",parname,value);        

      //remember this location
      pt_loc_old_x[n] = pt_loc_x[n];
      pt_loc_old_y[n] = pt_loc_y[n];
      pt_loc_old_z[n] = pt_loc_z[n];
    }
  }
  else{
    ierr = CCTK_ParameterSet ("regrid_every","CarpetReGrid2","-1");
    if(pt_verbose>0) CCTK_INFO("not regridding now");
    regrid_now = 0;           //just to make sure
  }

  if(pt_verbose>0)
    CCTK_INFO ("done");
}