CoDiPack  2.3.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
jacobianTextReaderWriter.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2024 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include "../../config.h"
38#include "jacobianBaseReaderWriter.hpp"
39
41namespace codi {
56 template<typename T_Type>
58 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
59 using Tape = typename Type::Tape;
60 using Identifier = typename Type::Identifier;
61 using Real = typename Type::Real;
62
63 FILE* fileHandleTxt = nullptr;
64
65 bool printIO;
67
69 JacobianTextTapeWriter(std::string const& name, std::vector<Identifier> const& in,
70 std::vector<Identifier> const& out, bool ifIO, bool ifColumnNames)
71 : CommonBaseTapeWriter<T_Type>(name, in, out), printIO(ifIO), printColumnNames(ifColumnNames) {};
72
74 void start(Tape& tape) {
75 if (printIO) {
76 this->printIoText(tape);
77 }
78
79 this->openFile(fileHandleTxt, this->fileName, "w");
80 if (printColumnNames) {
81 fprintf(fileHandleTxt, "| LHS Index | # of Args | RHS Indices | RHS Jacobian Values |");
82 }
83 }
84
89 void writeStatement(Identifier const& curLhsIdentifier, size_t& curJacobianPos, Real const* const rhsJacobians,
90 Identifier const* const rhsIdentifiers, Config::ArgumentSize const& nJacobians) {
91 fprintf(fileHandleTxt, "\n%d %hhu [", curLhsIdentifier, static_cast<Config::ArgumentSize>(nJacobians));
92
93 if (nJacobians == Config::StatementInputTag) CODI_Unlikely {
94 // Do nothing.
95 } else CODI_Likely {
96 for (size_t argCount = 0; argCount < nJacobians; argCount++) {
97 fprintf(fileHandleTxt, " %d ", rhsIdentifiers[curJacobianPos + argCount]);
98 }
99
100 fprintf(fileHandleTxt, "] [");
101 for (size_t argCount = 0; argCount < nJacobians; argCount++) {
102 fprintf(fileHandleTxt, " %0.12e ", rhsJacobians[curJacobianPos + argCount]);
103 }
104 }
105 fprintf(fileHandleTxt, "]");
106 }
107
109 void finish() {
110 fclose(fileHandleTxt);
111 }
112 };
113
129 template<typename T_Type>
131 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
132 using Tape = typename Type::Tape;
133 using Identifier = typename Type::Identifier;
134 using Real = typename Type::Real;
135
137
139 void readFile(std::string const& name) {
140 FILE* fileHandleReadTxt = nullptr;
141
142 bool isFirstIdentifier = true;
143 Identifier lowestIndex = 0;
144
145 Identifier lhsIdentifier;
147
148 std::vector<Identifier> rhsIdentifiers(Config::MaxArgumentSize, 0);
149 std::vector<Real> rhsJacobians(Config::MaxArgumentSize, 0);
150 this->fileName = name;
151
152 this->restoreIoText();
153 this->tape.getIndexManager().updateLargestCreatedIndex(this->largestIndex);
154
155 this->openFile(fileHandleReadTxt, this->fileName, "r+");
156
157 // Read and discard column titles.
158 fscanf(fileHandleReadTxt, "| LHS Index | # of Args | RHS Indices | RHS Jacobian Values |");
159
160 // Read the statements.
161 while (fscanf(fileHandleReadTxt, "\n%d %hhu [", &lhsIdentifier, &nArgs) == 2) {
163 // TODO.
164 } else if (nArgs == Config::StatementInputTag) CODI_Unlikely {
165 // Do nothing.
166 } else CODI_Likely {
167 for (size_t argumentCount = 0; argumentCount < nArgs; argumentCount++) {
168 fscanf(fileHandleReadTxt, " %d ", &rhsIdentifiers[argumentCount]);
169 }
170 fscanf(fileHandleReadTxt, "] [");
171 for (size_t argumentCount = 0; argumentCount < nArgs; argumentCount++) {
172 fscanf(fileHandleReadTxt, " %lf ", &rhsJacobians[argumentCount]);
173 }
174 }
175 fscanf(fileHandleReadTxt, "]");
176
177 this->registerStatement(lhsIdentifier, nArgs, rhsIdentifiers, rhsJacobians, lowestIndex, isFirstIdentifier);
178 }
179
180 /* Update the user provided IO with a potential offset in the linear case. The registerStatement() returns
181 the offset.*/
182 this->updateUserIO(lowestIndex);
183
184 fclose(fileHandleReadTxt);
185 }
186 };
187}
#define CODI_Unlikely
Declare unlikely evaluation of an execution path.
Definition config.h:399
#define CODI_Likely
Declare likely evaluation of an execution path.
Definition config.h:397
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
size_t constexpr StatementLowLevelFunctionTag
Statement tag for low level functions.
Definition config.h:126
size_t constexpr MaxArgumentSize
Maximum number of arguments in a statement.
Definition config.h:120
uint8_t ArgumentSize
Type for the number of arguments in statements.
Definition config.h:117
size_t constexpr StatementInputTag
Tag for statements that are inputs. Used in linear index management context.
Definition config.h:123
CoDiPack - Code Differentiation Package.
Definition codi.hpp:91
void restoreIoText()
Constructor.
Definition commonReaderWriterBase.hpp:161
void updateUserIO(Identifier const &linearOffset)
This method is used to remove any offset and to update the largest created index.
Definition commonReaderWriterBase.hpp:209
Tape tape
The newly resorted tape.
Definition commonReaderWriterBase.hpp:153
Identifier largestIndex
The largest index on the stored tape.
Definition commonReaderWriterBase.hpp:155
This class is a common base for all the writers and produces a IO file that contains the input and ou...
Definition commonReaderWriterBase.hpp:84
void printIoText(Tape &tape)
Generate the IO file in a text format.
Definition commonReaderWriterBase.hpp:100
std::string fileName
The base file name provided by the user.
Definition commonReaderWriterBase.hpp:51
void openFile(FILE *&fileHandle, std::string const &name, std::string const &mode)
Definition commonReaderWriterBase.hpp:67
Used to register a statement for a Jacobian tape.
Definition jacobianBaseReaderWriter.hpp:48
void registerStatement(Identifier const &lhsIdentifier, Config::ArgumentSize const &nArgs, std::vector< Identifier > const &rhsIdentifiers, std::vector< Real > const &rhsJacobians, Identifier &lowestIndex, bool &isFirstIdentifier)
Constructor.
Definition jacobianBaseReaderWriter.hpp:57
Reads and restores a text file for a Jacobian tape.
Definition jacobianTextReaderWriter.hpp:130
typename Type::Real Real
See TapeReaderInterface.
Definition jacobianTextReaderWriter.hpp:134
T_Type Type
See TapeReaderInterface.
Definition jacobianTextReaderWriter.hpp:131
typename Type::Identifier Identifier
See TapeReaderInterface.
Definition jacobianTextReaderWriter.hpp:133
typename Type::Tape Tape
See TapeReaderInterface.
Definition jacobianTextReaderWriter.hpp:132
JacobianTextTapeReader()
Constructor.
Definition jacobianTextReaderWriter.hpp:136
void readFile(std::string const &name)
This method uses the the fileName to reproduce a valid tape.
Definition jacobianTextReaderWriter.hpp:139
Writes a Jacobian tape in a text format.
Definition jacobianTextReaderWriter.hpp:57
typename Type::Identifier Identifier
See TapeWriterInterface.
Definition jacobianTextReaderWriter.hpp:60
typename Type::Real Real
See TapeWriterInterface.
Definition jacobianTextReaderWriter.hpp:61
void finish()
After all the statements have been written, the finish method finalizes the writing process.
Definition jacobianTextReaderWriter.hpp:109
bool printIO
Flag to enable the writing of the IO file.
Definition jacobianTextReaderWriter.hpp:65
bool printColumnNames
Flag to enable column names.
Definition jacobianTextReaderWriter.hpp:66
T_Type Type
See TapeWriterInterface.
Definition jacobianTextReaderWriter.hpp:58
typename Type::Tape Tape
See TapeWriterInterface.
Definition jacobianTextReaderWriter.hpp:59
void start(Tape &tape)
Destructor.
Definition jacobianTextReaderWriter.hpp:74
JacobianTextTapeWriter(std::string const &name, std::vector< Identifier > const &in, std::vector< Identifier > const &out, bool ifIO, bool ifColumnNames)
Constructor.
Definition jacobianTextReaderWriter.hpp:69
void writeStatement(Identifier const &curLhsIdentifier, size_t &curJacobianPos, Real const *const rhsJacobians, Identifier const *const rhsIdentifiers, Config::ArgumentSize const &nJacobians)
Called for each statement. The method writes the current statement to the file. This overload is used...
Definition jacobianTextReaderWriter.hpp:89
FILE * fileHandleTxt
File handle.
Definition jacobianTextReaderWriter.hpp:63