1 // -*- C++ -*-
2 /**
3 * @brief Abstract top-level class for all spatial functions
4 *
5 * Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca
6 *
7 * This library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21 #include "openturns/ValueFunction.hxx"
22 #include "openturns/PersistentObjectFactory.hxx"
23 #include "openturns/EvaluationImplementation.hxx"
24 #include "openturns/NoEvaluation.hxx"
25
26 BEGIN_NAMESPACE_OPENTURNS
27
28 CLASSNAMEINIT(ValueFunction)
29
30 static const Factory<ValueFunction> Factory_ValueFunction;
31
32 /* Default constructor */
ValueFunction()33 ValueFunction::ValueFunction()
34 : FieldFunctionImplementation()
35 {
36 // Nothing to do
37 }
38
39 /* Parameter constructor */
ValueFunction(const Function & function,const Mesh & mesh)40 ValueFunction::ValueFunction(const Function & function,
41 const Mesh & mesh)
42 : FieldFunctionImplementation(mesh, function.getInputDimension(), mesh, function.getOutputDimension())
43 , function_(function)
44 {
45 // Set the descriptions
46 setInputDescription(function_.getInputDescription());
47 setOutputDescription(function_.getOutputDescription());
48 }
49
50 /* Parameter constructor */
ValueFunction(const Evaluation & evaluation,const Mesh & mesh)51 ValueFunction::ValueFunction(const Evaluation & evaluation,
52 const Mesh & mesh)
53 : FieldFunctionImplementation(mesh, evaluation.getInputDimension(), mesh, evaluation.getOutputDimension())
54 , function_(evaluation)
55 {
56 // Set the descriptions
57 setInputDescription(function_.getInputDescription());
58 setOutputDescription(function_.getOutputDescription());
59 }
60
61 /* Parameter constructor */
ValueFunction(const EvaluationImplementation & evaluation,const Mesh & mesh)62 ValueFunction::ValueFunction(const EvaluationImplementation & evaluation,
63 const Mesh & mesh)
64 : FieldFunctionImplementation(mesh, evaluation.getInputDimension(), mesh, evaluation.getOutputDimension())
65 , function_(evaluation)
66 {
67 // Set the descriptions
68 setInputDescription(function_.getInputDescription());
69 setOutputDescription(function_.getOutputDescription());
70 }
71
72 /* Virtual constructor */
clone() const73 ValueFunction * ValueFunction::clone() const
74 {
75 return new ValueFunction(*this);
76 }
77
78 /* Comparison operator */
operator ==(const ValueFunction &) const79 Bool ValueFunction::operator ==(const ValueFunction & ) const
80 {
81 return true;
82 }
83
84 /* String converter */
__repr__() const85 String ValueFunction::__repr__() const
86 {
87 OSS oss(true);
88 oss << "class=" << ValueFunction::GetClassName()
89 << " evaluation=" << function_.__repr__();
90 return oss;
91 }
92
93 /* String converter */
__str__(const String & offset) const94 String ValueFunction::__str__(const String & offset) const
95 {
96 return OSS(false) << function_.__str__(offset);
97 }
98
99 /* Operator () */
operator ()(const Sample & inFld) const100 Sample ValueFunction::operator() (const Sample & inFld) const
101 {
102 if (inFld.getDimension() != getInputDimension()) throw InvalidArgumentException(HERE) << "Error: expected field values of dimension=" << getInputDimension() << ", got dimension=" << inFld.getDimension();
103 if (inFld.getSize() != getInputMesh().getVerticesNumber()) throw InvalidArgumentException(HERE) << "Error: expected field values of size=" << getInputMesh().getVerticesNumber() << ", got size=" << inFld.getSize();
104 callsNumber_.increment();
105 return function_(inFld);
106 }
107
108 /* Get the i-th marginal function */
getMarginal(const UnsignedInteger i) const109 ValueFunction::Implementation ValueFunction::getMarginal(const UnsignedInteger i) const
110 {
111 if (!(i < getOutputDimension())) throw InvalidArgumentException(HERE) << "Error: the index of a marginal function must be in the range [0, outputDimension-1], here index=" << i << " and outputDimension=" << getOutputDimension();
112 return new ValueFunction(function_.getMarginal(i), getInputMesh());
113 }
114
115 /* Get the function corresponding to indices components */
getMarginal(const Indices & indices) const116 ValueFunction::Implementation ValueFunction::getMarginal(const Indices & indices) const
117 {
118 if (!indices.check(getOutputDimension())) throw InvalidArgumentException(HERE) << "Error: the indices of a marginal function must be in the range [0, outputDimension-1] and must be different";
119 return new ValueFunction(function_.getMarginal(indices), getInputMesh());
120 }
121
122 /* Function accessor */
getFunction() const123 Function ValueFunction::getFunction() const
124 {
125 return function_;
126 }
127
isActingPointwise() const128 Bool ValueFunction::isActingPointwise() const
129 {
130 return true;
131 }
132
133 /* Method save() stores the object through the StorageManager */
save(Advocate & adv) const134 void ValueFunction::save(Advocate & adv) const
135 {
136 FieldFunctionImplementation::save(adv);
137 adv.saveAttribute( "function_", function_ );
138 }
139
140 /* Method load() reloads the object from the StorageManager */
load(Advocate & adv)141 void ValueFunction::load(Advocate & adv)
142 {
143 FieldFunctionImplementation::load(adv);
144 adv.loadAttribute( "function_", function_ );
145 }
146
147 END_NAMESPACE_OPENTURNS
148