| 1 | //extension_velocity_marcher.cpp |
| 2 | |
| 3 | #include "extension_velocity_marcher.h" |
| 4 | |
| 5 | #include <stdexcept> |
| 6 | #include <iostream> |
| 7 | |
| 8 | #include "math.h" |
| 9 | |
| 10 | void extensionVelocityMarcher::initalizeFrozen() |
| 11 | { |
| 12 | //loop over phi to find zero values |
| 13 | // and mark them as frozen |
| 14 | for (int i=0; i<size_; i++) |
| 15 | { |
| 16 | if (flag_[i] != Mask && phi_[i]==0.0) |
| 17 | { |
| 18 | flag_[i]=Frozen; |
| 19 | distance_[i]=0.0; |
| 20 | f_ext_[i]=speed_[i]; |
| 21 | } |
| 22 | } |
| 23 | //loop over all of phi and for each point check each direction |
| 24 | // to see if we cross the zero level set |
| 25 | // if so calculate the minimum distance to the zero level set |
| 26 | // mark as frozen. |
| 27 | for (int i=0; i<size_; i++) |
| 28 | if (flag_[i] == Far) |
| 29 | { |
| 30 | double ldistance[MaximumDimension]; |
| 31 | double lspeed[MaximumDimension]; |
| 32 | bool borders=false; |
| 33 | for (int dim=0; dim<dim_; dim++) |
| 34 | { |
| 35 | ldistance[dim]=0; |
| 36 | lspeed[dim]=0; |
| 37 | for (int j=-1; j<2; j+=2) // each direction |
| 38 | { |
| 39 | int naddr = _getN(i,dim,j,Mask); |
| 40 | if (naddr!=-1 && phi_[i] * phi_[naddr]<0) |
| 41 | { |
| 42 | // this cell and neighbor span the zero level set. |
| 43 | borders=true; |
| 44 | //calculate the distance to the zero level set. |
| 45 | double d = dx_[dim]*phi_[i]/(phi_[i]-phi_[naddr]); |
| 46 | if (ldistance[dim]==0 || ldistance[dim]>d) |
| 47 | { |
| 48 | ldistance[dim] = d; |
| 49 | if (ext_mask_[i]) |
| 50 | lspeed[dim] = speed_[naddr]; |
| 51 | else if (ext_mask_[naddr]) |
| 52 | lspeed[dim] = speed_[i]; |
| 53 | else |
| 54 | lspeed[dim] = speed_[i] + d / dx_[dim] * (speed_[naddr] - speed_[i]); |
| 55 | } |
| 56 | } |
| 57 | } // for each direction |
| 58 | } // for each dimension |
| 59 | if (borders) |
| 60 | { |
| 61 | double numerator = 0.0; |
| 62 | double denominator = 0.0; |
| 63 | for (int dim=0; dim<dim_; dim++) |
| 64 | { |
| 65 | if (ldistance[dim] != 0.0) |
| 66 | { |
| 67 | numerator += lspeed[dim]/pow(ldistance[dim],2); |
| 68 | denominator += 1/pow(ldistance[dim],2); |
| 69 | } |
| 70 | } |
| 71 | if (denominator != 0.0) |
| 72 | { |
| 73 | f_ext_[i] = numerator/denominator; |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | throw std::runtime_error( |
| 78 | "div by zero (flag=2) in scikit-fmm extension marcher"); |
| 79 | } |
| 80 | |
| 81 | double dsum = 0; |
| 82 | for (int dim=0; dim<dim_; dim++) |
| 83 | if (ldistance[dim]>0) dsum += 1/ldistance[dim]/ldistance[dim]; |
| 84 | if (phi_[i]<0) |
| 85 | distance_[i] = -sqrt(1/dsum); |
| 86 | else distance_[i] = sqrt(1/dsum); |
| 87 | flag_[i]=Frozen; |
| 88 | } |
| 89 | }// for each point in the far field |
| 90 | } |
| 91 | |
| 92 | void extensionVelocityMarcher::finalizePoint(int i, double phi_i) |
| 93 | { |
| 94 | // set the extension velocity of this point |
| 95 | // find f_ext where grad f_ext . grad phi = 0 |
| 96 | // as described in Adalsteinsson and Sethian |
| 97 | |
| 98 | // technically we do not need to calculate this extension velocity |
| 99 | // until the point is frozen. |
| 100 | |
| 101 | double ldistance[MaximumDimension]; |
| 102 | double lspeed[MaximumDimension]; |
| 103 | |
| 104 | double numerator = 0.0; |
| 105 | double denominator = 0.0; |
| 106 | |
| 107 | for (int dim=0; dim<dim_; dim++) |
| 108 | { |
| 109 | lspeed[dim]=0; |
| 110 | ldistance[dim]=0; |
| 111 | for (int j=-1; j<2; j+=2) // each direction |
| 112 | { |
| 113 | int naddr = _getN(i,dim,j,Mask); |
| 114 | if (naddr!=-1 && flag_[naddr]==Frozen) |
| 115 | { |
| 116 | //determine which direction, in this dimension, is nearest to |
| 117 | //the front. Calculate the distance to front in this direction |
| 118 | double d = distance_[i] - distance_[naddr]; |
| 119 | if (ldistance[dim]==0 || ldistance[dim]>d) |
| 120 | { |
| 121 | // this condition avoids a rare div by zero case where d = 0 |
| 122 | if (fabs(d) > std::numeric_limits<double>::epsilon()) |
| 123 | { |
| 124 | ldistance[dim] = d; |
| 125 | lspeed[dim] = f_ext_[naddr]; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } // for each direction |
| 130 | } // for each dimension |
| 131 | |
| 132 | for (int dim=0; dim<dim_; dim++) |
| 133 | { |
| 134 | numerator += fabs(ldistance[dim])*lspeed[dim]*idx2_[dim]; |
| 135 | denominator += fabs(ldistance[dim])*idx2_[dim]; |
| 136 | } |
| 137 | |
| 138 | if (denominator != 0.0) |
| 139 | { |
| 140 | f_ext_[i] = numerator/denominator; |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | throw std::runtime_error( |
| 145 | "div by zero error in scikit-fmm extension velocity"); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void extensionVelocityMarcher::cleanUp() |
| 150 | { |
| 151 | for (int i=0; i<size_; i++) |
| 152 | { |
| 153 | if (flag_[i] != Frozen) f_ext_[i] = maxDouble; |
| 154 | } |
| 155 | } |