Chaste Release::3.1
|
00001 /* 00002 00003 Copyright (c) 2005-2012, University of Oxford. 00004 All rights reserved. 00005 00006 University of Oxford means the Chancellor, Masters and Scholars of the 00007 University of Oxford, having an administrative office at Wellington 00008 Square, Oxford OX1 2JD, UK. 00009 00010 This file is part of Chaste. 00011 00012 Redistribution and use in source and binary forms, with or without 00013 modification, are permitted provided that the following conditions are met: 00014 * Redistributions of source code must retain the above copyright notice, 00015 this list of conditions and the following disclaimer. 00016 * Redistributions in binary form must reproduce the above copyright notice, 00017 this list of conditions and the following disclaimer in the documentation 00018 and/or other materials provided with the distribution. 00019 * Neither the name of the University of Oxford nor the names of its 00020 contributors may be used to endorse or promote products derived from this 00021 software without specific prior written permission. 00022 00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 00034 */ 00035 00036 #include "UblasCustomFunctions.hpp" 00037 00038 c_vector<double, 1> Create_c_vector(double x) 00039 { 00040 c_vector<double, 1> v; 00041 v[0] = x; 00042 return v; 00043 } 00044 00045 c_vector<double, 2> Create_c_vector(double x, double y) 00046 { 00047 c_vector<double, 2> v; 00048 v[0] = x; 00049 v[1] = y; 00050 return v; 00051 } 00052 00053 c_vector<double, 3> Create_c_vector(double x, double y, double z) 00054 { 00055 c_vector<double, 3> v; 00056 v[0] = x; 00057 v[1] = y; 00058 v[2] = z; 00059 return v; 00060 } 00061 00062 c_vector<double,3> CalculateEigenvectorForSmallestNonzeroEigenvalue(c_matrix<double, 3, 3>& rA) 00063 { 00064 PetscBLASInt info; 00065 c_vector<PetscReal, 3> eigenvalues_real_part; 00066 c_vector<PetscReal, 3> eigenvalues_imaginary_part; 00067 c_vector<PetscScalar, 4*3 > workspace; 00068 c_matrix<PetscScalar, 3, 3> right_eigenvalues; 00069 00070 char dont_compute_left_evectors = 'N'; 00071 char compute_right_evectors = 'V'; 00072 00073 PetscBLASInt matrix_size = 3; 00074 PetscBLASInt matrix_ld = matrix_size; 00075 PetscBLASInt workspace_size = 4*matrix_size; 00076 00077 c_matrix<PetscScalar, 3, 3> a_transpose; 00078 noalias(a_transpose) = trans(rA); 00079 00080 // PETSc alias for dgeev or dgeev_ 00081 LAPACKgeev_(&dont_compute_left_evectors, &compute_right_evectors, 00082 &matrix_size, a_transpose.data(), &matrix_ld, 00083 eigenvalues_real_part.data(), eigenvalues_imaginary_part.data(), 00084 NULL, &matrix_ld, 00085 right_eigenvalues.data(), &matrix_ld, 00086 workspace.data(), &workspace_size, 00087 &info); 00088 assert(info==0); 00089 00090 // If this fails a complex eigenvalue was found 00091 assert(norm_2(eigenvalues_imaginary_part) < DBL_EPSILON); 00092 00093 unsigned index_of_smallest = UINT_MAX; 00094 double min_eigenvalue = DBL_MAX; 00095 00096 for (unsigned i=0; i<3; i++) 00097 { 00098 double eigen_magnitude = fabs(eigenvalues_real_part(i)); 00099 if (eigen_magnitude < min_eigenvalue && eigen_magnitude >= DBL_EPSILON) 00100 { 00101 // A zero eigenvalue is ignored 00102 min_eigenvalue = eigen_magnitude; 00103 index_of_smallest = i; 00104 } 00105 } 00106 assert (min_eigenvalue != DBL_MAX); 00107 assert (index_of_smallest != UINT_MAX); 00108 assert (min_eigenvalue >= DBL_EPSILON); 00109 00110 c_vector<double, 3> output; 00111 output(0) = right_eigenvalues(index_of_smallest, 0); 00112 output(1) = right_eigenvalues(index_of_smallest, 1); 00113 output(2) = right_eigenvalues(index_of_smallest, 2); 00114 00115 return output; 00116 }