Exemplo n.º 1
0
MyVector<double> sumVectorsAndThenDivide(const MyVector<double>& A,
					 const MyVector<double>& B,
					 const double& divisor) {

  //Check that the vectors are the same size
  REQUIRE(A.Size() == B.Size(), "Error: vectors not the same size.\n");

  //Make a new MyVector<double> of the same size as the addends
  //Fill each element with zero.

  const int vectorSize = A.Size(); //length of each vector
  MyVector<double> sum(MV::Size(vectorSize), 0.);

  //Add the vectors component-by-component
  //Use the MyVector member function operator[](int i) which returns the
  //the ith component of the vector. 

  for(int i=0; i<vectorSize; ++i) {
    sum[i] = (A[i] + B[i]) / divisor; 
  }

  //Return the result

  return sum;  
}
Exemplo n.º 2
0
int main(int argc, char** argv) {
    MpiInit(&argc, &argv);

    const std::string helptxt =
"OneDFormat.input:                                                       \n"
"Input file for Assignment 5, Problem 2.                                 \n"
"Format   = string;               # Which 1-D output format              \n"
"Basename = string;               # Filename (without extension)         \n"
"Times    = double, double, ...;  # Which times to output                \n";
 
    const std::string options = ReadFileIntoString("OneDFormat.input");
    OptionParser parser(options, helptxt);
    const std::string format   = parser.Get<std::string>("Format");
    const std::string basename = parser.Get<std::string>("Basename");
    MyVector<double> times     = parser.Get<MyVector<double> >("Times");
 
    OneDimDataWriter* pWriter = OneDimDataWriter::Create(format, basename);
    for(int i = 0; i < times.Size(); ++i) {
        ComputeProfileAndOutput(times[i], pWriter);
    }
    delete pWriter;

    MpiFinalize();
    return EXIT_SUCCESS;
}
Exemplo n.º 3
0
MyVector<double> SumVectors(const MyVector<double>& v1,
                            const MyVector<double>& v2) {
    // Check preconditions
    REQUIRE(v1.Size() == v2.Size(), "Addends must be the same size");

    // Make a new MyVector<double> of the same size as the addends and fill
    //   each element with zero
    const int vectorSize = v1.Size();
    MyVector<double> sum(MV::Size(vectorSize), 0.0);
 
    // Add the vectors component-by-component
    for(int i = 0; i < vectorSize; ++i) {
        sum[i] = v1[i] + v2[i];
    }
 
    return sum;
}
Exemplo n.º 4
0
double TotalVolumeOfSpheresFromRadii(const MyVector<double>& radii) {
  const int Pi = 3.1415926535897932;
  double result = 0.0;
  int i;
  double r;
  for(i=0;i<radii.Size();++i) 
     double r = radii[i];
     result += (4/3)*Pi*r*r*r;
  return result;
}
Exemplo n.º 5
0
int main(int /*argc*/, char** /*argv*/) {
  //Try out the new sumVectors function                                         
 
  //First, create a couple of vectors, then add them together                   
 
  //const MyVector<double> A(MV::fill, 3.,4.,5.);
  //const MyVector<double> B(MV::fill, 6.,7.,8.);
  
  OptionParser p(ReadFileIntoString("Vectors.input"));
  
  MyVector <double> A = p.Get<MyVector<double> >("FirstVectorToAdd");
  MyVector <double> B = p.Get<MyVector<double> >("SecondVectorToAdd");
  	
  //Next, make a vector to store the result                                     
 
  MyVector<double> result(MV::Size(A.Size()),0.);
 
  //Test that the vectors are the same size                                    
  REQUIRE(A.Size() == B.Size(), "Error: vectors not the same size.\n");
  
  double d = p.Get<double>("Divider");
  REQUIRE(d != 0, "Error: divider is zero");

  //Add the vectors together                                                    
  result = sumVectorsDivided(A,B,d);
  //bug
  //result = A; 
  double resultMagnitudeSq = 0.0;
  for(int i=0;i<result.Size();++i) {
    resultMagnitudeSq += result[i]*result[i];
  }
  //Test that the result magnitude is zero  
  REQUIRE(resultMagnitudeSq == 0, "The result magnitude is not zero\n");

  //Print out the results                                                        
  cout << "Vector A = " << A << endl;
  cout << "Vector B = " << B << endl;
  cout << "Sum divided by " << d << " = " << result << endl;
 

  //Return success                                                              
  return EXIT_SUCCESS;
}
Exemplo n.º 6
0
int main(int /*argc*/, char** /*argv*/) {

  //Create UtilsForTesting object
  UtilsForTesting u;

  //Try out the new sumVectorsAndThenDivide function

  //First, read in some vectors from the file Vectors.input

  OptionParser p(ReadFileIntoString("Vectors.input"));
  MyVector<double> A = p.Get<MyVector<double> >("FirstVectorToAdd");
  MyVector<double> B = p.Get<MyVector<double> >("SecondVectorToAdd");
  double divisor = p.Get<double>("Divisor",1.);
  
  //Next, make a vector to store the result

  MyVector<double> result(MV::Size(A.Size()),0.);
  
  //Test that the divisor is nonzero
  IS_TRUE(divisor != 0, "Divisor is zero");

  //REQUIRE the divisor to be nonzero
  //Commented out to verify that the IS_TRUE line above also causes the test 
  //to fail
  //REQUIRE(divisor != 0, "Divisor must be nonzero");

  //Do the computation
  
  result = sumVectorsAndThenDivide(A,B,divisor);

  //Print out the results
  
  std::cout << "Vector A = " << A << std::endl;
  std::cout << "Vector B = " << B << std::endl;
  std::cout << "(A+B)/divisor = " << result << std::endl;

  //Test that the result magnitude is zero
  double resultMagnitudeSq = 0.0;
  for(int i=0;i<result.Size();++i) {
    resultMagnitudeSq += result[i]*result[i];
  }

  IS_ZERO(resultMagnitudeSq,"Sum of vector and its negative nonzero.");

  //How many tests failed?
  std::cout << u.NumberOfTestsFailed() << " tests failed." << std::endl;

  //Return number of failed tests
  return u.NumberOfTestsFailed();
}
Exemplo n.º 7
0
double MyTotalVolumeOfSpheresFromRadii(const MyVector<double>& radii)
{
  //declare pi as a double!
  const double pi = 3.1415926535897932;
  double result = 0.0;

  // don't declare unneeded vars out here
  for (int i=0; i<radii.Size(); ++i)
  {
    // don't even need r in side the loop.. go straight to radii[i]
    result += (4.0/3.0) * pi * pow(radii[i], 3);
  }
  return result;
}
Exemplo n.º 8
0
MyVector<double> DivideVectors(const MyVector<double>& numerator,
								const double& denominator) {
	//Make a new MyVector<double> of the same size as the divisors
	//Fill each element with zero

	const int vectorSize = numerator.Size();

	// Division for vectors which aren't the same size shouldn't take place
	MyVector<double> quotient(MV::Size(vectorSize), 0.);

	//Divide the vectors component-by-component
	for (int i=0; i < vectorSize; ++i) {
		quotient[i] = numerator[i] / denominator;
	}

	return quotient;
}
Exemplo n.º 9
0
int main(int /*argc*/, char** /*argv*/) {
  UtilsForTesting u;
  //Try out the new sumVectors function                                         
 
  //First, create a couple of vectors, then add them together                   
/* 
  const MyVector<double> A(MV::fill, 3.,4.,5.);
  const MyVector<double> B(MV::fill, 6.,7.,8.);
*/
  OptionParser p(ReadFileIntoString("Vectors.input"));
  MyVector<double> A = p.Get<MyVector<double> >("FirstVectorToAdd");
  MyVector<double> B = p.Get<MyVector<double> >("SecondVectorToAdd");
  double divisor = p.Get<double>("Divisor",1.0);
  REQUIRE(divisor!=0.0, "Error: divisor set to zero");
  IS_TRUE(divisor!=0.0, "Error: divisor is equal to zero");

  //Next, make a vector to store the result                                     
 
  MyVector<double> result(MV::Size(A.Size()),0.);
 
  //Add the vectors together                                                    
 
  result = sumVectors(A,B,divisor);
 
  //Print out the results                                                       
 
  std::cout << "Vector A = " << A << std::endl;
  std::cout << "Vector B = " << B << std::endl;
  std::cout << "Sum      = " << result << std::endl;

  //Test that the result magnitude is zero                                      
  double resultMagnitudeSq = 0.0;
  for(int i=0;i<result.Size();++i) {
    resultMagnitudeSq += result[i]*result[i];
  }
  IS_ZERO(resultMagnitudeSq,"Sum of vector and its negative nonzero.");


  //Return success                                                              
  //return EXIT_SUCCESS;
  return u.NumberOfTestsFailed();
}
Exemplo n.º 10
0
MyVector<double> SumVectors(const MyVector<double>& A,
                            const MyVector<double>& B) {

  //Make a new MyVector<double> of the same size as the addends
  //Fill each element with zero.
  const int vectorSize = A.Size();

  //Initialize to A.Size() for efficiency
  MyVector<double> sum = A;

  //Add the vectors component-by-component
  //Use the MyVector member function operator[](int i) which returns the
  //the ith component of the vector.
  for(int i=0; i<vectorSize; ++i) {
    sum[i] += B[i];
  }

  //Return the result
  return sum;
}
Exemplo n.º 11
0
int main(int /*argc*/, char** /*argv*/) {

  //Try out the new sumVectors function                                         

  //First, create a couple of vectors, then add them together                   

  OptionParser p(ReadFileIntoString("Vectors.input"));
  MyVector<double> A = p.Get<MyVector<double> >("FirstVectorToAdd");
  MyVector<double> B = p.Get<MyVector<double> >("SecondVectorToAdd");
  const double C = p.Get<double>("DoubleToDivideBy");

  // Check that C isn't equal to 0
  UtilsForTesting u;
  IS_TRUE(C != 0.0, "ERROR: DoubleToDivideBy can't be 0.0"); 
  //REQUIRE(divisor != 0.0, "ERROR: DoubleToDivideBY can't be 0.0"); 

  //Next, make a vector to store the result                                     

  MyVector<double> result(MV::Size(A.Size()),0.);

  //Add the vectors together                                                    

  result = addDivideVectors(A,B,C);

  //Print out the results                                                       

  std::cout << "Vector A = " << A << std::endl;
  std::cout << "Vector B = " << B << std::endl;
  std::cout << "Result      = " << result << std::endl;

  double resultMagnitudeSq = 0.0;
  for(int i=0; i<result.Size(); ++i){
    resultMagnitudeSq += result[i]*result[i];
  }
  
  IS_ZERO(resultMagnitudeSq, "Sum of vector and its negative nonzero.");
 
  //Return success                                                              
  return u.NumberOfTestsFailed();
}
Exemplo n.º 12
0
MyVector<double> sumVectors(const MyVector<double>& A,
                            const MyVector<double>& B,
			    const double& norm) {
 
  //Make a new MyVector<double> of the same size as the addends                 
  //Fill each element with zero.                                                
 
  const int vectorSize = A.Size(); //length of each vector                      
  MyVector<double> sum(MV::Size(vectorSize), 0.);
 
  //Add the vectors component-by-component                                      
  //Use the MyVector member function operator[](int i) which returns the        
  //the ith component of the vector.                                            
 
  for(int i=0; i<vectorSize; ++i) {
    sum[i] = (A[i] + B[i]) / norm;
  }
 
  //Return the result                                                           
 
  return sum;
}
Exemplo n.º 13
0
  void OutputReducedDatIlana::AppendToFileImpl(const double time,
					       const MyVector<double>& x,
					       const MyVector<double>& y) const {

    REQUIRE(x.Size()==y.Size(), "x and y are not the same size!");
    REQUIRE(x.Size()>0 && y.Size()>0, "x and y must have at least 1 data point!");

    // Calculate cosine of minimum angle
    double cosMinAngle = cos(mMinAngle*180./M_PI);

    // Every time AppendToFileImpl is called, the number of files is increased
    // File name reflects this.
    std::ostringstream File;
    File << mBaseName << "_Num" << ++mOutFileNum << ".dat";
    std::ofstream out(File.str().c_str());

    // First point
    out << DoubleToString(x[0],16) << " " << DoubleToString(y[0],16) << "\n";

    if(x.Size()>2) {
      for(int i=1; i<x.Size()-1; i++){

	double xdiff1 = x[i] - x[i-1];
	double ydiff1 = y[i] - y[i-1];
	double xdiff2 = x[i+1] - x[i];
	double ydiff2 = y[i+1] - y[i];

	// Figure out cosine of angle between line segments
	double cosAngle = (xdiff1*xdiff2 + ydiff1*ydiff2)/sqrt((xdiff1*xdiff1 + ydiff1*ydiff1)*(xdiff2*xdiff2 + ydiff2*ydiff2));

	if(cosAngle<=cosMinAngle)
	  out << DoubleToString(x[i],16) << " " << DoubleToString(y[i],16) << "\n";
      }
    }
    // Last point
    out << DoubleToString(x[x.Size()-1],16) << " " << DoubleToString(y[y.Size()-1],16) << "\n";
  }
Exemplo n.º 14
0
int main() {

    const std::string Help =
        "-------------------------------------------------------------------------\n"
        "TestComputeAKV:                                                          \n"
        "-------------------------------------------------------------------------\n"
        "OPTIONS:                                                                 \n"
        "Nth=<int>  theta resolution [default 3]                                  \n"
        "Nph=<int>  phi resolution [default 4]                                    \n"
        "Radius=<double> radius of sphere. [default 1.0]                          \n"
        "AKVGuess=MyVector<double> a guess for the values of THETA, thetap, phip  \n"
        "         [default (0.,0.,0.)]                                            \n"
        "L_resid_tol=<double> tolerance for L residuals when finding approximate  \n"
        "            Killing vectors.  [default 1.e-12]                           \n"
        "v_resid_tol=<double> tolerance for v residuals when finding approximate  \n"
        "            Killing vectors.  [default 1.e-12]                           \n"
        "min_thetap = for values less than this, thetap is considered close to    \n"
        "               zero. [default 1.e-5]                                     \n"
        "symmetry_tol=<double> abs(THETA) must be less than this value to be      \n"
        "             considered an exact symmetry.  [default 1.e-11]             \n"
        "ResidualSize=<double> determines the tolerance for residuals from the    \n"
        "             multidimensional root finder.  [default to 1.e-11]          \n"
        "Solver = <std::string> which gsl multidimensional root finding algorith  \n"
        "        should be used. [default Newton]                                 \n"
        "Verbose=<bool> Print spectral coefficients and warnings if true          \n"
        "        [default false]                                                  \n"
        ;

    std::string Options = ReadFileIntoString("Test.input");
    OptionParser op(Options,Help);
    const int Nth = op.Get<int>("Nth", 3);
    const int Nph = op.Get<int>("Nph", 4);
    const double rad = op.Get<double>("Radius",1.0);
    MyVector<double> AKVGuess =
        op.Get<MyVector<double> >("AKVGuess",MyVector<double>(MV::Size(3),0.0));
    //must be three-dimensional
    REQUIRE(AKVGuess.Size()==3,"AKVGuess has Size " << AKVGuess.Size()
            << ", should be 3.");
    const double L_resid_tol = op.Get<double>("L_resid_tol", 1.e-12);
    const double v_resid_tol = op.Get<double>("L_resid_tol", 1.e-12);
    const double residualSize = op.Get<double>("ResidualSize", 1.e-11);
    const double min_thetap = op.Get<double>("min_theta",1.e-5);
    const double symmetry_tol = op.Get<double>("symmetry_tol",1.e-11);
    const std::string solver = op.Get<std::string>("Solver","Newton");
    const bool verbose = op.Get<bool>("Verbose", false);
    const MyVector<bool> printDiagnostic = MyVector<bool>(MV::Size(6), true);

    //create skm
    const StrahlkorperMesh skm(Nth, Nph);
    //create surface basis
    const SurfaceBasis sb(skm);
    //get theta, phi
    const DataMesh theta(skm.SurfaceCoords()(0));
    const DataMesh phi(skm.SurfaceCoords()(1));

    //set the initial guesses to be along particular axes
    const int axes = 3; //the number of perpendicular axes

    //create conformal factors for every rotation
    const int syms = 5; //the number of axisymmetries we are testing

    for(int s=4; s<5; s++) { //index over conformal factor symmetries
        //for(int s=0; s<syms; s++){//index over conformal factor symmetries
        //create conformal factor
        const DataMesh Psi = ConstructConformalFactor(theta, phi, s);

        //set the initial guesses
        double THETA[3] = {AKVGuess[0],0.,0.};
        double thetap[3] = {AKVGuess[1],0.,0.};
        double phip[3] = {AKVGuess[2],0.,0.};

        //save the v, xi solutions along particular axes
        MyVector<DataMesh> v(MV::Size(3),DataMesh::Empty);
        MyVector<DataMesh> rotated_v(MV::Size(3),DataMesh::Empty);
        MyVector<Tensor<DataMesh> > xi(MV::Size(axes),Tensor<DataMesh>(2,"1",DataMesh::Empty));

        //save the <v_i|v_j> inner product solutions
        double v0v0 = 0.;
        double v1v1 = 0.;
        double v2v2 = 0.;
        double v0v1 = 0.;
        double v0v2 = 0.;
        double v1v2 = 0.;
        //int symmetries[3] = 0; //counts the number of symmetries

        //compute some useful quantities
        const DataMesh rp2 = rad * Psi * Psi;
        const DataMesh r2p4 = rp2*rp2;
        const DataMesh llncf = sb.ScalarLaplacian(log(Psi));
        const DataMesh Ricci = 2.0 * (1.0-2.0*llncf) / r2p4;
        const Tensor<DataMesh> GradRicci = sb.Gradient(Ricci);

        for(int a=0; a<axes; a++) { //index over perpendicular axes to find AKV solutions

            //if the diagnostics below decide that there is a bad solution for v[a]
            //(usually a repeated solution), this flag will indicate that the
            //solver should be run again
            bool badAKVSolution = false;

            //generate a guess for the next axis of symmetry based on prior solutions.
            AxisInitialGuess(thetap, phip, a);

            //create L
            DataMesh L(DataMesh::Empty);

            //setup struct with all necessary data
            rparams p = {theta, phi, rp2, sb, llncf, GradRicci,
                         L, v[a], L_resid_tol, v_resid_tol, verbose, true
                        };

            RunAKVsolvers(THETA[a], thetap[a], phip[a], min_thetap,
                          residualSize, verbose, &p, solver);

            std::cout << "Solution found with : THETA[" << a << "] = " << THETA[a] << "\n"
                      << "                     thetap[" << a << "] = " << (180.0/M_PI)*thetap[a] << "\n"
                      << "                       phip[" << a << "] = " << (180.0/M_PI)*phip[a]
                      << std::endl;

            //check inner products
            // <v_i|v_j> = Integral 0.5 * Ricci * Grad(v_i) \cdot Grad(v_j) dA
            switch(a) {
            case 0:
                //compute inner product <v_0|v_0>
                v0v0 = AKVInnerProduct(v[0],v[0],Ricci,sb)*sqrt(2.)*M_PI;
                //if(v0v0<symmetry_tol) //symmetries++;
                std::cout << "<v_0|v_0> = " << v0v0 << std::endl;
                std::cout << "-THETA <v_0|v_0> = " << -THETA[a]*v0v0 << std::endl;
                break;
            case 1:
                //compute inner products <v_1|v_1>, <v_0|v_1>
                v1v1 = AKVInnerProduct(v[1],v[1],Ricci,sb)*sqrt(2.)*M_PI;
                v0v1 = AKVInnerProduct(v[0],v[1],Ricci,sb)*sqrt(2.)*M_PI;
                //if(v1v1<symmetry_tol) //symmetries++;
                std::cout << "<v_1|v_1> = " << v1v1 << std::endl;
                std::cout << "<v_0|v_1> = " << v0v1 << std::endl;
                std::cout << "-THETA <v_1|v_1> = " << -THETA[a]*v1v1 << std::endl;
                if(fabs(v0v0) == fabs(v1v2)) badAKVSolution = true;
                break;
            case 2:
                //compute inner products <v_2|v_2>, <v_0|v_2>, <v_1|v_2>
                v2v2 = AKVInnerProduct(v[2],v[2],Ricci,sb)*sqrt(2.)*M_PI;
                v0v2 = AKVInnerProduct(v[0],v[2],Ricci,sb)*sqrt(2.)*M_PI;
                v1v2 = AKVInnerProduct(v[1],v[2],Ricci,sb)*sqrt(2.)*M_PI;
                //if(v2v2<symmetry_tol) //symmetries++;
                std::cout << "<v_2|v_2> = " << v2v2 << std::endl;
                std::cout << "<v_0|v_2> = " << v0v2 << std::endl;
                std::cout << "<v_1|v_2> = " << v1v2 << std::endl;
                std::cout << "-THETA <v_2|v_2> = " << -THETA[a]*v2v2 << std::endl;
                if(fabs(v0v0) == fabs(v0v2)) badAKVSolution = true;
                if(fabs(v1v1) == fabs(v1v2)) badAKVSolution = true;
                break;
            }

            //Gram Schmidt orthogonalization
            switch(a) {
            case 1:
                if(v0v0<symmetry_tol && v1v1<symmetry_tol) { //two symmetries, v2v2 should also be symmetric
                    GramSchmidtOrthogonalization(v[0], v0v0, v[1], v0v1);
                }
                break;
            case 2:
                if(v0v0<symmetry_tol) {
                    if(v1v1<symmetry_tol) {
                        REQUIRE(v2v2<symmetry_tol, "Three symmetries required, but only two found.");
                        GramSchmidtOrthogonalization(v[0], v0v0, v[2], v0v2);
                        GramSchmidtOrthogonalization(v[1], v1v1, v[2], v1v2);
                    } else if(v2v2<symmetry_tol) {
                        REQUIRE(false, "Three symmetries required, but only two found.");
                    } else {
                        GramSchmidtOrthogonalization(v[1], v1v1, v[2], v1v2);
                    }
                } else if(v1v1<symmetry_tol) {
                    if(v2v2<symmetry_tol) {
                        REQUIRE(false, "Three symmetries required, but only two found.");
                    } else {
                        GramSchmidtOrthogonalization(v[0], v0v0, v[2], v0v2);
                    }
                } else if(v2v2<symmetry_tol) {
                    GramSchmidtOrthogonalization(v[0], v0v0, v[1], v0v1);
                }
                break;
            }

            //create xi (1-form)
            xi[a] = ComputeXi(v[a], sb);

            //perform diagnostics
            //Psi and xi are unscaled and unrotated
            KillingDiagnostics(sb, L, Psi, xi[a], rad, printDiagnostic);

            //rotate v, Psi for analysis
            rotated_v[a] = RotateOnSphere(v[a],theta,phi,
                                          sb,thetap[a],phip[a]);

            DataMesh rotated_Psi = RotateOnSphere(Psi,theta,phi,
                                                  sb,thetap[a],phip[a]);

            //compare scale factors

            const double scaleAtEquator =
                NormalizeAKVAtOnePoint(sb, rotated_Psi, rotated_v[a], rad, M_PI/2., 0.0);
            PrintSurfaceNormalization(sb,rotated_Psi,theta,phi,rotated_v[a],scaleAtEquator,rad);
            MyVector<double> scaleInnerProduct = InnerProductScaleFactors(v[a], v[a], Ricci, r2p4, sb);
            PrintSurfaceNormalization(sb,rotated_Psi,theta,phi,rotated_v[a],scaleInnerProduct[0],rad);
            PrintSurfaceNormalization(sb,rotated_Psi,theta,phi,rotated_v[a],scaleInnerProduct[1],rad);
            PrintSurfaceNormalization(sb,rotated_Psi,theta,phi,rotated_v[a],scaleInnerProduct[2],rad);
            OptimizeScaleFactor(rotated_v[a], rotated_Psi, rad, sb, theta,
                                phi, scaleAtEquator, scaleInnerProduct[0], scaleInnerProduct[1], scaleInnerProduct[2]);

            //scale v
            v[a] *= scaleAtEquator;

            //recompute scaled xi (1-form)
            xi[a] = ComputeXi(v[a], sb);

            if(badAKVSolution) {
                v[a] = 0.;
                thetap[a] += M_PI/4.;
                phip[a] += M_PI/4.;
                a--;
                std::cout << "This was a bad / repeated solution, and will be recomputed." << std::endl;
            }
            std::cout << std::endl;
        }//end loop over perpendicular AKV axes

        std::cout << "\n" << std::endl;
    }


    // Return 0 for success
    return NumberOfTestsFailed;
}