Coverage Report

Created: 2025-08-11 06:28

/src/quantlib/ql/methods/finitedifferences/meshers/fdmmeshercomposite.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2008 Andreas Gaida
5
 Copyright (C) 2008 Ralph Schreyer
6
 Copyright (C) 2008 Klaus Spanderen
7
8
 This file is part of QuantLib, a free-software/open-source library
9
 for financial quantitative analysts and developers - http://quantlib.org/
10
11
 QuantLib is free software: you can redistribute it and/or modify it
12
 under the terms of the QuantLib license.  You should have received a
13
 copy of the license along with this program; if not, please email
14
 <quantlib-dev@lists.sf.net>. The license is also available online at
15
 <http://quantlib.org/license.shtml>.
16
17
 This program is distributed in the hope that it will be useful, but WITHOUT
18
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 FOR A PARTICULAR PURPOSE.  See the license for more details.
20
*/
21
22
#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
23
#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
24
25
namespace QuantLib {
26
27
    namespace {
28
        typedef ext::shared_ptr<Fdm1dMesher> T;
29
30
        ext::shared_ptr<FdmLinearOpLayout> getLayoutFromMeshers(
31
0
                 const std::vector<ext::shared_ptr<Fdm1dMesher> > & meshers) {
32
0
            std::vector<Size> dim(meshers.size());
33
0
            for (Size i=0; i < dim.size(); ++i) {
34
0
                dim[i] = meshers[i]->size();
35
0
            }
36
0
            return ext::make_shared<FdmLinearOpLayout>(std::move(dim));
37
0
        }
38
    }
39
40
    FdmMesherComposite::FdmMesherComposite(
41
        const ext::shared_ptr<Fdm1dMesher>& mesher)
42
0
    : FdmMesher(getLayoutFromMeshers({mesher})),
43
0
      mesher_({mesher}) {
44
0
    }
45
46
47
    FdmMesherComposite::FdmMesherComposite(
48
        const ext::shared_ptr<Fdm1dMesher>& m1,
49
        const ext::shared_ptr<Fdm1dMesher>& m2)
50
0
    : FdmMesher(getLayoutFromMeshers({m1, m2})),
51
0
      mesher_({m1, m2}) {
52
0
    }
53
54
    FdmMesherComposite::FdmMesherComposite(
55
        const ext::shared_ptr<Fdm1dMesher>& m1,
56
        const ext::shared_ptr<Fdm1dMesher>& m2,
57
        const ext::shared_ptr<Fdm1dMesher>& m3)
58
0
    : FdmMesher(getLayoutFromMeshers({m1, m2, m3})),
59
0
      mesher_({m1, m2, m3}) {
60
0
    }
61
62
    FdmMesherComposite::FdmMesherComposite(
63
        const ext::shared_ptr<Fdm1dMesher>& m1,
64
        const ext::shared_ptr<Fdm1dMesher>& m2,
65
        const ext::shared_ptr<Fdm1dMesher>& m3,
66
        const ext::shared_ptr<Fdm1dMesher>& m4)
67
0
    : FdmMesher(getLayoutFromMeshers({m1, m2, m3, m4})),
68
0
      mesher_({m1, m2, m3, m4}) {
69
0
    }
70
71
    FdmMesherComposite::FdmMesherComposite(
72
        const std::vector<ext::shared_ptr<Fdm1dMesher> > & mesher)
73
0
    : FdmMesher(getLayoutFromMeshers(mesher)), mesher_(mesher) {
74
0
    }
75
76
    FdmMesherComposite::FdmMesherComposite(
77
        const ext::shared_ptr<FdmLinearOpLayout>& layout,
78
        const std::vector<ext::shared_ptr<Fdm1dMesher> > & mesher)
79
0
    : FdmMesher(layout), mesher_(mesher) {
80
0
        for (Size i=0; i < mesher.size(); ++i) {
81
0
            QL_REQUIRE(mesher[i]->size() == layout->dim()[i],
82
0
                       "size of 1d mesher " << i << " does not fit to layout");
83
0
        }
84
0
    }
85
86
    Real FdmMesherComposite::dplus(const FdmLinearOpIterator& iter,
87
0
                                   Size direction) const {
88
0
        return mesher_[direction]->dplus(iter.coordinates()[direction]);
89
0
    }
90
91
    Real FdmMesherComposite::dminus(const FdmLinearOpIterator& iter,
92
0
                                    Size direction) const {
93
0
        return mesher_[direction]->dminus(iter.coordinates()[direction]);
94
0
    }
95
96
    Real FdmMesherComposite::location(const FdmLinearOpIterator& iter,
97
0
                                      Size direction) const {
98
0
        return mesher_[direction]->location(iter.coordinates()[direction]);
99
0
    }
100
101
0
    Array FdmMesherComposite::locations(Size direction) const {
102
0
        Array retVal(layout_->size());
103
104
0
        for (const auto& iter : *layout_) {
105
0
            retVal[iter.index()] =
106
0
                mesher_[direction]->locations()[iter.coordinates()[direction]];
107
0
        }
108
109
0
        return retVal;
110
0
    }
111
112
    const std::vector<ext::shared_ptr<Fdm1dMesher> >&
113
0
        FdmMesherComposite::getFdm1dMeshers() const {
114
0
        return  mesher_;
115
0
    }
116
}