Debian Codegraph @

texmf-dist/doc/latex/asmeconf/examples/asmeconf-lualatex-ode-example.tex

raw
1 % !TEX TS-program = LuaLaTeX
2 % !TEX encoding = UTF-8 Unicode
3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 %%
5 %% This file is an example of using asmeconf with lualatex to solve and plot an ode in a landscape figure.
6 %%
7 %% Use of an up-to-date LaTeX distribution (after 2022) is very strongly recommended.
8 %%
9 %% Author: John H. Lienhard V
10 %% Department of Mechanical Engineering
11 %% Massachusetts Institute of Technology
12 %% Cambridge, MA 02139-4307 USA
13 %%
14 \def\luaexversionno{1.04}
15 %% 2025/03/27
16 %%
17 %=========================================================
18 %%
19 %% LICENSE:
20 %%
21 %% Copyright (c) 2025 John H. Lienhard
22 %% Offered under the MIT license: https://ctan.org/license/mit
23 %%
24 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
25
26 \DocumentMetadata{%
27 lang=en-US, % default
28 pdfstandard=A-3u,% A-2b, A-2u, A-3b, A-3u. Check that your figures also meet the standard you choose.
29 pdfversion=1.7,% 2.0 (Version 2.0 supports pdfstandard=A-4f with pdftex or UA-2 with LuaLaTeX)
30 % pdfversion = 2.0, % default
31 % pdfstandard = { ua-2 , a-4f },
32 % tagging = on, % for producing tagged pdf
33 }
34
35 \documentclass[grid,colorlinks,nofoot]{asmeconf}
36
37 \hypersetup{%
38 pdftitle={Example of asmconf with LuaLaTeX to solve an ODE},
39 pdfkeywords={asmeconf, LuaLaTeX, ODE, pgfplots, landscape figure},
40 pdfauthor={John H. Lienhard},
41 }
42
43 \usepackage[figuresright]{rotating}% to use a landscape figure
44 \usepackage{luacode}% for compatibility with older installations
45 % logos used in this document
46 \newcommand*\pdfTeX{pdf\TeX}
47 \newcommand*\LuaLaTeX{Lua\LaTeX}
48 \newcommand*\BibTeX{Bib\TeX}
49
50
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 %% Now use lua code
53
54 %% Runge-Kutta for simple first-order ODE
55 \begin{luacode*}
56 -- Differential equation y’(t) = f(t,y)
57 -- with f(t,y) = A * y * cos(t+sqrt(1+y)).
58 -- Initial condition: y(0) = 1
59 function f(t,y,A)
60 return A * y * math.cos(t+math.sqrt(1+y))
61 end
62 -- Code to write PGFplots data as coordinates
63 function print_RKfour(tMax,npoints,A,option)
64 local t0 = 0.0
65 local y0 = 1.0
66 local A = A
67 local h = (tMax-t0)/(npoints-1)
68 local t = t0
69 local y = y0
70 if option~=[[]] then
71 tex.sprint("\\addplot["..option.."] coordinates{")
72 else
73 tex.sprint("\\addplot coordinates{")
74 end
75 tex.sprint("("..t0..","..y0..")")
76 for i=1, npoints do
77 k1 = h * f(t,y,A)
78 k2 = h * f(t+h/2,y+k1/2,A)
79 k3 = h * f(t+h/2,y+k2/2,A)
80 k4 = h * f(t+h,y+k3,A)
81 y = y + (k1+2*k2+2*k3+k4)/6
82 t = t + h
83 tex.sprint("("..t..","..y..")")
84 end
85 tex.sprint("}")
86 end
87 \end{luacode*}
88
89 %% Define a latex macro to call the case to be plotted
90 \newcommand\addLUADEDplot[4][]{%
91 \directlua{print_RKfour(#2,#3,#4,[[#1]])}%
92 }
93 % SYNTAX: Solution of the initial value problem
94 % Code assumed t = 0 at start and y(0) = 1
95 % #2 is the final time
96 % #3 is the number of points
97 % #4 is an amplitude parameter
98 % #1 are options passed to the pgfplots \addplot function.
99
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 %% For plotting the results, call pgfplots package
102
103 \usepackage{pgfplots}% load AFTER xcolor, which was already called by asmeconf.cls
104 \pgfplotsset{compat=newest}% to activate latest features
105
106 \pgfplotsset{%
107 width=\textwidth,%
108 height=0.6\textwidth,%
109 /tikz/font={\sffamily},%
110 % every axis plot/.style={line width=2pt},
111 every axis/.append style={very thick},% see manual page 189
112 every minor tick/.append style={very thin,black},% modifies the style `every tick'
113 every minor grid/.append style={very thin, color=Snow4},
114 every major tick/.append style={thin, black},% modifies the style `every minor tick'
115 every major grid/.append style={thin, color=Snow4},
116 major tick length={1.2em},
117 minor tick length={0.5em},
118 }
119
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121
122 % This environment will generate the .bib file the first time you run the code
123 % if the .bib file already exists, it will not be overwritten
124 \begin{filecontents}{asmeconf-lualatex-ode-example.bib}
125 @online{fairbairns,
126 author = {Robin Fairbairns and Sebastian Rahtz and Leonor Barroca},
127 title = {A Package for Rotated Objects in \LaTeX},
128 version = {2.16d},
129 organization = {Comprehensive \TeX\ Archive Network},
130 year = {2016},
131 url = {https://www.ctan.org/pkg/rotating},
132 urldate = {October 2, 2019},
133 }
134 @article{montijano2014,
135 title = {Numerical methods with \LuaLaTeX},
136 author = {Juan I. Montijano and Mario P{\'{e}}rez and Luis R{\'{a}}ndez and Juan Luis Varona},
137 year = 2014,
138 volume = 35,
139 month = {January},
140 number = 1,
141 pages = {51--56},
142 journal = {TUGboat},
143 url = {https://tug.org/TUGboat/tb35-1/tb109montijano.pdf},
144 note = {Open access.}
145 }
146 @manual{pgfplots,
147 title = {Manual for Package \textsc{PGFPLOTS}},
148 url = {https://ctan.org/pkg/pgfplots},
149 author = {Christian Feuers{\"{a}}nger},
150 version = {1.17},
151 year = {2020},
152 organization = {Comprehensive \TeX\ Archive Network},
153 month = feb,
154 urldate = {January 4, 2021},
155 }
156 @manual{lua,
157 author = {Roberto {l}erusalimschy and Luiz Henrique {de Figueiredo} and Waldemar Celes},
158 title = {{L}ua 5.3 Reference Manual},
159 url = {https://www.lua.org/manual/5.3/},
160 organization = {Pontifical Catholic University},
161 address = {Rio de Janeiro, Brazil},
162 year = {2017},
163 }
164 \end{filecontents}
165
166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 \begin{document}
168
169 \ConfName{Proceedings of the \texttt{asmeconf} \linebreak International Examples Congress and Exposition}
170 \ConfAcronym{AIECE21}
171 \ConfDate{January 20, 2021}
172 \ConfCity{Cambridge, MA}
173 \PaperNo{AIECE2021-0001}
174
175 \title{Example of \NoCaseChange{{\upshape\LuaLaTeX}} with asmeconf.cls for ODE Integration}
176
177 \SetAuthors{John H.\ Lienhard V\affil{1}\CorrespondingAuthor{[email protected]}}
178 \SetAffiliation{1}{Department of Mechanical Engineering \\ Massachusetts Institute of Technology \\ Cambridge, MA}
179
180 \maketitle
181
182 \versionfootnote{Version~\luaexversionno, \today}
183
184 \keywords{asmeconf, \LuaLaTeX, ODE, pgfplots, landscape}
185
186 %%%%%%%%% ABSTRACT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 \begin{abstract}
188 This paper is an example of using {\upshape\texttt{asmeconf}} with {\upshape\LuaLaTeX} to solve an ODE initial value problem using a fourth-order Runge-Kutta method and to plot the result using {\upshape\texttt{PGFPLOTS}}. The use of a landscape figure is also illustrated. References are given for further reading.
189 \end{abstract}
190
191 %%%%%%%%% NOMENCLATURE (OPTIONAL) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192
193 \begin{nomenclature}
194 \entry{$A$}{Constant parameter [--]}
195 \entry{$t$}{Time [s]}
196 \entry{$y(t)$}{Position [m]}
197 \end{nomenclature}
198
199 %%%%%%%%% BODY OF PAPER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200
201 \section{Introduction}
202 \LuaLaTeX{} is built upon the Lua programming language~\cite{lua}. By directly using Lua code in a \LaTeX\ file, we can accomplish a wide range of tasks, as illustrated in the open-access paper by Montijano et al.~\cite{montijano2014}. In the present example, we follow Montijano et al.\ in solving a nonlinear first-order ordinary differential equation and plotting the result---all within a single \LaTeX\ file!
203
204 \section{Solution to an initial value problem}
205 We consider an initial value problem like that of Montijano et al.:
206 \begin{equation}\label{eqn:1}
207 y'(t) = A\cdot y(t) \cos\Big(t + \sqrt{1 + y(t)}\,\Big) \text{ with }y(0)=1
208 \end{equation}
209 Here, $A$ is a constant. We may adopt a fourth-order Runge-Kutta algorithm for the integration, which we shall perform to $t = 30$~s using a 400 point discretization. The details of the Runge-Kutta algorithm and a listing of the code are given in Montijano et al. (You can also read the code in the present \texttt{.tex} file.)
210
211 The algorithm is implemented directly in the preamble of this file, and the results are plotted in Fig.~\ref{fig:1} for $A = \{0.25, 0.5, 0.75, 1.0\}$. Plotting is done using the \texttt{PGFPLOTS} package~\cite{pgfplots}.
212
213 Landscape figures may be produced at full-page size by putting \verb|\usepackage[figuresright]{rotating}| (Fig.~\ref{fig:1}) into your \texttt{.tex} file's preamble and using the \texttt{sidewaysfigure*} environment~\cite{fairbairns}.
214
215 \begin{sidewaysfigure*}
216 \begin{tikzpicture}
217 \begin{axis}[%
218 xmin=0.,
219 xmax=30.,
220 ymin=0.0,
221 ymax=1.1,
222 xtick={0,5,...,30},
223 ytick={0,0.2,...,1.0},
224 minor x tick num=4,
225 minor y tick num=3,
226 xlabel={Time, $\mathsf{t}$ [s]},
227 ylabel={Position, $\mathsf{y}$ [m]},
228 xticklabel={$\mathsf{\pgfmathprintnumber{\tick}}$},
229 yticklabel={$\mathsf{\pgfmathprintnumber{\tick}}$},
230 legend style={
231 at={(0.7,0.85)},
232 anchor=west,
233 fill=none,
234 cells={anchor=west},
235 },
236 ]
237 % tMax = 30, npoints = 400, A is the last argument
238 \addLUADEDplot[color=Purple4,densely dashed,,very thick]{30}{400}{0.25};% color names are from xcolor package
239 \addlegendentry[fill=white]{$\mathsf{A = 0.25}$}
240 \addLUADEDplot[color=Chartreuse4,dashdotted,very thick]{30}{400}{0.5};
241 \addlegendentry[fill=white]{$\mathsf{A = 0.5}$}
242 \addLUADEDplot[color=Red3,densely dotted,very thick]{30}{400}{0.75};
243 \addlegendentry[fill=white]{$\mathsf{A = 0.75}$}
244 \addLUADEDplot[color=Blue3,smooth,very thick]{30}{400}{1};
245 \addlegendentry[fill=white]{$\mathsf{A = 1}$}
246 \end{axis}
247 \end{tikzpicture}
248 \caption{A trial of pgfplot with Luacode Runge-Kutta integration}\label{fig:1}
249 \end{sidewaysfigure*}
250
251
252 %%% CONCLUSIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253
254 \section{Conclusion}
255 \LuaLaTeX{} enables numerical computations within a \LaTeX{} environment. By combining this capability with
256 \texttt{PGFPlots}, the need for separate numerical and/or graphics packages can be reduced.
257
258 %%% ACKNOWLEDGMENTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
259
260 \section*{Acknowledgments}
261 The example shown in this paper is directly based on an example given by Montijano et al.~\cite{montijano2014}. Additional examples, such as the
262 Lorenz attractor, are contained in that paper.
263
264
265 %%% REFERENCES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266
267 \bibliographystyle{asmeconf}%% .bst file following ASME conference format. Do not change.
268 \bibliography{asmeconf-lualatex-ode-example}%% this bib file will be generated from the filecontents environment upon first run of this .tex file.
269
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271
272 \end{document}
273