source: trunk/cppad/local/check_for_nan.hpp @ 3746

Last change on this file since 3746 was 3746, checked in by bradbell, 4 years ago

merge to branch: trunk
from repository: https://github.com/coin-or/CppAD
start hash code: 57d3215cae5e9da7b4c92e89f038d70870ef7327
end hash code: 9aebc1ca2c0949dec7c2d156517db26e60f28159

commit 9aebc1ca2c0949dec7c2d156517db26e60f28159
Author: Brad Bell <bradbell@…>
Date: Sun Nov 8 20:15:38 2015 -0800

Remove invisible white space.

commit a92ac50e9f4c8d0007ea5a245b3e23145dfcebfe
Author: Brad Bell <bradbell@…>
Date: Sun Nov 8 20:15:31 2015 -0800

Use vectorBool with partial sparsity patterns per pass to reduce memory requirements.


solve_callback.hpp: remove invisible white space.
rev_sparse_jac.hpp: fix bug (argument transposed).
bool_sparsity.cpp: remove invisible white space.

commit c09744b13ba2c70d6ffa857206d45560154d800a
Author: Brad Bell <bradbell@…>
Date: Sun Nov 8 03:22:57 2015 -0800

check_for_nan.hpp: fix minor type in documentation.

File size: 4.7 KB
Line 
1// $Id$
2# ifndef CPPAD_CHECK_FOR_NAN_INCLUDED
3# define CPPAD_CHECK_FOR_NAN_INCLUDED
4
5/* --------------------------------------------------------------------------
6CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-15 Bradley M. Bell
7
8CppAD is distributed under multiple licenses. This distribution is under
9the terms of the
10                    Eclipse Public License Version 1.0.
11
12A copy of this license is included in the COPYING file of this distribution.
13Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
14-------------------------------------------------------------------------- */
15/*
16$begin check_for_nan$$
17$spell
18        std
19        vec
20        Cpp
21        const
22        bool
23        newline
24$$
25$section Check an ADFun Object For Nan Results$$
26
27$head Syntax$$
28$icode%f%.check_for_nan(%b%)
29%$$
30$icode%b% = %f%.check_for_nan()
31%$$
32$codei%get_check_for_nan(%vec%, %file%)
33%$$
34
35$head Purpose$$
36If $code NDEBUG$$ is not defined and
37the result of a $cref/forward/forward_order/$$ or $cref/reverse/reverse_any/$$
38calculation contains a  $cref nan$$,
39CppAD can halt with an error message.
40
41$head f$$
42For the syntax where $icode b$$ is an argument,
43$icode f$$ has prototype
44$codei%
45        ADFun<%Base%> %f%
46%$$
47(see $codei%ADFun<%Base%>%$$ $cref/constructor/FunConstruct/$$).
48For the syntax where $icode b$$ is the result,
49$icode f$$ has prototype
50$codei%
51        const ADFun<%Base%> %f%
52%$$
53
54$head b$$
55This argument or result has prototype
56$codei%
57        bool %b%
58%$$
59Future calls to $icode%f%.Forward%$$ will (will not) check for $code nan$$.
60depending on if $icode b$$ is true (false).
61
62$head Default$$
63The value for this setting after construction of $icode f$$) is true.
64The value of this setting is not affected by calling
65$cref Dependent$$ for this function object.
66
67$head Debugging$$
68If this error is detected during zero order forward mode,
69the values of the independent variables that resulted in the $code nan$$
70are written to a temporary binary file.
71This is so that you can run the original source code with those values
72to see what is causing the $code nan$$.
73
74$subhead vector_size$$
75The error message with contain the text
76$codei%vector_size = %vector_size%$$ followed the newline character
77$code '\n'$$.
78The value of $icode vector_size$$ is the number of elements
79in the independent vector.
80
81$subhead file_name$$
82The error message with contain the text
83$codei%file_name = %file_name%$$ followed the newline character
84$code '\n'$$.
85The value of $icode file_name$$ is the name of the temporary file
86that contains the dependent variable values.
87
88$head get_check_for_nan$$
89This routine can be used to get the independent variable
90values that result in a $code nan$$.
91
92$subhead vec$$
93This argument has prototype
94$codei%
95        CppAD::vector<%Base%>& %vec%
96%$$
97It size must be equal to the corresponding value of
98$cref/vector_size/check_for_nan/Debugging/vector_size/$$
99in the corresponding error message.
100The input value of its elements does not matter.
101Upon return, it will contain the values for the independent variables,
102in the corresponding call to $cref Independent$$,
103that resulted in the $code nan$$.
104(Note that the call to $code Independent$$ uses an vector with elements
105of type $codei%AD<%Base%>%$$ and $icode vec$$ has elements of type
106$icode Base$$.)
107
108$subhead file$$
109This argument has prototype
110$codei%
111        const std::string& %file%
112%$$
113It must be the value of
114$cref/file_name/check_for_nan/Debugging/file_name/$$
115in the corresponding error message.
116
117
118$head Example$$
119$children%
120        example/check_for_nan.cpp
121%$$
122The file
123$cref check_for_nan.cpp$$
124contains an example and test of these operations.
125It returns true if it succeeds and false otherwise.
126
127$end
128*/
129
130# include <cppad/vector.hpp>
131# include <cppad/configure.hpp>
132# include <fstream>
133# include <stdlib.h>
134# include <unistd.h>
135
136namespace CppAD { // BEGIN_CPPAD_NAMESPACE
137
138template <class Base>
139void put_check_for_nan(const CppAD::vector<Base>& vec, std::string& file_name)
140{
141        size_t char_size       = sizeof(Base) * vec.size();
142        const char* char_ptr   = reinterpret_cast<const char*>( vec.data() );
143# if CPPAD_HAS_MKSTEMP
144        char pattern[] = "/tmp/fileXXXXXX";
145        int fd = mkstemp(pattern);
146        file_name = pattern;
147        write(fd, char_ptr, char_size);
148        close(fd);
149# else
150        file_name = tmpnam( CPPAD_NULL );
151        std::fstream file_out(file_name.c_str(), std::ios::out|std::ios::binary );
152        file_out.write(char_ptr, char_size);
153        file_out.close();
154# endif
155        return;
156}
157
158template <class Base>
159void get_check_for_nan(CppAD::vector<Base>& vec, const std::string& file_name)
160{       //
161        size_t n = vec.size();
162        size_t char_size = sizeof(Base) * n;
163        char* char_ptr   = reinterpret_cast<char*>( vec.data() );
164        //
165        std::fstream file_in(file_name.c_str(), std::ios::in|std::ios::binary );
166        file_in.read(char_ptr, char_size);
167        //
168        return;
169}
170
171} // END_CPPAD_NAMESPACE
172
173# endif
Note: See TracBrowser for help on using the repository browser.