UblasCustomFunctions.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #include "UblasCustomFunctions.hpp"
00030 
00031 c_vector<double, 1> Create_c_vector(double x)
00032 {
00033     c_vector<double, 1> v;
00034     v[0] = x;
00035     return v;
00036 }
00037 
00038 c_vector<double, 2> Create_c_vector(double x, double y)
00039 {
00040     c_vector<double, 2> v;
00041     v[0] = x;
00042     v[1] = y;
00043     return v;
00044 }
00045 
00046 c_vector<double, 3> Create_c_vector(double x, double y, double z)
00047 {
00048     c_vector<double, 3> v;
00049     v[0] = x;
00050     v[1] = y;
00051     v[2] = z;
00052     return v;
00053 }
00054 
00055 c_vector<double,3> CalculateEigenvectorForSmallestNonzeroEigenvalue(c_matrix<double, 3, 3>& rA)
00056 {
00057     int info;
00058     c_vector<double, 3> eigenvalues_real_part;
00059     c_vector<double, 3> eigenvalues_imaginary_part;
00060     c_vector<double, 4*3 > workspace;
00061     c_matrix<double, 3, 3> right_eigenvalues;
00062 
00063     char dont_compute_left_evectors = 'N';
00064     char compute_right_evectors = 'V';
00065 
00066     int matrix_size = 3;
00067     int matrix_ld = matrix_size;
00068     int workspace_size = 4*matrix_size;
00069 
00070     c_matrix<double, 3, 3> a_transpose;
00071     noalias(a_transpose) = trans(rA);
00072 
00073     // PETSc alias for dgeev or dgeev_
00074     LAPACKgeev_(&dont_compute_left_evectors, &compute_right_evectors,
00075            &matrix_size, a_transpose.data(),&matrix_ld,
00076            eigenvalues_real_part.data(), eigenvalues_imaginary_part.data(),
00077            NULL, &matrix_ld,
00078            right_eigenvalues.data(),&matrix_ld,
00079            workspace.data(),&workspace_size,
00080            &info);
00081     assert(info==0);
00082 
00083     // If this fails a complex eigenvalue was found
00084     assert(norm_2(eigenvalues_imaginary_part) < DBL_EPSILON);
00085 
00086     unsigned index_of_smallest=UINT_MAX;
00087     double min_eigenvalue = DBL_MAX;
00088 
00089     for (unsigned i=0; i<3; i++)
00090     {
00091         double eigen_magnitude = fabs(eigenvalues_real_part(i));
00092         if (eigen_magnitude < min_eigenvalue && eigen_magnitude >= DBL_EPSILON)
00093         {
00094             // A zero eigenvalue is ignored
00095             min_eigenvalue = eigen_magnitude;
00096             index_of_smallest = i;
00097         }
00098     }
00099     assert (min_eigenvalue != DBL_MAX);
00100     assert (index_of_smallest != UINT_MAX);
00101     assert (min_eigenvalue >= DBL_EPSILON);
00102 
00103     c_vector<double, 3> output;
00104     output(0) = right_eigenvalues(index_of_smallest, 0);
00105     output(1) = right_eigenvalues(index_of_smallest, 1);
00106     output(2) = right_eigenvalues(index_of_smallest, 2);
00107 
00108     return output;
00109 }
Generated on Thu Dec 22 13:00:07 2011 for Chaste by  doxygen 1.6.3