Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
UblasCustomFunctions.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
37
38c_vector<double, 1> Create_c_vector(double x)
39{
40 c_vector<double, 1> v;
41 v[0] = x;
42 return v;
43}
44
45c_vector<double, 2> Create_c_vector(double x, double y)
46{
47 c_vector<double, 2> v;
48 v[0] = x;
49 v[1] = y;
50 return v;
51}
52
53c_vector<double, 3> Create_c_vector(double x, double y, double z)
54{
55 c_vector<double, 3> v;
56 v[0] = x;
57 v[1] = y;
58 v[2] = z;
59 return v;
60}
61
62c_vector<double, 3> CalculateEigenvectorForSmallestNonzeroEigenvalue(c_matrix<double, 3, 3>& rA)
63{
64 //Check for symmetry
65 if (norm_inf(rA - trans(rA)) > 10 * DBL_EPSILON)
66 {
67 EXCEPTION("Matrix should be symmetric");
68 }
69
70 // Find the eigenvector by brute-force using the power method.
71 // We can't use the inverse method, because the matrix might be singular
72
73 c_matrix<double, 3, 3> copy_A(rA);
74 //Eigenvalue 1
75 c_vector<double, 3> eigenvec1 = scalar_vector<double>(3, 1.0);
76
77 double eigen1 = CalculateMaxEigenpair(copy_A, eigenvec1);
78
79 // Take out maximum eigenpair
80 c_matrix<double, 3, 3> wielandt_reduce_first_vector = identity_matrix<double>(3, 3);
81 wielandt_reduce_first_vector -= outer_prod(eigenvec1, eigenvec1);
82 copy_A = prod(wielandt_reduce_first_vector, copy_A);
83
84 c_vector<double, 3> eigenvec2 = scalar_vector<double>(3, 1.0);
85 double eigen2 = CalculateMaxEigenpair(copy_A, eigenvec2);
86
87 // Take out maximum (second) eigenpair
88 c_matrix<double, 3, 3> wielandt_reduce_second_vector = identity_matrix<double>(3, 3);
89 wielandt_reduce_second_vector -= outer_prod(eigenvec2, eigenvec2);
90 copy_A = prod(wielandt_reduce_second_vector, copy_A);
91
92 c_vector<double, 3> eigenvec3 = scalar_vector<double>(3, 1.0);
93 double eigen3 = CalculateMaxEigenpair(copy_A, eigenvec3);
94
95 //Look backwards through the eigenvalues, checking that they are non-zero
96 if (eigen3 >= DBL_EPSILON)
97 {
98 return eigenvec3;
99 }
100 if (eigen2 >= DBL_EPSILON)
101 {
102 return eigenvec2;
103 }
104 UNUSED_OPT(eigen1);
105 assert(eigen1 > DBL_EPSILON);
106 return eigenvec1;
107}
108
109double CalculateMaxEigenpair(c_matrix<double, 3, 3>& rA, c_vector<double, 3>& rEigenvector)
110{
111 double norm = 0.0;
112 double step = DBL_MAX;
113 while (step > DBL_EPSILON) //Machine precision
114 {
115 c_vector<double, 3> old_value(rEigenvector);
116 rEigenvector = prod(rA, rEigenvector);
117 norm = norm_2(rEigenvector);
118 rEigenvector /= norm;
119 if (norm < DBL_EPSILON)
120 {
121 //We don't care about a zero eigenvector, so don't polish it
122 break;
123 }
124 step = norm_inf(rEigenvector - old_value);
125 }
126 return norm;
127}
#define EXCEPTION(message)
#define UNUSED_OPT(var)