Ben Huber committed May 31, 2017 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 ``````#include #include #include #include using namespace xerus; const size_t MAX_NUM_PER_SITE = 64; Tensor create_M() { Tensor M = -1*Tensor::identity({MAX_NUM_PER_SITE, MAX_NUM_PER_SITE}); for (size_t i = 0; i < MAX_NUM_PER_SITE-1; ++i) { M[{i+1, i}] = 1.0; } return M; } Tensor create_L() { Tensor L({MAX_NUM_PER_SITE, MAX_NUM_PER_SITE}); L.modify_diagonal_entries([](value_t& _x, const size_t _i) { _x = double(_i)/double(_i+5); }); return L; } Tensor create_S() { Tensor S({MAX_NUM_PER_SITE, MAX_NUM_PER_SITE}); // Set diagonal for (size_t i = 0; i < MAX_NUM_PER_SITE; ++i) { S[{i, i}] = -double(i); } // Set offdiagonal for (size_t i = 0; i < MAX_NUM_PER_SITE-1; ++i) { S[{i, i+1}] = double(i+1); } return 0.07*S; } TTOperator create_operator(const size_t _degree) { const Index i, j, k, l; // Create matrices const Tensor M = create_M(); const Tensor S = create_S(); const Tensor L = create_L(); const Tensor Sstar = 0.7*M+S; const Tensor I = Tensor::identity({MAX_NUM_PER_SITE, MAX_NUM_PER_SITE}); // Create empty TTOperator TTOperator A(2*_degree); Tensor comp; // Create first component comp(i, j, k, l) = Sstar(j, k)*Tensor::dirac({1, 3}, 0)(i, l) + L(j, k)*Tensor::dirac({1, 3}, 1)(i, l) + I(j, k)*Tensor::dirac({1, 3}, 2)(i, l); A.set_component(0, comp); // Create middle components comp(i, j, k, l) = I(j, k) * Tensor::dirac({3, 3}, {0, 0})(i, l) + M(j, k) * Tensor::dirac({3, 3}, {1, 0})(i, l) + S(j, k) * Tensor::dirac({3, 3}, {2, 0})(i, l) + L(j, k) * Tensor::dirac({3, 3}, {2, 1})(i, l) + I(j, k) * Tensor::dirac({3, 3}, {2, 2})(i, l); for(size_t c = 1; c+1 < _degree; ++c) { A.set_component(c, comp); } // Create last component comp(i, j, k, l) = I(j, k)*Tensor::dirac({3, 1}, 0)(i, l) + M(j, k)*Tensor::dirac({3, 1}, 1)(i, l) + S(j, k)*Tensor::dirac({3, 1}, 2)(i, l); A.set_component(_degree-1, comp); return A; } /// @brief calculates the one-norm of a TTTensor, assuming that all entries in it are positive double one_norm(const TTTensor &_x) { Index j; return double(_x(j&0) * TTTensor::ones(_x.dimensions)(j&0)); } std::vector implicit_euler(const TTOperator& _A, TTTensor _x, const double _stepSize, const size_t _n) { const TTOperator op = TTOperator::identity(_A.dimensions)-_stepSize*_A; Index j,k; auto ourALS = ALS_SPD; ourALS.convergenceEpsilon = 0; ourALS.numHalfSweeps = 2; std::vector results; TTTensor nextX = _x; results.push_back(_x); for(size_t i = 0; i < _n; ++i) { ourALS(op, nextX, _x); // Normalize double norm = one_norm(nextX); nextX /= norm; XERUS_LOG(iter, "Done itr " << i << " residual: " << frob_norm(op(j/2,k/2)*nextX(k&0) - _x(j&0)) << " norm: " << norm); _x = nextX; results.push_back(_x); } return results; } double get_mean_concentration(const TTTensor& _res, const size_t _i) { const Index k,l; TensorNetwork result(_res); const Tensor weights({MAX_NUM_PER_SITE}, [](const size_t _k){ return double(_k); }); const Tensor ones = Tensor::ones({1, MAX_NUM_PER_SITE, 1}); for (size_t j = 0; j < _res.degree(); ++j) { if (j == _i) { result(l&0) = result(k, l&1) * weights(k); } else { result(l&0) = result(k, l&1) * ones(k); } } // at this point the degree of 'result' is 0, so there is only one entry return result[{}]; } void print_mean_concentrations_to_file(const std::vector &_result) { std::fstream out("mean.dat", std::fstream::out); for (const auto& res : _result) { for (size_t k = 0; k < res.degree(); ++k) { out << get_mean_concentration(res, k) << ' '; } out << std::endl; } } int main() { const size_t numProteins = 10; const size_t numSteps = 300; const double stepSize = 1.0; const size_t rankX = 3; auto start = TTTensor::dirac( std::vector(numProteins, MAX_NUM_PER_SITE), 0 ); start.use_dense_representations(); start += 1e-14 * TTTensor::random( start.dimensions, std::vector(start.degree()-1, rankX-1) ); const auto A = create_operator(numProteins); const auto results = implicit_euler(A, start, stepSize, numSteps); print_mean_concentrations_to_file(results); } ``````