1 | package PIANOS; |
2 | |
3 | import PIANOS.datastructures.*; |
4 | import PIANOS.exceptions.*; |
5 | import PIANOS.io.*; |
6 | import PIANOS.io.ComputationalModelParser; |
7 | import PIANOS.generator.*; |
8 | |
9 | import java.util.*; |
10 | import java.io.*; |
11 | |
12 | /** The PIANOS main class. |
13 | Writes Fortran module files for a simulation program that matches the model description files given. |
14 | May be run on the command line with 8 or 1 parameters, see the PIANOS manual on the |
15 | CD-ROM or in the tar.gz for details. |
16 | See writeProgram for the Java way of running PIANOS. |
17 | */ |
18 | |
19 | public class Generator{ |
20 | private static ComputationalModel model = null; |
21 | private static DistributionFactory factory = null; |
22 | private static FortranWriter writer; |
23 | |
24 | //For testing... |
25 | public static void setModel(ComputationalModel modelToSet){ |
26 | model = modelToSet; |
27 | } |
28 | |
29 | /** The PIANOS main interface. Writes definitions.f90, input.f90, output.f90, proposal.f90, main.f90. |
30 | Note that when compiling the user will also need user_dist.f90 that contains the user-defined |
31 | distributions used in the model. |
32 | |
33 | @param userDistFileName the filename of the user-defined distributions file |
34 | @param modelFileName the filename of the model description file |
35 | @param initialValueFileName the filename of the simulation initial values file |
36 | @param simulationFileName the filename of the simulation parameters file (burn-in, thinning) |
37 | @param proposalFileName the filename of the proposal strategies and distributions file |
38 | @param updateFileName the filename of the update strategy and iterations file |
39 | @param toOutputFileName the filename of the parameters desired as output |
40 | @param lastValuesFileName the filename of the file where the last values are to be saved after the simulation |
41 | */ |
42 | public static void writeProgram(String userDistFileName, String modelFileName, |
43 | String initialValueFileName, String simulationFileName, String proposalFileName, |
44 | String updateFileName, String toOutputFileName, String lastValueFileName) |
45 | throws MissingFunctionException, SyntaxException, InvalidProposalException, IOException, |
46 | MissingDistributionException, IllegalParametersException, |
47 | InvalidModelException{ |
48 | |
49 | |
50 | |
51 | model = null; |
52 | factory = new DistributionFactory(userDistFileName); |
53 | |
54 | |
55 | model = ComputationalModelParser.readModel(modelFileName, initialValueFileName, |
56 | simulationFileName, proposalFileName, updateFileName, toOutputFileName, factory); |
57 | |
58 | System.out.print("Writing simulation program..."); |
59 | |
60 | |
61 | model.setLastValuesFileName(lastValueFileName); |
62 | |
63 | LinkedList<Variable> variables = model.getVariableList(); |
64 | |
65 | // DEBUG |
66 | /* |
67 | System.out.println("GENERATOR DEBUG: Generator.java: Global variable list from the parser"); |
68 | for (Variable v: variables) |
69 | System.out.println(v.getName()); |
70 | */ |
71 | |
72 | String callParameters = setCallParameters(model); |
73 | |
74 | Definitions.generateDefinitions(model); |
75 | |
76 | // DEBUG |
77 | /* |
78 | variables = model.getVariableList(); |
79 | System.out.println("GENERATOR DEBUG: Generator.java: Global variable list after Definitions"); |
80 | for (Variable v: variables) |
81 | System.out.println(v.getName()); |
82 | */ |
83 | |
84 | Input.generateInput(callParameters, model); |
85 | |
86 | // DEBUG |
87 | /* |
88 | variables = model.getVariableList(); |
89 | System.out.println("Generator.java: Global variable list after Input"); |
90 | for (Variable v: variables) |
91 | System.out.println(v.getName()); |
92 | */ |
93 | |
94 | Output.generateOutput(callParameters, model); |
95 | |
96 | // DEBUG |
97 | /* |
98 | variables = model.getVariableList(); |
99 | System.out.println("Generator.java: Global variable list after Output"); |
100 | for (Variable v: variables) |
101 | System.out.println(v.getName()); |
102 | */ |
103 | |
104 | Proposal.generateProposal(model); |
105 | |
106 | // DEBUG |
107 | /* |
108 | variables = model.getVariableList(); |
109 | System.out.println("Generator.java: Global variable list after Proposal"); |
110 | for (Variable v: variables) |
111 | System.out.println(v.getName()); |
112 | */ |
113 | |
114 | FortranMain.generateMain(callParameters, model); |
115 | |
116 | // DEBUG |
117 | /* |
118 | variables = model.getVariableList(); |
119 | System.out.println("GENERATOR DEBUG: Generator.java: Global variable list after FortranMain"); |
120 | for (Variable v: variables) |
121 | System.out.println(v.getName()); |
122 | */ |
123 | System.out.println("\t\t\t\t\t[OK]"); |
124 | System.out.println("The program completed successfully."); |
125 | } |
126 | |
127 | /** The PIANOS command-line interface. Writes definitions.f90, input.f90, output.f90, proposal.f90, main.f90. |
128 | Note that when compiling the user will also need user_dist.f90 that contains the user-defined |
129 | distributions used in the model. Command-line parameters specified in order |
130 | (choose first 8 or the last one which incorporates the 8): |
131 | |
132 | @param userDistFileName the filename of the user-defined distributions file |
133 | @param modelFileName the filename of the model description file |
134 | @param initialValueFileName the filename of the simulation initial values file |
135 | @param simulationFileName the filename of the simulation parameters file (burn-in, thinning) |
136 | @param proposalFileName the filename of the proposal strategies and distributions file |
137 | @param updateFileName the filename of the update strategy and iterations file |
138 | @param toOutputFileName the filename of the parameters desired as output |
139 | @param lastValuesFileName the filename of the file where the last values are to be saved after the simulation |
140 | @param allFilesFileName (mutually exclusive with the parameters above) |
141 | A file containing the file names of all the files specified above, in the format: <br> |
142 | <i>(begin of file)</i><br> |
143 | user distributions: relative/path/to/user_dist.f90 <br> |
144 | model: relative/path/to/modelFile<br> |
145 | initial values: relative/path/to/initialValuesFile<br> |
146 | simulation: relative/path/to/simulationFile<br> |
147 | proposals: relative/path/to/proposalsFile<br> |
148 | update strategy: relative/path/to/updateFile<br> |
149 | to output: relative/path/to/toOutputFile<br> |
150 | last values: relative/path/to/lastValuesFile<br> |
151 | <i>(end of file)</i><br> |
152 | The order of the identifier - file name pairs is arbitrary.<br> |
153 | The file may contain comments beginning with a hash '#'<br> |
154 | */ |
155 | public static void main(String[] args) throws SyntaxException, IOException{ |
156 | |
157 | model = null; |
158 | String userDistFileName = null; |
159 | String modelFileName = null; |
160 | String initialValueFileName = null; |
161 | String simulationFileName = null; |
162 | String proposalFileName = null; |
163 | String updateFileName = null; |
164 | String toOutputFileName = null; |
165 | String lastValuesFileName = null; |
166 | if (args.length != 8 && args.length != 1) { |
167 | System.out.println("Wrong number of command-line arguments. Expected: 1 or 8: either one file with the " + |
168 | "filenames (user_dist, model, initial values, simulation, proposal, update, to output, last values) " + |
169 | "or these filenames directly."); |
170 | System.exit(1); |
171 | } |
172 | if (args.length == 1){ |
173 | Scanner reader = null; |
174 | int linenum = 0; |
175 | try{ |
176 | reader = new Scanner(new File(args[0])); |
177 | } catch (FileNotFoundException fnf){ |
178 | fnf.printStackTrace(); |
179 | System.exit(1); |
180 | } |
181 | while (reader.hasNextLine()){ |
182 | String line = reader.nextLine().trim(); |
183 | linenum++; |
184 | if (line.length() <= 0 || line.charAt(0) == '#') |
185 | continue; |
186 | |
187 | if (line.startsWith("user distributions:")){ |
188 | line = line.substring(19).trim(); |
189 | userDistFileName = line; |
190 | } else if (line.startsWith("model:")){ |
191 | line = line.substring(6).trim(); |
192 | modelFileName = line; |
193 | } else if (line.startsWith("initial values:")){ |
194 | line = line.substring(15).trim(); |
195 | initialValueFileName = line; |
196 | } else if (line.startsWith("simulation:")){ |
197 | line = line.substring(11).trim(); |
198 | simulationFileName = line; |
199 | } else if (line.startsWith("proposals:")){ |
200 | line = line.substring(10).trim(); |
201 | proposalFileName = line; |
202 | } else if (line.startsWith("update strategy:")){ |
203 | line = line.substring(16).trim(); |
204 | updateFileName = line; |
205 | } else if (line.startsWith("to output:")){ |
206 | line = line.substring(10).trim(); |
207 | toOutputFileName = line; |
208 | } else if (line.startsWith("last values:")){ |
209 | line = line.substring(12).trim(); |
210 | lastValuesFileName = line; |
211 | } else{ |
212 | throw new SyntaxException("Invalid line in" + args[0] + ": (line "+ linenum + ")"); |
213 | } |
214 | } |
215 | |
216 | |
217 | } else { |
218 | userDistFileName = args[0]; |
219 | modelFileName = args[1]; |
220 | initialValueFileName = args[2]; |
221 | simulationFileName = args[3]; |
222 | proposalFileName = args[4]; |
223 | updateFileName = args[5]; |
224 | toOutputFileName = args[6]; |
225 | lastValuesFileName = args[7]; |
226 | } |
227 | if (userDistFileName == null) |
228 | throw new IOException("Cannot proceed without a good userDistFileName"); |
229 | if (modelFileName == null) |
230 | throw new IOException("Cannot proceed without a good modelFileName"); |
231 | if (initialValueFileName == null) |
232 | throw new IOException("Cannot proceed without a good initialValueFileName"); |
233 | if (simulationFileName == null) |
234 | throw new IOException("Cannot proceed without a good simulationFileName"); |
235 | if (proposalFileName == null) |
236 | throw new IOException("Cannot proceed without a good proposalFileName"); |
237 | if (updateFileName == null) |
238 | throw new IOException("Cannot proceed without a good updateFileName"); |
239 | if (toOutputFileName == null) |
240 | throw new IOException("Cannot proceed without a good toOutputFileName"); |
241 | if (lastValuesFileName == null) |
242 | throw new IOException("Cannot proceed without a good lastValuesFileName"); |
243 | |
244 | System.out.println("PIANOS using files:"); |
245 | System.out.println("------------------------"); |
246 | System.out.println("user distributions:\t\""+ userDistFileName +"\""); |
247 | System.out.println("model:\t\t\t\""+ modelFileName +"\""); |
248 | System.out.println("initial values:\t\t\""+ initialValueFileName +"\""); |
249 | System.out.println("simulation:\t\t\""+ simulationFileName +"\""); |
250 | System.out.println("proposals:\t\t\""+ proposalFileName +"\""); |
251 | System.out.println("update strategy:\t\""+ updateFileName +"\""); |
252 | System.out.println("to output:\t\t\""+ toOutputFileName +"\""); |
253 | System.out.println("last values:\t\t\""+ lastValuesFileName +"\""); |
254 | System.out.println("------------------------"); |
255 | try{ |
256 | writeProgram(userDistFileName, modelFileName, initialValueFileName, simulationFileName, |
257 | proposalFileName, updateFileName, toOutputFileName, lastValuesFileName); |
258 | } catch (Exception e){ |
259 | e.printStackTrace(); |
260 | System.exit(1); |
261 | } |
262 | } |
263 | |
264 | private static String setCallParameters(ComputationalModel model){ |
265 | String callParameters = ""; |
266 | LinkedList<Variable> variables = model.getVariableList(); |
267 | LinkedList<Entity> entities = model.getEntityList(); |
268 | boolean first = true; |
269 | |
270 | for (Variable variableIterator : variables){ |
271 | if(first){ |
272 | callParameters = variableIterator.getName(); |
273 | first = false; |
274 | } else |
275 | callParameters = callParameters + ", " + variableIterator.getName(); |
276 | |
277 | } |
278 | |
279 | for (Entity entityIterator : entities){ |
280 | variables = entityIterator.getVariableList(); |
281 | for (Variable variableIterator : variables){ |
282 | callParameters = callParameters + ", " + variableIterator.getName(); |
283 | } |
284 | } |
285 | |
286 | return callParameters; |
287 | } |
288 | |
289 | // introduce parameters |
290 | public static ArrayList<String> generateIntroduction(){ |
291 | |
292 | assert model != null; |
293 | |
294 | ArrayList<String> toWrite = new ArrayList<String>(); |
295 | |
296 | for (Variable v : model.getVariableList()){ |
297 | if (v.isInteger()) |
298 | toWrite.add("TYPE(variables_int), INTENT(INOUT) :: " + v.getName()); |
299 | else |
300 | toWrite.add("TYPE(variables_real), INTENT(INOUT) :: " + v.getName()); |
301 | } |
302 | |
303 | for (Entity e : model.getEntityList()){ |
304 | ArrayList<Variable> variables = new ArrayList<Variable>(e.getVariableList()); |
305 | for (Variable v : variables){ |
306 | if (v.isInteger()) |
307 | toWrite.add("TYPE(variables_int), INTENT(INOUT) :: " + v.getName()); |
308 | else |
309 | toWrite.add("TYPE(variables_real), INTENT(INOUT) :: " + v.getName()); |
310 | } |
311 | } |
312 | return toWrite; |
313 | } |
314 | } |
315 | |