UblasCustomFunctions.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
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 
00030 #include "UblasCustomFunctions.hpp"
00031 
00032 
00033 c_vector<double, 1> Create_c_vector(double x)
00034 {
00035     c_vector<double, 1> v;
00036     v[0] = x;
00037     return v;
00038 }
00039 
00040 c_vector<double, 2> Create_c_vector(double x, double y)
00041 {
00042     c_vector<double, 2> v;
00043     v[0] = x;
00044     v[1] = y;
00045     return v;
00046 }
00047 
00048 c_vector<double, 3> Create_c_vector(double x, double y, double z)
00049 {
00050     c_vector<double, 3> v;
00051     v[0] = x;
00052     v[1] = y;
00053     v[2] = z;
00054     return v;
00055 }
00056 
00057 c_vector<double,3> CalculateEigenvectorForSmallestNonzeroEigenvalue(c_matrix<double,3,3> &A)
00058 {
00059     int info;
00060     c_vector<double, 3 > eigenvalues_real_part;
00061     c_vector<double, 3 > eigenvalues_imaginary_part;
00062     c_vector<double, 4*3 > workspace;
00063     c_matrix<double, 3, 3> right_eigenvalues;
00064         
00065     char dont_compute_left_evectors = 'N';
00066     char compute_right_evectors = 'V';
00067     
00068     int matrix_size = 3;
00069     int matrix_ld = matrix_size;
00070     int workspace_size = 4*matrix_size;
00071     
00072     c_matrix<double, 3, 3> a_transpose;
00073     noalias(a_transpose) = trans(A);    
00074     
00075     //PETSc alias for dgeev or dgeev_
00076     LAPACKgeev_(&dont_compute_left_evectors, &compute_right_evectors, 
00077            &matrix_size, a_transpose.data(),&matrix_ld,
00078            eigenvalues_real_part.data(), eigenvalues_imaginary_part.data(), 
00079            NULL, &matrix_ld, 
00080            right_eigenvalues.data(),&matrix_ld,
00081            workspace.data(),&workspace_size,
00082            &info);    
00083     assert(info==0);
00084 
00085     // if this fails a complex eigenvalue was found
00086     assert(norm_2(eigenvalues_imaginary_part) == 0.0);
00087     
00088     unsigned index_of_smallest=UINT_MAX;
00089     double min_eigenvalue = DBL_MAX;
00090     
00091     for (unsigned i=0; i<3; i++)    
00092     {
00093         double eigen_magnitude = fabs(eigenvalues_real_part(i));
00094         if (eigen_magnitude < DBL_EPSILON)
00095         {
00096             //A zero eigenvalue is ignored
00097             continue;
00098         }
00099         if (eigen_magnitude < min_eigenvalue)
00100         {
00101             min_eigenvalue = eigen_magnitude;
00102             index_of_smallest = i;
00103         }
00104     }
00105     assert (min_eigenvalue != DBL_MAX);
00106     assert (index_of_smallest != UINT_MAX);
00107     assert (min_eigenvalue >= DBL_EPSILON);
00108 
00109     c_vector<double, 3> output;            
00110     output(0) = right_eigenvalues(index_of_smallest,0);
00111     output(1) = right_eigenvalues(index_of_smallest,1);
00112     output(2) = right_eigenvalues(index_of_smallest,2);
00113 
00114     return output;
00115     
00116 }

Generated on Wed Mar 18 12:51:52 2009 for Chaste by  doxygen 1.5.5