1 | package PIANOS.generator; |
2 | import PIANOS.Generator; |
3 | import PIANOS.datastructures.*; |
4 | import PIANOS.exceptions.*; |
5 | import PIANOS.io.*; |
6 | import java.io.*; |
7 | import java.util.*; |
8 | |
9 | /** |
10 | * This class is used to generate the Fortran module "input". |
11 | */ |
12 | public class Input { |
13 | |
14 | /** Generates and writes the Fortran module input.f90. |
15 | * @param callParameters The correct parameter order to be used in subroutine headers |
16 | * @param model The model this simulation is related to |
17 | */ |
18 | public static void generateInput(String callParameters, ComputationalModel model) |
19 | throws IOException, SyntaxException{ |
20 | |
21 | FortranWriter out = new FortranWriter("input.f90"); |
22 | ArrayList <String> toWrite = new ArrayList<String>(); |
23 | |
24 | LinkedList<Variable> variables = model.getVariableList(); |
25 | LinkedList<Entity> entities = model.getEntityList(); |
26 | |
27 | |
28 | toWrite.add("MODULE input"); |
29 | toWrite.add("USE definitions"); |
30 | toWrite.add("IMPLICIT NONE"); |
31 | toWrite.add(""); |
32 | toWrite.add("CONTAINS"); |
33 | toWrite.addAll(generateReadData(variables, entities, callParameters)); |
34 | toWrite.addAll(generateSetInitialValues(model.getInitialValueFile(), variables, entities, |
35 | callParameters)); |
36 | toWrite.addAll(generateReadSpatial(model.getNeighbourCount(), entities)); |
37 | toWrite.add(""); |
38 | toWrite.add("END MODULE input"); |
39 | out.write(toWrite); |
40 | } |
41 | |
42 | // generate the subroutine read_data |
43 | private static ArrayList<String> generateReadData(LinkedList<Variable> variables, |
44 | LinkedList<Entity> entities, String callParameters) throws SyntaxException{ |
45 | ArrayList<String> toWrite = new ArrayList<String>(); |
46 | toWrite.add("SUBROUTINE read_data("+ callParameters +")"); |
47 | toWrite.add("! This subroutine reads the data."); |
48 | toWrite.add("IMPLICIT NONE"); |
49 | toWrite.add(""); |
50 | // introduce the parameters. |
51 | toWrite.addAll(Generator.generateIntroduction()); |
52 | toWrite.add(""); |
53 | toWrite.add("INTEGER :: i, j, temp, next, iostatus"); |
54 | toWrite.add("INTEGER, PARAMETER :: dp=SELECTED_REAL_KIND(8, 0)"); |
55 | toWrite.add("REAL (KIND=dp), ALLOCATABLE, DIMENSION(:) :: datafile"); |
56 | toWrite.add("INTEGER, ALLOCATABLE, DIMENSION(:) :: nexts, missingfile"); |
57 | toWrite.add(""); |
58 | toWrite.addAll(readEntityData(entities)); |
59 | toWrite.add(""); |
60 | toWrite.add("END SUBROUTINE read_data"); |
61 | toWrite.add(""); |
62 | return toWrite; |
63 | } |
64 | |
65 | /* Helper methods for generateReadData |
66 | */ |
67 | // read all entities' data |
68 | private static ArrayList<String> readEntityData(LinkedList<Entity> entities) |
69 | throws SyntaxException{ |
70 | ArrayList<String> toWrite = new ArrayList<String>(); |
71 | for (int i = 0; i < entities.size(); ++i) { |
72 | if(entities.get(i).getDataFile() == null) continue; |
73 | |
74 | toWrite.addAll(readDataFile(entities.get(i))); |
75 | } |
76 | return toWrite; |
77 | } |
78 | |
79 | // return correct names for datafile_1 and datafile_missing |
80 | private static String[] getNames(String fname){ |
81 | String[] names = new String[2]; |
82 | |
83 | if (fname.indexOf('.') < 0) |
84 | names[0] = fname + "_1.txt"; |
85 | else |
86 | names[0] = fname.substring(0, fname.indexOf('.')) + "_1.txt"; |
87 | |
88 | if (fname.indexOf('.') < 0) |
89 | names[1] = fname + "_missing.txt"; |
90 | else |
91 | names[1] = fname.substring(0, fname.indexOf('.')) + "_missing.txt"; |
92 | |
93 | return names; |
94 | } |
95 | |
96 | private static ArrayList<String> readDataFile(Entity e) throws SyntaxException{ |
97 | ArrayList<String> toWrite = new ArrayList<String>(); |
98 | String fname = e.getDataFile(); |
99 | String data; |
100 | String missing; |
101 | String[] names = getNames(fname); |
102 | data = names[0]; |
103 | missing = names[1]; |
104 | |
105 | toWrite.add("OPEN(UNIT=16, FILE= '"+data+"', STATUS='old', IOSTAT=iostatus)"); |
106 | toWrite.addAll(ioErrorCheck("'"+data+"'", true)); |
107 | toWrite.add("OPEN(UNIT=17, FILE= '"+missing+"', STATUS='old', IOSTAT=iostatus)"); |
108 | toWrite.addAll(ioErrorCheck("'"+missing+"'", true)); |
109 | |
110 | // file availability checked, open confirmed |
111 | // reading left |
112 | |
113 | ArrayList<Variable> datavars = new ArrayList<Variable>(); |
114 | for (Variable var: e.getVariableList()){ |
115 | if (var.isData()) datavars.add(var); |
116 | } |
117 | |
118 | if (datavars.size() == 1 && e.isMatrix()) |
119 | toWrite.addAll(readMatrixDataFor(data, missing, datavars.get(0))); |
120 | else if (!e.isMatrix()) |
121 | toWrite.addAll(readDataFileFor(data, missing, datavars)); |
122 | else // matrix and multiple variables |
123 | throw new SyntaxException("multiple data variables and matrix form entity!"); |
124 | // this HAS been checked in the parser |
125 | |
126 | // TODO: |
127 | // If the array has more stuff |
128 | // than one, generate a reading loop of the entity's size |
129 | // and read indices to the variables according to the proto_model_spat. |
130 | // if it has only one entry, read the whole file into the |
131 | // variable array (easy) |
132 | |
133 | toWrite.add("CLOSE(17)"); |
134 | toWrite.add("CLOSE(16)"); |
135 | toWrite.add(""); |
136 | |
137 | return toWrite; |
138 | } |
139 | |
140 | private static ArrayList<String> readMatrixDataFor(String data, String missing, Variable var){ |
141 | ArrayList<String> toWrite = new ArrayList<String>(); |
142 | // ok, we have a two-dimensional variable. |
143 | // this has been checked for validity in the parser. |
144 | int ySize = var.getEntity().getYCoordinate().getSize(); |
145 | int xSize = var.getEntity().getXCoordinate().getSize(); |
146 | toWrite.add("next = 1"); |
147 | toWrite.add(""); |
148 | toWrite.add("DO i = 1, "+ ySize); |
149 | toWrite.add(""); |
150 | toWrite.add("READ(UNIT=16, FMT=*, IOSTAT=iostatus) "+ |
151 | var.getName() + " % two_dim(i, :) % value"); |
152 | toWrite.addAll(ioErrorCheck("'"+data+"'")); |
153 | toWrite.add("DO j = 1, "+ xSize); |
154 | toWrite.add(""); |
155 | toWrite.add("IF (j == "+ xSize + ") THEN"); |
156 | toWrite.add("READ(17, FMT='(I2)', IOSTAT=iostatus) temp"); |
157 | toWrite.add("ELSE"); |
158 | toWrite.add("READ(17, FMT='(I2)', ADVANCE='NO', IOSTAT=iostatus) temp"); |
159 | toWrite.add("END IF"); |
160 | toWrite.addAll(verboseIOError("'"+missing+"'")); |
161 | toWrite.add("IF (temp == 1) THEN"); |
162 | toWrite.add(var.getName() + " % two_dim(i, j) % is_data = .FALSE."); |
163 | toWrite.add(var.getName() + " % two_dim_missing(next, 1) = i"); |
164 | toWrite.add(var.getName() + " % two_dim_missing(next, 2) = j"); |
165 | toWrite.add("next = next + 1"); |
166 | toWrite.add("END IF"); |
167 | toWrite.add("END DO"); |
168 | toWrite.add("END DO"); |
169 | return toWrite; |
170 | } |
171 | |
172 | private static ArrayList<String> readDataFileFor(String data, String missing, |
173 | ArrayList<Variable> datavars){ |
174 | ArrayList<String> toWrite = new ArrayList<String>(); |
175 | // the variables are all one-dimensional. let's see how long they are. |
176 | int size = datavars.get(0).getEntity().getSize(); |
177 | toWrite.add("IF (ALLOCATED(datafile)) THEN"); |
178 | toWrite.add("DEALLOCATE(datafile)"); |
179 | toWrite.add("DEALLOCATE(missingfile)"); |
180 | toWrite.add("DEALLOCATE(nexts)"); |
181 | toWrite.add("END IF"); |
182 | toWrite.add("ALLOCATE(datafile(1:"+datavars.get(0).getEntity().getLineLength()+ "))"); |
183 | toWrite.add("ALLOCATE(missingfile(1:"+datavars.get(0).getEntity().getLineLength()+ "))"); |
184 | toWrite.add(""); |
185 | toWrite.add("ALLOCATE(nexts(1:"+datavars.size()+"))"); |
186 | toWrite.add("nexts = 1"); |
187 | toWrite.add(""); |
188 | toWrite.add("DO i = 1, "+ size); |
189 | toWrite.add("READ(16, FMT=*, IOSTAT=iostatus) datafile(:)"); |
190 | toWrite.addAll(verboseIOError("'"+data+"'")); |
191 | for (Variable var: datavars){ |
192 | if (var.isInteger()) |
193 | toWrite.add(var.getName() + " % one_dim(i) % value = NINT(datafile("+ |
194 | var.getColumn() +"))"); |
195 | else |
196 | toWrite.add(var.getName() + " % one_dim(i) % value = (datafile("+ |
197 | var.getColumn() +"))"); |
198 | } |
199 | toWrite.add(""); |
200 | toWrite.add("READ(17, FMT=*, IOSTAT=iostatus) missingfile(:)"); |
201 | toWrite.addAll(verboseIOError("'"+missing+"'")); |
202 | for (int index = 0; index < datavars.size(); ++index){ |
203 | Variable var = datavars.get(index); |
204 | toWrite.add("IF(missingfile("+ var.getColumn()+ ") == 1) THEN"); |
205 | toWrite.add(var.getName() + " % one_dim(i) % is_data = .FALSE."); |
206 | toWrite.add(var.getName() + " % one_dim_missing(nexts("+ (index + 1) +")) = i"); |
207 | toWrite.add("nexts("+ (index +1) +") = nexts("+ (index +1) + ") +1"); |
208 | // ok, that looks horrible... |
209 | |
210 | toWrite.add("END IF"); |
211 | toWrite.add(""); |
212 | } |
213 | toWrite.add("END DO"); |
214 | return toWrite; |
215 | } |
216 | |
217 | |
218 | |
219 | // generate the subroutine set_initial_values |
220 | private static ArrayList<String> generateSetInitialValues(String file, LinkedList<Variable> |
221 | variables, LinkedList<Entity> entities, String callParameters) { |
222 | ArrayList<String> toWrite = new ArrayList<String>(); |
223 | toWrite.add(""); |
224 | toWrite.add("SUBROUTINE set_initial_values("+callParameters+")"); |
225 | toWrite.add("! This subroutine reads the initial values of the parameters and sets them."); |
226 | toWrite.add("IMPLICIT NONE"); |
227 | toWrite.add(""); |
228 | toWrite.addAll(Generator.generateIntroduction()); |
229 | toWrite.add(""); |
230 | toWrite.add("CHARACTER(LEN=100) :: line, var_name"); |
231 | // 100 should not be too shabby. This is the maximum header length. Has no |
232 | // effect on initial value definition lines. |
233 | toWrite.add("! HARD-CODED: the filename of the initial values file"); |
234 | toWrite.add("CHARACTER(LEN=*), PARAMETER :: initial_values_filename='"+file+"'"); |
235 | toWrite.add("INTEGER :: i, j, linenum"); |
236 | toWrite.add("INTEGER :: vertical_start, vertical_end, horizontal_start, horizontal_end"); |
237 | toWrite.add("INTEGER :: ind, iostatus"); |
238 | toWrite.addAll(readInitialValues("initial_values_filename", variables, entities)); |
239 | toWrite.add("END SUBROUTINE set_initial_values"); |
240 | toWrite.add(""); |
241 | return toWrite; |
242 | } |
243 | |
244 | /* Helper methods for generateSetInitialValues |
245 | * |
246 | */ |
247 | |
248 | // do the main work of initial values reading |
249 | private static ArrayList<String> readInitialValues(String file, LinkedList<Variable> variables, |
250 | LinkedList<Entity> entities){ |
251 | ArrayList<String> toWrite = new ArrayList<String>(); |
252 | toWrite.add("OPEN(UNIT=16, FILE=initial_values_filename, STATUS='old', IOSTAT=iostatus)"); |
253 | toWrite.addAll(ioErrorCheck(file, true)); |
254 | toWrite.add("READ (16, FMT='(A)', IOSTAT=iostatus) line"); |
255 | toWrite.addAll(verboseIOError(file)); |
256 | toWrite.add("linenum = 1"); |
257 | toWrite.add("IF (.NOT. line == '?? initial values') THEN"); |
258 | toWrite.add("WRITE(*, *) 'Expected initial values legend in ' , initial_values_filename"); |
259 | toWrite.add("WRITE(*, *) 'on line 1 not found. Quitting.'"); |
260 | toWrite.add("CLOSE(16)"); |
261 | toWrite.add("STOP"); |
262 | toWrite.add("END IF"); |
263 | toWrite.add(""); |
264 | toWrite.add("DO WHILE (.TRUE.)"); |
265 | toWrite.add("READ (16, FMT='(A)', IOSTAT=iostatus) line"); |
266 | toWrite.add(""); |
267 | toWrite.add("linenum = linenum + 1"); |
268 | toWrite.add(""); |
269 | toWrite.add("IF (iostatus < 0) THEN"); |
270 | toWrite.add("RETURN"); // finished reading |
271 | toWrite.add("ELSE IF (iostatus > 0) THEN"); |
272 | toWrite.add("WRITE (*, *) 'Could not read ', initial_values_filename,', line ', linenum, '. Exiting program.'"); |
273 | toWrite.add("STOP"); |
274 | toWrite.add("END IF"); |
275 | toWrite.add(""); |
276 | toWrite.addAll(readVariableSpecific(variables, entities)); |
277 | toWrite.add("END DO ! Ends the file-reading loop."); |
278 | toWrite.add("CLOSE(16)"); |
279 | return toWrite; |
280 | } |
281 | |
282 | private static ArrayList<String> readVariableSpecific(LinkedList<Variable> variables, |
283 | LinkedList<Entity> entities){ |
284 | ArrayList<String> toWrite = new ArrayList<String>(); |
285 | toWrite.add("line = TRIM(line)"); |
286 | toWrite.add(""); |
287 | toWrite.add("IF (LEN_TRIM(line) == 0) THEN"); |
288 | toWrite.add("CYCLE"); |
289 | toWrite.add("END IF"); |
290 | toWrite.add("IF (line(1:1) == '#') THEN"); |
291 | toWrite.add("CYCLE"); |
292 | toWrite.add("END IF"); |
293 | toWrite.add(""); |
294 | toWrite.add("! now we have a \"? alpha 1:something\" at hand"); |
295 | // This has been checked through and through in the parser. Doing a bit more freely here. |
296 | toWrite.add("IF (line(1:1) /= '?') THEN"); |
297 | toWrite.add("WRITE(*, *) 'Expected legend on line ', linenum, 'not found, quitting.'"); |
298 | toWrite.add("STOP"); |
299 | toWrite.add("END IF"); |
300 | toWrite.add(""); |
301 | toWrite.add("line = line (3:LEN_TRIM(line))"); |
302 | toWrite.add("ind = INDEX(line, ' ')"); |
303 | toWrite.add("IF (ind <= 1) THEN"); |
304 | toWrite.add("var_name = line"); |
305 | toWrite.add("ELSE"); |
306 | toWrite.add("var_name = TRIM(line(1:ind))"); |
307 | toWrite.add("END IF"); |
308 | toWrite.add("line = line(LEN_TRIM(var_name)+2:LEN_TRIM(line))"); |
309 | toWrite.add("! trim the variable name out"); |
310 | toWrite.add("! and the trailing space"); |
311 | toWrite.add("vertical_start = 0"); |
312 | toWrite.add("vertical_end = 0"); |
313 | toWrite.add("horizontal_start = 0"); |
314 | toWrite.add("horizontal_end = 0"); |
315 | toWrite.add("SELECT CASE (var_name)"); |
316 | toWrite.addAll(generateInitialValueCases(variables, entities)); |
317 | toWrite.add("END SELECT"); |
318 | toWrite.add(""); |
319 | return toWrite; |
320 | } |
321 | |
322 | private static ArrayList<String> generateInitialValueCases(LinkedList<Variable> variables, |
323 | LinkedList<Entity> entities){ |
324 | ArrayList<String> toWrite = new ArrayList<String>(); |
325 | |
326 | for (Variable v : variables){ |
327 | if (v.isFunctional()) |
328 | continue; |
329 | if (v.isData() && v.getMissingValueCount() == 0) |
330 | continue; |
331 | |
332 | toWrite.addAll(initialValueCaseFor(v)); |
333 | toWrite.add("! end of case "+v.getName()); |
334 | } |
335 | for (Entity e: entities){ |
336 | for (Variable v: e.getVariableList()){ |
337 | if (v.isFunctional()) |
338 | continue; |
339 | if (v.isData() && v.getMissingValueCount() == 0) |
340 | continue; |
341 | |
342 | toWrite.addAll(initialValueCaseFor(v)); |
343 | toWrite.add("! end of case "+v.getName()); |
344 | } |
345 | } |
346 | toWrite.add(""); |
347 | return toWrite; |
348 | } |
349 | |
350 | private static ArrayList<String> initialValueCaseFor(Variable var){ |
351 | ArrayList<String> toWrite = new ArrayList<String>(); |
352 | toWrite.add(""); |
353 | if (var.getEntity() == null) |
354 | toWrite.addAll(initialValueCaseGlobal(var)); |
355 | else if (!var.getEntity().isMatrix()) |
356 | toWrite.addAll(initialValueCaseOneDim(var)); |
357 | else |
358 | toWrite.addAll(initialValueCaseTwoDim(var)); |
359 | return toWrite; |
360 | } |
361 | |
362 | private static ArrayList<String> initialValueCaseGlobal(Variable var){ |
363 | ArrayList<String> toWrite = new ArrayList<String>(); |
364 | String name = var.getName(); |
365 | toWrite.add(""); |
366 | toWrite.add("CASE ('"+name +"')"); |
367 | toWrite.add("READ(16, *, IOSTAT=iostatus) "+ name + " % one_dim (1) % value"); |
368 | toWrite.add("linenum = linenum + 1"); |
369 | toWrite.add(""); |
370 | toWrite.add("IF (iostatus < 0) THEN"); |
371 | toWrite.add("WRITE (*, *) 'Ran out of data when reading ', " + |
372 | "initial_values_filename, '. Exiting program.'"); |
373 | toWrite.add("STOP"); |
374 | toWrite.add("ELSE IF (iostatus > 0) THEN"); |
375 | toWrite.add("WRITE (*, *) 'Could not read ', initial_values_filename, ', line '," + |
376 | " linenum, '. Exiting program.'"); |
377 | toWrite.add("STOP"); |
378 | toWrite.add("END IF"); |
379 | toWrite.add(""); |
380 | toWrite.add(name +" % one_dim(1) % initial_value_set = .TRUE."); |
381 | toWrite.add(""); |
382 | return toWrite; |
383 | } |
384 | |
385 | private static ArrayList<String> initialValueCaseOneDim(Variable var){ |
386 | ArrayList<String> toWrite = new ArrayList<String>(); |
387 | String name = var.getName(); |
388 | toWrite.add(""); |
389 | toWrite.add("CASE ('"+name +"')"); |
390 | toWrite.add("! HARD-CODED:"+ name +" is one-dimensional so we consider the"); |
391 | toWrite.add("! format \""+ name + " 1:20\" for example"); |
392 | toWrite.add("ind = INDEX(line, ':')"); |
393 | toWrite.add("line(ind:ind) = ' '"); |
394 | toWrite.add("READ (line, FMT=*, IOSTAT=iostatus) vertical_start, vertical_end"); |
395 | toWrite.addAll(veryVerboseIOError("linenum", "initial_values_filename")); |
396 | toWrite.add("DO i = vertical_start, vertical_end"); |
397 | toWrite.add("READ(16, *, IOSTAT=iostatus) "+ name +" % one_dim (i) % value"); |
398 | toWrite.add("linenum = linenum +1"); |
399 | toWrite.addAll(veryVerboseIOError("linenum", "initial_values_filename")); |
400 | toWrite.add(name +" % one_dim(i) % initial_value_set = .TRUE."); |
401 | toWrite.add(""); |
402 | toWrite.add("END DO"); |
403 | return toWrite; |
404 | } |
405 | |
406 | private static ArrayList<String> initialValueCaseTwoDim(Variable var){ |
407 | ArrayList<String> toWrite = new ArrayList<String>(); |
408 | String name = var.getName(); |
409 | toWrite.add(""); |
410 | toWrite.add("CASE ('"+name +"')"); |
411 | toWrite.add("! HARD-CODED: "+ name +" is two-dimensional so we consider the"); |
412 | toWrite.add("! format \""+ name +" 1:20 5:400\" for example"); |
413 | toWrite.add("DO i = 1, 2"); |
414 | toWrite.add("ind = INDEX(line, ':')"); |
415 | toWrite.add("line(ind:ind) = ' '"); |
416 | toWrite.add("END DO"); |
417 | toWrite.add(""); |
418 | toWrite.add("READ (line, FMT=*, IOSTAT=iostatus) vertical_start, vertical_end, horizontal_start, horizontal_end"); |
419 | toWrite.add(""); |
420 | toWrite.addAll(veryVerboseIOError("linenum", "initial_values_filename")); |
421 | toWrite.add("DO i = vertical_start, vertical_end"); |
422 | toWrite.add("READ(16, *, IOSTAT=iostatus) "+ name +" % two_dim (i, horizontal_start:horizontal_end) % value"); |
423 | toWrite.add("linenum = linenum +1"); |
424 | toWrite.addAll(veryVerboseIOError("linenum", "initial_values_filename")); |
425 | toWrite.add(name +" % two_dim(i, horizontal_start:horizontal_end) % initial_value_set = .TRUE."); |
426 | toWrite.add(""); |
427 | toWrite.add("END DO"); |
428 | //TODO: UNDER CONSTRUCTION |
429 | return toWrite; |
430 | } |
431 | |
432 | // generate the subroutine read_spatial |
433 | private static ArrayList<String> generateReadSpatial(int neighbours, |
434 | LinkedList<Entity> entities) { |
435 | ArrayList<String> toWrite = new ArrayList<String>(); |
436 | Entity spatial = null; |
437 | for (int i = 0; i < entities.size(); ++i){ |
438 | if (entities.get(i).isSpatial()){ |
439 | spatial = entities.get(i); |
440 | break; |
441 | } |
442 | } |
443 | if (spatial == null){ |
444 | // There are no spatial entities in this model |
445 | return toWrite; |
446 | } |
447 | int size = spatial.getSize(); |
448 | toWrite.add(""); |
449 | toWrite.add("SUBROUTINE set_spatial(spatial)"); |
450 | toWrite.add("! sets the array describing neighbour relations"); |
451 | toWrite.add("! according to the adjacency matrix file."); |
452 | toWrite.add("IMPLICIT NONE"); |
453 | toWrite.add(""); |
454 | toWrite.add("! HARD-CODED: the dimensions (see main)"); |
455 | |
456 | toWrite.add("INTEGER, DIMENSION("+ size +", "+ (neighbours +1) +"), INTENT(OUT) :: spatial"); |
457 | toWrite.add("INTEGER, DIMENSION("+size +", "+ size +") :: matrix"); |
458 | toWrite.add("INTEGER :: iostatus, i, j, num"); |
459 | toWrite.add("CHARACTER (LEN=*), PARAMETER :: filename='"+ spatial.getSpatialMatrixFile() +"'"); |
460 | toWrite.add(""); |
461 | toWrite.add("! HARD-CODED: the name of the adjacency matrix file "); |
462 | toWrite.add(""); |
463 | toWrite.add("! Reads in the adjacency matrix."); |
464 | toWrite.add(""); |
465 | toWrite.add("OPEN(UNIT=16, FILE=filename, STATUS='old', IOSTAT=iostatus)"); |
466 | toWrite.addAll(ioErrorCheck("filename", true)); |
467 | toWrite.add("DO i=1, "+ size); |
468 | toWrite.add("READ(16, FMT=*, IOSTAT=iostatus) matrix(i, :)"); |
469 | toWrite.addAll(ioErrorCheck("filename")); |
470 | toWrite.add("END DO"); |
471 | toWrite.add(""); |
472 | toWrite.add("DO i = 1, "+ size); |
473 | toWrite.add("num = 0"); |
474 | toWrite.add("DO j = 1, "+ size); |
475 | toWrite.add("IF (matrix(i, j) == 1) THEN"); |
476 | toWrite.add("IF (num > "+ neighbours +") THEN"); |
477 | toWrite.add("WRITE (*, *) 'More neighbours than expected for (', i, j, '), exiting.'"); |
478 | toWrite.add("STOP"); |
479 | toWrite.add("END IF"); |
480 | toWrite.add("spatial(i, num + 2) = j"); |
481 | toWrite.add("num = num + 1"); |
482 | toWrite.add("END IF"); |
483 | toWrite.add("END DO"); |
484 | toWrite.add("spatial(i, 1) = num"); |
485 | toWrite.add("END DO"); |
486 | toWrite.add(""); |
487 | toWrite.add("END SUBROUTINE set_spatial"); |
488 | toWrite.add(""); |
489 | return toWrite; |
490 | } |
491 | |
492 | /* General help methods |
493 | */ |
494 | |
495 | // checks for errors and prints a very verbose io error message |
496 | private static ArrayList<String> veryVerboseIOError(String linevar, String fname){ |
497 | ArrayList<String> toWrite = new ArrayList<String>(); |
498 | toWrite.add(""); |
499 | toWrite.add("IF (iostatus < 0) THEN"); |
500 | toWrite.add("WRITE (*, *) 'Ran out of data when reading line ', "+ linevar +", ' in ', "+ fname +", '. Exiting program.'"); |
501 | toWrite.add("STOP"); |
502 | toWrite.add("ELSE IF (iostatus > 0) THEN"); |
503 | toWrite.add("WRITE (*, *) 'Could not read ', "+ fname +", ', line ', "+ linevar +", '. Exiting program.'"); |
504 | toWrite.add("STOP"); |
505 | toWrite.add("END IF"); |
506 | toWrite.add(""); |
507 | return toWrite; |
508 | } |
509 | |
510 | // checks for errors and prints a verbose io error message |
511 | private static ArrayList<String> verboseIOError(String fname){ |
512 | ArrayList<String> toWrite = new ArrayList<String>(); |
513 | toWrite.add(""); |
514 | toWrite.add("IF (iostatus < 0) THEN"); |
515 | toWrite.add("WRITE (*, *) 'Ran out of data when reading ', "+ fname +", '. Exiting program.'"); |
516 | toWrite.add("STOP"); |
517 | toWrite.add("ELSE IF (iostatus > 0) THEN"); |
518 | toWrite.add("WRITE (*, *) 'Could not read ', "+ fname +", '. Exiting program.'"); |
519 | toWrite.add("STOP"); |
520 | toWrite.add("END IF"); |
521 | toWrite.add(""); |
522 | return toWrite; |
523 | } |
524 | |
525 | /** runs ioErrorCheck with fname and false. |
526 | */ |
527 | private static ArrayList<String> ioErrorCheck(String fname){ |
528 | return ioErrorCheck(fname, false); |
529 | } |
530 | /** generate: check if iostatus is wrong, and print an error about fname |
531 | * if open == false, say that could not read, if open == true, say could not open |
532 | **/ |
533 | private static ArrayList<String> ioErrorCheck(String fname, boolean open){ |
534 | ArrayList<String> toWrite = new ArrayList<String>(); |
535 | toWrite.add(""); |
536 | toWrite.add("IF (iostatus /= 0) THEN"); |
537 | if (open) |
538 | toWrite.add("WRITE (*, *) 'Could not open ', "+fname+", '. Exiting program.'"); |
539 | else |
540 | toWrite.add("WRITE (*, *) 'Could not read ', "+fname+", '. Exiting program.'"); |
541 | toWrite.add("STOP"); |
542 | toWrite.add("END IF"); |
543 | toWrite.add(""); |
544 | return toWrite; |
545 | } |
546 | } |