energy.gp: Add gnuplot script and rules to build an energy
[assignment-template.git] / Makefile
1 # Copyright (C) 2012-2013  W. Trevor King
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 # Declare variables describing package information.
17 #   http://www.gnu.org/software/make/manual/html_node/Setting.html
18 # The $(...) syntax references a variable's value.
19 #   http://www.gnu.org/software/make/manual/html_node/Reference.html
20 # You can override variables from the command line.  For example
21 #   $ make VERSION=2 dist
22 # will generate phys305-hw4-2.tar.gz
23 COURSE = phys305
24 PACKAGE = hw4
25 VERSION = 1
26 RELEASE = $(COURSE)-$(PACKAGE)-$(VERSION)
27 RUN_PROGRAM =
28 SCRIPTS =
29 C_PROGRAMS = one_gaussian_bump error_analysis
30 CXX_PROGRAMS =
31 PROGRAMS = $(C_PROGRAMS) $(CXX_PROGRAMS)
32
33 # Define the source files that will be distributed in the tarball
34 SOURCE = *.c *.h COPYING Makefile README $(SCRIPTS)
35
36 # Define a list of object files needed to link PROGRAM.  We're setting
37 # things up here so that a program % depends on the object file %.o.
38 # For details on `foreach` and `eval`, see
39 #   http://www.gnu.org/software/make/manual/html_node/Foreach-Function.html
40 #   http://www.gnu.org/software/make/manual/html_node/Eval-Function.html
41 $(foreach PROGRAM, $(PROGRAMS), $(eval $(PROGRAM)_OBJECTS = $(PROGRAM).o))
42
43 # You can override a paricular case here with something like:
44 #hello_world_OBJECTS = hello_world.o utility_code.o
45
46 # You may want to link agains external libraries.  For example, to
47 # link against the system math library, use.
48 #   hello_world_LIBS = -lm
49 $(foreach PROGRAM, $(PROGRAMS), $(eval $(PROGRAM)_LIBS = -lm))
50
51 # Define useful programs (this makes it easy to swap in alternates)
52 CP = cp
53 GREP = grep
54 MKDIR = mkdir
55 RM = rm
56 TAR = tar
57
58 # Declare targets that do not generate files of the same name.
59 #   http://www.gnu.org/software/make/manual/html_node/Phony-Targets.html
60 .PHONY: all help clean dist run print- printvars
61
62 # target: all - the default target
63 # The target of the first rule is the default goal.  See:
64 #   http://www.gnu.org/software/make/manual/html_node/Rules.html
65 all: $(PROGRAMS)
66
67 # Dependency declarations
68 one_gaussian_bump_OBJECTS = one_gaussian_bump.o safe-strto.o
69 one_gaussian_bump.o safe-strto.o: config.h safe-strto.h
70
71 # target: help - display callable targets
72 # Use `grep` to search this file for target comments
73 help:
74         $(GREP) '^# target:' [Mm]akefile
75
76 # target: clean - remove automatically generated files
77 clean:
78         $(RM) -rf $(PROGRAMS) *.o *.data *.png $(RELEASE)*
79
80 # target: dist - generate a tarball packaging the source
81 # Here, we move the source into a temporary release directory, tar the
82 # release directory, and remove the release directory.
83 dist:
84         $(MKDIR) $(RELEASE)
85         $(CP) -r $(SOURCE) $(RELEASE)
86         $(TAR) -czf $(RELEASE).tar.gz $(RELEASE)
87         $(RM) -rf $(RELEASE)
88
89 # target: hello_world - compile the hello_world program
90 # Use GCC to link the program from object files.
91 # For an explanation of the static pattern rule syntax:
92 #   targets ...: target-pattern: prereq-patterns ...
93 # see
94 #   http://www.gnu.org/software/make/manual/html_node/Static-Usage.html
95 # For an explanation of $@, $^, and other special variables, see
96 #   http://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html
97 # For an explanation of .SECONDEXPANSION, see
98 #   http://www.gnu.org/software/make/manual/html_node/Secondary-Expansion.html
99 # `$$` escapes make-variable expansion for the first pass through the
100 # recipe.  See
101 #   http://www.gnu.org/software/make/manual/html_node/Variables-in-Recipes.html
102 #
103 # What's going on here?  During the read-in phase, Make expands the
104 # rule to
105 #   hello_world goodbye_world: % : $($(@)_OBJECTS)
106 # Because we're using .SECONDEXPANSION, Make expands the prerequisites
107 # again during the target-update phase.  If we're building
108 # hello_world, $(@) will expand to hello_world, and we'll have
109 #   hello_world goodbye_world: hello_world : $(hello_world_OBJECTS)
110 # as the variable expansion continues, we end up with
111 #   hello_world goodbye_world: hello_world : hello_world.o
112 # which is the final rule used to determine the prerequisites.
113 #
114 # The recipe expands to
115 #   cc  -o hello_world hello_world.o $(hello_world_LIBS)
116 # which expands to (if hello_world_LIBS was set to `-lm`)
117 #   cc  -o hello_world hello_world.o -lm
118 #
119 # Striking the right balance between "everything handled
120 # automatically" (i.e. "complicated") and "everything handled
121 # manually" (i.e. tedious) is difficult, and maybe this rule crosses
122 # the line.  The simpler alternative would be to define your own
123 # prerequisites for each program you wish to compile, and you're
124 # certainly allowed to go that route if you wish.
125 .SECONDEXPANSION:
126 $(C_PROGRAMS): % : $$($$(@)_OBJECTS)
127         $(CC) $(LDFLAGS) -o "$@" $^ $($(@)_LIBS)
128
129 .SECONDEXPANSION:
130 $(CXX_PROGRAMS): % : $$($$(*)_OBJECTS)
131         $(CXX) $(LDFLAGS) -o "$@" $^ $($(@)_LIBS)
132
133 # target: run - use the program for its intended purpose
134 # Here we just execute RUN_PROGRAM, but you could also use something
135 # like
136 #   run: $(RUN_PROGRAM) plot.gp
137 #     ./$(RUN_PROGRAM) > data
138 #     gnuplot plot.gp
139 # where plot.gp was a gnuplot script for plotting data generated by
140 # RUN_PROGRAM.
141 run: error.png phase.png energy.png
142
143 .SECONDEXPANSION:
144 error-%.data: $(PROGRAMS)
145         echo -e "# dt\terror" > "$@"
146         for dt in 0.00004 0.0002 0.001 0.005 0.025; do \
147                 echo -en "$$dt\t" >> "$@"; \
148                 ./one_gaussian_bump --time-step "$$dt" --$(@:error-%.data=%) | \
149                          ./error_analysis >> "$@"; \
150         done
151
152 error.png: error.gp error-euler.data error-midpoint.data error-rk4.data
153         gnuplot "$<" > "$@"
154
155 phase.data:
156         > "$@"
157         for E in 0.1 0.5 1 1.5 2 2.5 3 3.5 4 4.5 4.9 5 5.1 5.5; do \
158                 ./one_gaussian_bump --vx0-energy "$$E" >> "$@"; \
159                 echo >> "$@"; \
160                 echo >> "$@"; \
161         done
162
163 phase.png: phase.gp phase.data
164         gnuplot "$<" > "$@"
165
166 energy.png: energy.gp phase.data
167         gnuplot "$<" > "$@"
168
169 # Pattern rule for compiling object files from C++ source
170 # There is an implicit rule for this in GNU make
171 #   http://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html
172 # but I redefine it here for clarity.  For an explanation of the
173 #   %.o: %.cpp
174 # syntax, see
175 #   http://www.gnu.org/software/make/manual/html_node/Pattern-Intro.html
176 #
177 # CXX and CXXFLAGS have defaults defined by make
178 #   http://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html
179 # but they can also be come from the environment
180 #   http://www.gnu.org/software/make/manual/html_node/Environment.html
181 # for example, try
182 #    $ make CXX=/usr/local/bin/g++ CXXFLAGS=-Wall
183 %.o: %.cpp
184         $(CXX) $(CXXFLAGS) -c "$<"
185
186 # Pattern rule for compiling object files from C source
187 # The comments from the C++ rule above also apply here
188 %.o: %.c
189         $(CC) $(CFLAGS) -c "$<"
190
191 # target: print-% - display a variable value (e.g. print-PROGRAMS)
192 # Take some of the mystery out of variable manipulation.  For example,
193 #   $ make print-hello_world_OBJECTS
194 # The `@` suppresses recipe echoing.  See
195 #   http://www.gnu.org/software/make/manual/html_node/Echoing.html
196 # The `info` function acts like the shell `echo` command.  See
197 #   http://www.gnu.org/software/make/manual/html_node/Make-Control-Functions.html
198 # For an explanation of $* and other special variables, see
199 #   http://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html
200 print-%:
201         @$(info $* = $($*))
202
203 # target: printvars - display all non-default variables
204 # .VARIBALES holds a list of all global variables.  See
205 #   http://www.gnu.org/software/make/manual/html_node/Special-Variables.html
206 # The functions---`sort`, `if`, `filter-out`, `origin`, `warning`, and
207 # `value`--- are discussed, respectively, here:
208 #   http://www.gnu.org/software/make/manual/html_node/Text-Functions.html#index-sort-580
209 #   http://www.gnu.org/software/make/manual/html_node/Conditional-Functions.html
210 #   http://www.gnu.org/software/make/manual/html_node/Text-Functions.html#index-filter_002dout-577
211 #   http://www.gnu.org/software/make/manual/html_node/Origin-Function.html
212 #   http://www.gnu.org/software/make/manual/html_node/Value-Function.html
213 printvars:
214         @$(foreach V, \
215                 $(sort $(.VARIABLES)), \
216                 $(if \
217                         $(filter-out environment default automatic, \
218                                 $(origin $V)), \
219                         $(info $V=$($V) (origin: $(origin $V), value: $(value $V))) \
220                         ) \
221                 )