summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/asmeconf/examples/asmeconf-lualatex-ode-example.tex
blob: 6db7d9c08a90e131cc0b20e3bb729ba0e784ae0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
% !TEX TS-program = LuaLaTeX
% !TEX encoding = UTF-8 Unicode
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%%
%%  This file is an example of using asmeconf with lualatex to solve and plot an ode in a landscape figure.
%%
%%  Author: John H. Lienhard V
%%          Department of Mechanical Engineering
%%          Massachusetts Institute of Technology
%%          Cambridge, MA 02139-4307 USA
%%
 %=========================================================
%% 
%% LICENSE: 
%%
%% Copyright (c) 2021 John H. Lienhard
%% Offered under the MIT license: https://ctan.org/license/mit 
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[varvw,colorlinks,nofoot]{asmeconf}

\hypersetup{%
	pdftitle={Example of asmconf with LuaLaTeX to solve an ODE},
	pdfkeywords={asmeconf, LuaLaTeX, ODE, pgfplots, landscape figure},
	pdfauthor={John H. Lienhard},
}

\usepackage[figuresright]{rotating}% to use a landscape figure


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Now use lua code

%% Runge-Kutta for simple first-order ODE
\begin{luacode*}
-- Differential equation y’(t) = f(t,y)
-- with f(t,y) = A * y * cos(t+sqrt(1+y)).
-- Initial condition: y(0) = 1
function f(t,y,A)
    return A * y * math.cos(t+math.sqrt(1+y))
end
-- Code to write PGFplots data as coordinates
function print_RKfour(tMax,npoints,A,option)
     local t0 = 0.0
     local y0 = 1.0
     local A = A
     local h = (tMax-t0)/(npoints-1)
     local t = t0
     local y = y0
     if option~=[[]] then
          tex.sprint("\\addplot["..option.."] coordinates{")
     else
          tex.sprint("\\addplot coordinates{")
     end
     tex.sprint("("..t0..","..y0..")")
     for i=1, npoints do
          k1 = h * f(t,y,A)
          k2 = h * f(t+h/2,y+k1/2,A)
          k3 = h * f(t+h/2,y+k2/2,A)
          k4 = h * f(t+h,y+k3,A)
          y = y + (k1+2*k2+2*k3+k4)/6
          t = t + h
          tex.sprint("("..t..","..y..")")
     end
     tex.sprint("}")
end
\end{luacode*}

%% Define a latex macro to call the case to be plotted
\newcommand\addLUADEDplot[4][]{%
     \directlua{print_RKfour(#2,#3,#4,[[#1]])}%
}
% SYNTAX: Solution of the initial value problem
% Code assumed t = 0 at start and y(0) = 1 
% #2 is the final time
% #3 is the number of points
% #4 is an amplitude parameter
% #1 are options passed to the pgfplots \addplot function.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% For plotting the results, call pgfplots package

\usepackage{pgfplots}% load AFTER xcolor, which was already called by asmeconf.cls
\pgfplotsset{compat=newest}% to activate latest features

\pgfplotsset{%
     width=\textwidth,%
     height=0.6\textwidth,%
     /tikz/font={\sffamily},% 
     % every axis plot/.style={line width=2pt},
    every axis/.append style={very thick},% see manual page 189
    every minor tick/.append style={very thin,black},% modifies the style `every tick'
    every minor grid/.append style={very thin, color=Snow4},
    every major tick/.append style={thin, black},% modifies the style `every minor tick'
    every major grid/.append style={thin, color=Snow4},
    major tick length={1.2em},
    minor tick length={0.5em},
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% This environment will generate the .bib file the first time you run the code
% if the .bib file already exists, it will not be overwritten
\begin{filecontents}{asmeconf-lualatex-ode-example.bib}
@online{fairbairns,
  author 	= {Robin Fairbairns and Sebastian Rahtz and Leonor Barroca},
  title 	= {A Package for Rotated Objects in \LaTeX},
  version	= {2.16d},
  organization = {Comprehensive \TeX\ Archive Network},
  year 		= {2016},
  url 		= {https://www.ctan.org/pkg/rotating},
  urldate 	= {October 2, 2019},
}      
@article{montijano2014,
   title	= {Numerical methods with {\LuaLaTeX}},
   author	= {Juan I. Montijano and Mario P{\'{e}}rez and Luis R{\'{a}}ndez and Juan Luis Varona},
   year		= 2014,
   volume	= 35,
   month	= {January},
   number	= 1,
   pages	= {51--56},
   journal	= {TUGboat},
   url		= {https://tug.org/TUGboat/tb35-1/tb109montijano.pdf},
   note		= {Open access.}
}
@manual{pgfplots,
   title 	= {Manual for Package \textsc{PGFPLOTS}},
   url		= {https://ctan.org/pkg/pgfplots},
   author 	= {Christian Feuers{\"{a}}nger},
   version 	= {1.17},
   year 	= {2020},
   organization = {Comprehensive \TeX\ Archive Network},
   month 	= feb,
   urldate	= {January 4, 2021},
}
@manual{lua,
   author 	= {Roberto {l}erusalimschy and Luiz Henrique {de Figueiredo} and Waldemar Celes},
   title    = {{L}ua 5.3 Reference Manual},
   url	 	= {https://www.lua.org/manual/5.3/},
   organization = {Pontifical Catholic University},
   address 	= {Rio de Janeiro, Brazil},
   year 	= {2017},
}
\end{filecontents}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\ConfName{Proceedings of the \texttt{asmeconf} \linebreak International Examples Congress and Exposition}
\ConfAcronym{AIECE21}
\ConfDate{January 20, 2021}
\ConfCity{Cambridge, MA}
\PaperNo{AIECE2021-0001}

\title{Example of \NoCaseChange{{\upshape\hologo{LuaLaTeX}}} with asmeconf.cls for ODE Integration}
 
\SetAuthors{John H.\ Lienhard V\affil{1}\CorrespondingAuthor{lienhard@mit.edu}}
\SetAffiliation{1}{Massachusetts Institute of Technology, Cambridge, MA}

\maketitle

\versionfootnote{Version~1.0, \today}

\keywords{asmeconf, \hologo{LuaLaTeX}, ODE, pgfplots, landscape}

%%%%%%%%%  ABSTRACT  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{abstract}
This paper is an example of using \texttt{asmeconf} with {\upshape\hologo{LuaLaTeX}} to solve an ODE initial value problem using a fourth-order Runge-Kutta method and to plot the result using \texttt{PGFPLOTS}. The use of a landscape figure is also illustrated. References are given for further reading.
\end{abstract}

%%%%%%%%%  NOMENCLATURE (OPTIONAL) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{nomenclature}
\entry{$A$}{Constant parameter [--]}
\entry{$t$}{Time [s]}
\entry{$y(t)$}{Position [m]}
\end{nomenclature}

%%%%%%%%%  BODY OF PAPER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Introduction}
\hologo{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!

\section{Solution to an initial value problem}
We consider an initial value problem like that of Montijano et al.:
\begin{equation}\label{eqn:1}
y'(t) = A\cdot y(t) \cos\Big(t + \sqrt{1 + y(t)}\,\Big) \text{ with }y(0)=1
\end{equation}
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.)

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}.

Landscape figures, such as Fig.~\ref{fig:1}, may be produced at full-page size by putting \verb|\usepackage[figuresright]{rotating}| in your \texttt{.tex} file's preamble and using the \texttt{sidewaysfigure*} environment~\cite{fairbairns}.

\begin{sidewaysfigure*}
\begin{tikzpicture}
\begin{axis}[%
	 xmin=0., 
	 xmax=30., 
	 ymin=0.0, 
	 ymax=1.1,
     xtick={0,5,...,30}, 
     ytick={0,0.2,...,1.0},
     minor x tick num=4, 
     minor y tick num=3,
     xlabel={Time, $\mathsf{t}$ [s]}, 
     ylabel={Position, $\mathsf{y}$ [m]},
     xticklabel={$\mathsf{\pgfmathprintnumber{\tick}}$},
     yticklabel={$\mathsf{\pgfmathprintnumber{\tick}}$},
     legend style={ 
    	at={(0.7,0.85)},
		anchor=west,
		fill=none,
        cells={anchor=west},
     },
     ]
% tMax = 30, npoints = 400, A is the last argument
\addLUADEDplot[color=Purple4,densely dashed,,very thick]{30}{400}{0.25};% color names are from xcolor package
	\addlegendentry[fill=white]{$\mathsf{A = 0.25}$}; 
\addLUADEDplot[color=Chartreuse4,dashdotted,very thick]{30}{400}{0.5};
	\addlegendentry[fill=white]{$\mathsf{A = 0.5}$}; 
\addLUADEDplot[color=Red3,densely dotted,very thick]{30}{400}{0.75};
	\addlegendentry[fill=white]{$\mathsf{A = 0.75}$}; 
\addLUADEDplot[color=Blue3,smooth,very thick]{30}{400}{1};
	\addlegendentry[fill=white]{$\mathsf{A = 1}$}; 
\end{axis}
\end{tikzpicture}
\caption{A trial of pgfplot with Luacode Runge-Kutta integration\label{fig:1}}
\end{sidewaysfigure*}


%%%  CONCLUSIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Conclusion}
\hologo{LuaLaTeX} enables numerical computations within a \LaTeX\ environment. By combining this capability with
\texttt{PGFPlots}, the need for separate numerical and/or graphics packages can be reduced.

%%%  ACKNOWLEDGMENTS %%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Acknowledgments}
The example shown in this paper is directly based on an example given by Montijano et al.~\cite{montijano2014}. Additional examples, such as the
Lorenz attractor, are contained in that paper.


%%%  REFERENCES  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\bibliographystyle{asmeconf}%% .bst file following ASME conference format. Do not change.
\bibliography{asmeconf-lualatex-ode-example}%% this bib file will be generated from the filecontents environment upon first run of this .tex file.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}