Debian Codegraph @

src/dr/evomodel/treedatalikelihood/continuous/AbstractDiffusionModelDelegate.java

raw symbols SCIP
1 /*
2 * HomogenousDiffusionModelDelegate.java
3 *
4 * Copyright (c) 2002-2016 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodel.treedatalikelihood.continuous;
27
28 import dr.evolution.tree.Tree;
29 import dr.evomodel.continuous.MultivariateDiffusionModel;
30 import dr.evomodel.treedatalikelihood.BufferIndexHelper;
31 import dr.evomodel.treedatalikelihood.continuous.cdi.ContinuousDiffusionIntegrator;
32 import dr.inference.model.AbstractModel;
33 import dr.inference.model.Model;
34 import dr.inference.model.Parameter;
35 import dr.inference.model.Variable;
36
37 import java.io.Serializable;
38
39 /**
40 * A simple diffusion model delegate with the same diffusion model over the whole tree
41 * @author Marc A. Suchard
42 * @author Andrew Rambaut
43 * @version $Id$
44 */
45 public abstract class AbstractDiffusionModelDelegate extends AbstractModel implements DiffusionProcessDelegate, Serializable {
46
47 // private static final boolean DEBUG = false;
48
49 final Tree tree;
50 private final MultivariateDiffusionModel diffusionModel;
51
52 private final BufferIndexHelper eigenBufferHelper;
53 private final BufferIndexHelper matrixBufferHelper;
54
55 AbstractDiffusionModelDelegate(Tree tree, MultivariateDiffusionModel diffusionModel,
56 int partitionNumber) {
57
58 super("AbstractDiffusionModelDelegate");
59
60 this.tree = tree;
61 this.diffusionModel = diffusionModel;
62 addModel(diffusionModel);
63
64 // two eigen buffers for each decomposition for store and restore.
65 eigenBufferHelper = new BufferIndexHelper(1, 0, partitionNumber);
66
67 // two matrices for each node less the root
68 matrixBufferHelper = new BufferIndexHelper(tree.getNodeCount(), 0, partitionNumber);
69 }
70
71 @Override
72 public int getEigenBufferCount() {
73 return eigenBufferHelper.getBufferCount();
74 }
75
76 @Override
77 public int getMatrixBufferCount() {
78 return matrixBufferHelper.getBufferCount();
79 }
80
81 @Override
82 public int getDiffusionModelCount() {
83 return 1;
84 }
85
86 @Override
87 public MultivariateDiffusionModel getDiffusionModel(int index) {
88 assert(index == 0);
89 return diffusionModel;
90 }
91
92 @Override
93 public int getMatrixIndex(int branchIndex) {
94 return matrixBufferHelper.getOffsetIndex(branchIndex);
95 }
96
97 @Override
98 public void setDiffusionModels(ContinuousDiffusionIntegrator cdi, boolean flip) {
99 if (flip) {
100 eigenBufferHelper.flipOffset(0);
101 }
102
103 cdi.setDiffusionPrecision(eigenBufferHelper.getOffsetIndex(0),
104 diffusionModel.getPrecisionParameter().getParameterValues(),
105 Math.log(diffusionModel.getDeterminantPrecisionMatrix())
106
107 );
108 }
109
110 @Override
111 public void updateDiffusionMatrices(ContinuousDiffusionIntegrator cdi, int[] branchIndices, double[] edgeLengths,
112 int updateCount, boolean flip) {
113
114 int[] probabilityIndices = new int[updateCount];
115
116 for (int i = 0; i < updateCount; i++) {
117 if (flip) {
118 matrixBufferHelper.flipOffset(branchIndices[i]);
119 }
120 probabilityIndices[i] = matrixBufferHelper.getOffsetIndex(branchIndices[i]);
121 }
122
123 cdi.updateBrownianDiffusionMatrices(
124 eigenBufferHelper.getOffsetIndex(0),
125 probabilityIndices,
126 edgeLengths,
127 getDriftRates(branchIndices, updateCount),
128 updateCount);
129 }
130
131 protected abstract double[] getDriftRates(int[] branchIndices, int updateCount);
132
133 @Override
134 public boolean hasDrift() { return false; }
135
136 @Override
137 protected void handleModelChangedEvent(Model model, Object object, int index) {
138 if (model == diffusionModel) {
139 fireModelChanged(model);
140 } else {
141 throw new RuntimeException("Unknown model");
142 }
143 }
144
145 @Override
146 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
147
148 }
149
150 @Override
151 public void storeState() {
152 eigenBufferHelper.storeState();
153 matrixBufferHelper.storeState();
154 }
155
156 @Override
157 public void restoreState() {
158 eigenBufferHelper.restoreState();
159 matrixBufferHelper.restoreState();
160 }
161
162 @Override
163 protected void acceptState() {
164
165 }
166 }