| 1 | package PIANOS.generator; |
| 2 | import PIANOS.io.*; |
| 3 | import PIANOS.datastructures.*; |
| 4 | import PIANOS.exceptions.*; |
| 5 | import PIANOS.*; |
| 6 | import java.util.*; |
| 7 | import java.io.*; |
| 8 | |
| 9 | public class FortranMain { |
| 10 | public static void generateMain(String callParameters, ComputationalModel model) |
| 11 | throws IOException, InvalidModelException, IllegalParametersException, |
| 12 | MissingFunctionException { |
| 13 | |
| 14 | ArrayList<String> result = new ArrayList<String>(); |
| 15 | LinkedList<Variable> variables = model.getVariableList(); |
| 16 | LinkedList<Entity> entities = model.getEntityList(); |
| 17 | |
| 18 | // Set the topologicalList in Acceptation |
| 19 | |
| 20 | ArrayList<Variable> allVariables = new ArrayList<Variable>(variables); |
| 21 | |
| 22 | for (Entity entity : entities) { |
| 23 | allVariables.addAll(entity.getVariableList()); |
| 24 | } |
| 25 | |
| 26 | Acceptation.setTopologicalList(allVariables); |
| 27 | |
| 28 | |
| 29 | // NOTE: callParameters are now set in a separate method before |
| 30 | // any generation. That is because they are needed in |
| 31 | // other modules in addition to Main. |
| 32 | |
| 33 | result.add("PROGRAM main"); |
| 34 | result.add("USE definitions"); |
| 35 | result.add("USE input"); |
| 36 | result.add("USE output"); |
| 37 | result.add("USE proposal"); |
| 38 | result.add("USE user_dist"); |
| 39 | result.add("IMPLICIT NONE "); |
| 40 | result.add(""); |
| 41 | |
| 42 | |
| 43 | //the variables in the model and their type (real or integer) |
| 44 | for (Variable variableIterator : variables){ |
| 45 | if (variableIterator.isInteger()) |
| 46 | result.add("TYPE(variables_int) :: " + variableIterator.getName()); |
| 47 | else |
| 48 | result.add("TYPE(variables_real) :: " + variableIterator.getName()); |
| 49 | } |
| 50 | |
| 51 | for (Entity entityIterator : entities){ |
| 52 | variables = entityIterator.getVariableList(); |
| 53 | for (Variable variableIterator : variables){ |
| 54 | if (variableIterator.isInteger()) |
| 55 | result.add("TYPE(variables_int) :: " + variableIterator.getName()); |
| 56 | else |
| 57 | result.add("TYPE(variables_real) :: " + variableIterator.getName()); |
| 58 | } |
| 59 | } |
| 60 | result.add(""); |
| 61 | |
| 62 | |
| 63 | result.add("CHARACTER(LEN=*), PARAMETER :: output_filename='" + model.getOutputFile() + |
| 64 | "', summary_filename='" + model.getSummaryFile() + "'"); |
| 65 | |
| 66 | result.add("INTEGER :: i"); |
| 67 | result.add("INTEGER, PARAMETER :: multiplier=100"); |
| 68 | result.add("INTEGER, PARAMETER :: thinning=" + model.getThinning() + ", burn_in=" + |
| 69 | model.getBurnIn()); |
| 70 | result.add("INTEGER :: status, progress"); |
| 71 | |
| 72 | Entity spatial = null; |
| 73 | |
| 74 | for (int i = 0; i < entities.size(); ++i){ |
| 75 | if (entities.get(i).isSpatial()){ |
| 76 | spatial = entities.get(i); |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | if (spatial != null){ |
| 81 | int size = spatial.getSize(); |
| 82 | |
| 83 | result.add("INTEGER, DIMENSION("+ size +", "+ (model.getNeighbourCount() +1) + |
| 84 | ") :: spatial"); |
| 85 | |
| 86 | } |
| 87 | result.add("CALL random_init()"); |
| 88 | |
| 89 | variables = model.getVariableList(); |
| 90 | for (Variable variableIterator : variables){ |
| 91 | result.add("ALLOCATE(" + variableIterator.getName() + " % one_dim(1:1))"); |
| 92 | if(!variableIterator.isFunctional()){ |
| 93 | result.add("ALLOCATE(" + variableIterator.getName() + " % buffer(multiplier))"); |
| 94 | result.add(variableIterator.getName() + " % one_dim % successful_changes = 0"); |
| 95 | result.add(variableIterator.getName() + " % buffer_index = -1"); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | for (Entity entityIterator : entities){ |
| 100 | variables = entityIterator.getVariableList(); |
| 101 | |
| 102 | if (!entityIterator.isMatrix()){ |
| 103 | for (Variable variableIterator : variables){ |
| 104 | result.add("ALLOCATE(" + variableIterator.getName() + " % one_dim(1:" + |
| 105 | entityIterator.getSize() + "))"); |
| 106 | |
| 107 | if (variableIterator.isFunctional()) |
| 108 | continue; |
| 109 | if (variableIterator.isData() && variableIterator.getMissingValueCount() == 0) |
| 110 | continue; |
| 111 | if (variableIterator.isData() && variableIterator.getMissingValueCount() > 0){ |
| 112 | |
| 113 | result.add("ALLOCATE(" + variableIterator.getName() + |
| 114 | " % one_dim_missing("+ variableIterator.getMissingValueCount() +"))"); |
| 115 | } |
| 116 | |
| 117 | result.add("ALLOCATE(" + variableIterator.getName() + "% buffer(multiplier * " |
| 118 | + entityIterator.getSize() + "))"); |
| 119 | result.add(variableIterator.getName() + " % one_dim % successful_changes = 0"); |
| 120 | result.add(variableIterator.getName() + " % buffer_index = -1"); |
| 121 | } |
| 122 | |
| 123 | }else{ |
| 124 | for (Variable variableIterator : variables){ |
| 125 | result.add("ALLOCATE(" + variableIterator.getName() + " % two_dim(1:" + |
| 126 | entityIterator.getYCoordinate().getSize() |
| 127 | + ", 1:" + entityIterator.getXCoordinate().getSize() + "))"); |
| 128 | |
| 129 | if (variableIterator.isFunctional()) |
| 130 | continue; |
| 131 | if (variableIterator.isData() && variableIterator.getMissingValueCount() == 0) |
| 132 | continue; |
| 133 | if (variableIterator.isData() && variableIterator.getMissingValueCount() > 0){ |
| 134 | result.add("ALLOCATE(" + variableIterator.getName() + |
| 135 | " % two_dim_missing("+ variableIterator.getMissingValueCount() +", 1:2))"); |
| 136 | } |
| 137 | |
| 138 | result.add("ALLOCATE(" + variableIterator.getName() + " % buffer(" + |
| 139 | entityIterator.getXCoordinate().getSize() |
| 140 | + " * " + entityIterator.getYCoordinate().getSize() + "))"); |
| 141 | result.add(variableIterator.getName() + |
| 142 | " % two_dim % successful_changes = 0"); |
| 143 | result.add(variableIterator.getName() + " % buffer_index = -1"); |
| 144 | } |
| 145 | |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | //Calls for reading data and setting initial values |
| 150 | result.add(""); |
| 151 | result.add("CALL read_data(" + callParameters + ")"); |
| 152 | if (spatial != null) |
| 153 | result.add("CALL set_spatial(spatial)"); |
| 154 | result.add("CALL set_initial_values(" + callParameters + ")"); |
| 155 | result.add("CALL set_functional(" + callParameters + ")"); |
| 156 | |
| 157 | |
| 158 | result.add(""); |
| 159 | result.add("OPEN(UNIT=15, FILE=output_filename, STATUS='REPLACE', IOSTAT=status)"); |
| 160 | result.add("IF (status /= 0) THEN"); |
| 161 | result.add("WRITE (*, *) 'Could not open ', output_filename, ' for writing. Exiting.'"); |
| 162 | result.add("STOP"); |
| 163 | result.add("END IF"); |
| 164 | result.add(""); |
| 165 | result.add("OPEN(UNIT=14, FILE=summary_filename, STATUS='REPLACE', IOSTAT=status)"); |
| 166 | result.add("IF (status /= 0) THEN"); |
| 167 | result.add("WRITE (*, *) 'Could not open ', summary_filename, ' for writing. Exiting.'"); |
| 168 | result.add("STOP"); |
| 169 | result.add("END IF"); |
| 170 | |
| 171 | result.add(""); |
| 172 | result.add("progress = 0"); |
| 173 | // generates a subroutine call |
| 174 | |
| 175 | if (model.getUpdateStrategy().equals("random")){ |
| 176 | // not implemented |
| 177 | System.out.println("The random update strategy is not implemented."); |
| 178 | throw new InvalidModelException("The random update strategy is not implemented."); |
| 179 | |
| 180 | //result.add("CALL update_one(" + callParameters + ", parameters_achieved)"); |
| 181 | |
| 182 | }else if(model.getUpdateStrategy().equals("sequential")){ |
| 183 | //All variables are updated at the same time |
| 184 | result.add("DO i=1, " + model.getIterations()); |
| 185 | result.add("CALL update_all(" + callParameters + ")"); |
| 186 | result.add("IF (i > burn_in .AND. MOD(i, thinning) == 0) THEN"); |
| 187 | result.add("CALL write_output("+callParameters + ")"); |
| 188 | result.add("END IF"); |
| 189 | result.add(""); |
| 190 | result.add("IF(i/("+ model.getIterations() + "/100) > progress) THEN"); |
| 191 | result.add("progress = i/("+ model.getIterations() + "/100)"); |
| 192 | result.add("WRITE(*, *) progress, '% of simulation complete'"); |
| 193 | result.add("END IF"); |
| 194 | |
| 195 | result.add("END DO"); |
| 196 | } |
| 197 | |
| 198 | //Write summary here... |
| 199 | result.add(""); |
| 200 | result.add("CALL write_summary(" + callParameters + ")"); |
| 201 | result.add("CALL write_last_values(" + callParameters + ")"); |
| 202 | |
| 203 | result.add("CLOSE(15)"); |
| 204 | result.add("WRITE (*, *) 'Simulation completed successfully.'"); |
| 205 | result.add(""); |
| 206 | result.add(""); |
| 207 | result.add("CONTAINS"); |
| 208 | |
| 209 | result.add("SUBROUTINE random_init()"); |
| 210 | result.add("! This subroutine initializes the default random number generator."); |
| 211 | result.add("IMPLICIT NONE"); |
| 212 | result.add("EXTERNAL G05CBF, G05ZAF"); |
| 213 | result.add("INTEGER :: size, status"); |
| 214 | result.add("INTEGER, DIMENSION(8) :: time"); |
| 215 | result.add("INTEGER, DIMENSION(:), ALLOCATABLE :: seed"); |
| 216 | result.add(""); |
| 217 | result.add("CALL RANDOM_SEED(size=size)"); |
| 218 | result.add("ALLOCATE(seed(size), stat=status)"); |
| 219 | result.add("IF (status > 0) STOP 'Failed to allocate!'"); |
| 220 | result.add("CALL DATE_AND_TIME(values = time)"); |
| 221 | result.add("seed = 100*time(7) + time(8)/10"); |
| 222 | result.add("CALL G05ZAF('O')"); |
| 223 | result.add("CALL G05CBF(seed)"); |
| 224 | result.add("END SUBROUTINE random_init"); |
| 225 | result.add(""); |
| 226 | result.addAll(generateSetFunctional(callParameters, model)); |
| 227 | |
| 228 | if (model.getUpdateStrategy().equals("random")) |
| 229 | result.addAll(generateUpdateOne(callParameters, model)); |
| 230 | else if (model.getUpdateStrategy().equals("sequential")) |
| 231 | result.addAll(generateUpdateAll(callParameters, model)); |
| 232 | |
| 233 | result.add("END PROGRAM main"); |
| 234 | |
| 235 | FortranWriter writer = new FortranWriter("main.f90"); |
| 236 | writer.write(result); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | |
| 241 | private static ArrayList<String> generateUpdateOne(String parameters, ComputationalModel |
| 242 | model) { |
| 243 | ArrayList<String> result = new ArrayList<String>(); |
| 244 | // Implemented iff we have time. |
| 245 | return result; |
| 246 | } |
| 247 | |
| 248 | private static ArrayList<String> generateUpdateAll(String parameters, ComputationalModel |
| 249 | model) |
| 250 | throws InvalidModelException, IllegalParametersException, MissingFunctionException { |
| 251 | ArrayList<String> result = new ArrayList<String>(); |
| 252 | LinkedList<Variable> variables = model.getVariableList(); |
| 253 | LinkedList<Entity> entities = model.getEntityList(); |
| 254 | |
| 255 | //Subroutine... |
| 256 | result.add("SUBROUTINE update_all(" + parameters + ")"); |
| 257 | result.add("IMPLICIT NONE"); |
| 258 | |
| 259 | //Variables... |
| 260 | result.addAll(Generator.generateIntroduction()); |
| 261 | |
| 262 | |
| 263 | |
| 264 | // !!! Introduce variables here !!! |
| 265 | |
| 266 | result.add("INTEGER, PARAMETER :: dp = SELECTED_REAL_KIND(8, 0)"); |
| 267 | result.add("INTEGER :: i, j, k, missing_index, prob_loop, ifail"); |
| 268 | result.add("INTEGER :: i2, j2, sum_int, neighbour, h"); |
| 269 | result.add("REAL (KIND=dp) :: p_acc, new_frequency, frequency"); |
| 270 | result.add("INTEGER :: int_transition"); |
| 271 | result.add("REAL (KIND=dp) :: transition_real, temp_real, sum_real"); |
| 272 | result.add("REAL (KIND=dp), SAVE, DIMENSION(1000) :: prob"); |
| 273 | result.add("INTEGER, SAVE :: prob_index = -1"); |
| 274 | |
| 275 | // Distributions' freq functions need these |
| 276 | result.add("EXTERNAL G01BKF"); |
| 277 | result.add("EXTERNAL G01BJF"); |
| 278 | result.add("EXTERNAL G01EEF"); |
| 279 | result.add("EXTERNAL G05FAF"); // for generating random number for acceptation |
| 280 | |
| 281 | |
| 282 | result.add("IF (prob_index > SIZE(prob) .OR. prob_index < 1) THEN"); |
| 283 | result.add("CALL G05FAF(0.0_dp, 1.0_dp, SIZE(prob), prob)"); |
| 284 | result.add("prob_index = 1"); |
| 285 | result.add("END IF"); |
| 286 | |
| 287 | |
| 288 | String strategy = model.getUpdateStrategy(); |
| 289 | variables = model.getVariableList(); |
| 290 | |
| 291 | /* |
| 292 | // DEBUG |
| 293 | System.out.println("GENERATOR DEBUG: printing global variables list:"); |
| 294 | for (Variable v : variables) |
| 295 | System.out.println(v.getName()); |
| 296 | */ |
| 297 | for (Variable variableIterator : variables){ |
| 298 | if (!variableIterator.isFunctional()){ |
| 299 | result.add("! Updating " + variableIterator.getName()); |
| 300 | result.addAll(Acceptation.generateNewValueCode(variableIterator)); |
| 301 | result.addAll(Acceptation.generateNewValuesFunctionalCode(variableIterator)); |
| 302 | result.addAll(Acceptation.generateAcceptationFormula(variableIterator)); |
| 303 | result.addAll(Acceptation.generateAcceptationCode(variableIterator, strategy)); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | |
| 308 | for (Entity entityIterator : entities){ |
| 309 | variables = entityIterator.getVariableList(); |
| 310 | |
| 311 | for (Variable variableIterator : variables){ |
| 312 | |
| 313 | // DEBUG |
| 314 | //System.out.println(variableIterator); |
| 315 | |
| 316 | if (variableIterator.isFunctional()){ |
| 317 | continue; |
| 318 | } |
| 319 | |
| 320 | if (variableIterator.isData() && variableIterator.getMissingValueCount() == 0){ |
| 321 | continue; |
| 322 | } |
| 323 | result.add("! Updating " + variableIterator.getName()); |
| 324 | |
| 325 | //We have an array... |
| 326 | if (variableIterator.getEntity().isMatrix() == false){ |
| 327 | //Variable is not data |
| 328 | if (!variableIterator.isData()) { |
| 329 | result.add("DO i = 1, " + variableIterator.getEntity().getSize()); |
| 330 | result.addAll(Acceptation.generateNewValueCode(variableIterator)); |
| 331 | result.addAll(Acceptation.generateNewValuesFunctionalCode(variableIterator)); |
| 332 | result.addAll(Acceptation.generateAcceptationFormula(variableIterator)); |
| 333 | result.addAll(Acceptation.generateAcceptationCode(variableIterator, strategy)); |
| 334 | result.add("END DO"); |
| 335 | |
| 336 | //Variable is data. Iterate through missing value array |
| 337 | } else { |
| 338 | result.add("DO missing_index = 1, " + variableIterator.getMissingValueCount()); |
| 339 | result.add("i=" + variableIterator.getName() + " % one_dim_missing(missing_index)"); |
| 340 | result.addAll(Acceptation.generateNewValueCode(variableIterator)); |
| 341 | result.addAll(Acceptation.generateNewValuesFunctionalCode(variableIterator)); |
| 342 | result.addAll(Acceptation.generateAcceptationFormula(variableIterator)); |
| 343 | result.addAll(Acceptation.generateAcceptationCode(variableIterator, strategy)); |
| 344 | result.add("END DO"); |
| 345 | |
| 346 | } |
| 347 | |
| 348 | //We have a matrix |
| 349 | } else { |
| 350 | //Variable is not data. |
| 351 | if (!variableIterator.isData()){ |
| 352 | result.add("DO i=1, " + variableIterator.getEntity().getYCoordinate().getSize()); |
| 353 | result.add("DO j=1, " + variableIterator.getEntity().getXCoordinate().getSize()); |
| 354 | |
| 355 | result.addAll(Acceptation.generateNewValueCode(variableIterator)); |
| 356 | result.addAll(Acceptation.generateNewValuesFunctionalCode(variableIterator)); |
| 357 | result.addAll(Acceptation.generateAcceptationFormula(variableIterator)); |
| 358 | result.addAll(Acceptation.generateAcceptationCode(variableIterator, strategy)); |
| 359 | |
| 360 | result.add("END DO"); |
| 361 | result.add("END DO"); |
| 362 | |
| 363 | //Variable is data. Iterate through missing value matrix. |
| 364 | }else{ |
| 365 | result.add("DO missing_index=1, " + variableIterator.getMissingValueCount()); |
| 366 | result.add("i=" + variableIterator.getName() + " % two_dim_missing(missing_index, 1)"); |
| 367 | result.add("j=" + variableIterator.getName() + " % two_dim_missing(missing_index, 2)"); |
| 368 | result.addAll(Acceptation.generateNewValueCode(variableIterator)); |
| 369 | result.addAll(Acceptation.generateNewValuesFunctionalCode(variableIterator)); |
| 370 | result.addAll(Acceptation.generateAcceptationFormula(variableIterator)); |
| 371 | result.addAll(Acceptation.generateAcceptationCode(variableIterator, strategy)); |
| 372 | result.add("END DO"); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | result.add("END SUBROUTINE update_all"); |
| 379 | return result; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | private static ArrayList<String> generateSetFunctional(String parameters, ComputationalModel |
| 384 | model) throws InvalidModelException{ |
| 385 | |
| 386 | ArrayList<String> result = new ArrayList<String>(); |
| 387 | LinkedList<Variable> variables = model.getVariableList(); |
| 388 | LinkedList<Entity> entities = model.getEntityList(); |
| 389 | |
| 390 | result.add(""); |
| 391 | result.add("SUBROUTINE set_functional(" + parameters + ")"); |
| 392 | result.add("! This subroutine calculates initial values for functional parameters."); |
| 393 | result.add("IMPLICIT NONE"); |
| 394 | |
| 395 | //Variables... |
| 396 | for (Variable variable : variables) { |
| 397 | if (variable.isInteger()) |
| 398 | result.add("TYPE(variables_int), INTENT(INOUT) :: " + variable.getName()); |
| 399 | else |
| 400 | result.add("TYPE(variables_real), INTENT(INOUT) :: " + variable.getName()); |
| 401 | } |
| 402 | |
| 403 | for (Entity entity : entities) { |
| 404 | variables = entity.getVariableList(); |
| 405 | for (Variable variable : variables){ |
| 406 | if (variable.isInteger()) |
| 407 | result.add("TYPE(variables_int), INTENT(INOUT) :: " + variable.getName()); |
| 408 | else |
| 409 | result.add("TYPE(variables_real), INTENT(INOUT) :: " + variable.getName()); |
| 410 | } |
| 411 | } |
| 412 | result.add(""); |
| 413 | result.add("INTEGER :: sum_int, k, i2, j2, neighbour"); |
| 414 | result.add("REAL (KIND=SELECTED_REAL_KIND(8, 0)) :: sum_real"); |
| 415 | result.add(""); |
| 416 | |
| 417 | for (Entity entity : entities) { |
| 418 | variables = entity.getVariableList(); |
| 419 | |
| 420 | for (Variable variable : variables) { |
| 421 | if (variable.isFunctional() && variable.isSpatial()){ |
| 422 | result.addAll(correctLooping(variable, true)); |
| 423 | result.addAll(setSpatialStartValue(variable)); // update spatials first |
| 424 | result.addAll(correctLooping(variable, false)); |
| 425 | result.add(""); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | |
| 431 | variables = model.getVariableList(); |
| 432 | |
| 433 | for (Variable variable : variables) { |
| 434 | if (variable.isSpatial()) // then the others |
| 435 | continue; |
| 436 | if (variable.isFunctional()){ |
| 437 | result.addAll(correctLooping(variable, true)); |
| 438 | result.addAll(setFunctionalStartValue(variable)); |
| 439 | result.addAll(correctLooping(variable, false)); |
| 440 | result.add(""); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | for (Entity entity : entities) { |
| 445 | variables = entity.getVariableList(); |
| 446 | for (Variable variable : variables){ |
| 447 | if (variable.isSpatial()) |
| 448 | continue; |
| 449 | if (variable.isFunctional()){ |
| 450 | result.addAll(correctLooping(variable, true)); |
| 451 | result.addAll(setFunctionalStartValue(variable)); |
| 452 | result.addAll(correctLooping(variable, false)); |
| 453 | result.add(""); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | |
| 459 | result.add("END SUBROUTINE set_functional"); |
| 460 | result.add(""); |
| 461 | return result; |
| 462 | } |
| 463 | |
| 464 | private static ArrayList<String> setSpatialStartValue(Variable var) throws InvalidModelException{ |
| 465 | |
| 466 | //System.out.println("GENERATOR DEBUG: setSpatialStartValue for " + var.getName()); |
| 467 | //System.out.println("Details: " + var); |
| 468 | |
| 469 | ArrayList<String> result = new ArrayList<String>(); |
| 470 | |
| 471 | String[] equation = var.getEquation().getEquation(); |
| 472 | String finalEquation = ""; |
| 473 | Variable par = var.getEquation().getParameters()[0]; |
| 474 | String parname = par.getName(); |
| 475 | String parIndexing = ""; |
| 476 | |
| 477 | |
| 478 | if (!var.getEntity().isMatrix()) { |
| 479 | if (equation[0].startsWith("COUNT")){ |
| 480 | finalEquation = "spatial(i2, 1)"; |
| 481 | result.add(var.getName() + " % one_dim(i2) % value = " + finalEquation); |
| 482 | } else{ |
| 483 | // calculate sum |
| 484 | |
| 485 | |
| 486 | if (par.isInteger()) |
| 487 | result.add("sum_int = 0"); |
| 488 | else |
| 489 | result.add("sum_real = 0"); |
| 490 | result.add("! 1 to number of neighbours"); |
| 491 | result.add("DO k = 1, spatial(i2, 1)"); |
| 492 | |
| 493 | result.add("neighbour = spatial(i2, k + 1)"); |
| 494 | if (par.isInteger()) |
| 495 | result.add("sum_int = sum_int + "+ parname + correctIndexing(par, |
| 496 | "neighbour") +" % value"); |
| 497 | else |
| 498 | result.add("sum_real = sum_real + "+ parname + correctIndexing(par, |
| 499 | "neighbour") +" % value"); |
| 500 | |
| 501 | result.add("END DO"); |
| 502 | if (par.isInteger()) |
| 503 | result.add(var.getName() + " % one_dim(i2) % value = sum_int"); |
| 504 | else |
| 505 | result.add(var.getName() + " % one_dim(i2) % value = sum_real"); |
| 506 | |
| 507 | } |
| 508 | |
| 509 | } else{ |
| 510 | String indexing = ""; |
| 511 | if (var.getEntity().getYCoordinate().isSpatial()) |
| 512 | indexing = "i2"; |
| 513 | else |
| 514 | indexing = "j2"; |
| 515 | |
| 516 | if (equation[0].startsWith("COUNT")){ |
| 517 | finalEquation = "spatial("+ indexing +", 1)"; |
| 518 | result.add(var.getName() + " % two_dim(i2, j2) % value = " + finalEquation); |
| 519 | } else{ |
| 520 | if (par.isInteger()) |
| 521 | result.add("sum_int = 0"); |
| 522 | else |
| 523 | result.add("sum_real = 0"); |
| 524 | result.add("! 1 to number of neighbours"); |
| 525 | result.add("DO k = 1, spatial("+ indexing +", 1)"); |
| 526 | |
| 527 | result.add("neighbour = spatial("+ indexing +", k + 1)"); |
| 528 | if (par.isInteger()) |
| 529 | result.add("sum_int = sum_int + "+ parname + correctIndexing(par, "neighbour") |
| 530 | +" % value"); |
| 531 | else |
| 532 | result.add("sum_real = sum_real + "+ parname + correctIndexing(par, "neighbour") |
| 533 | +" % value"); |
| 534 | result.add("END DO"); |
| 535 | if (par.isInteger()) |
| 536 | result.add(var.getName() + " % two_dim(i2, j2) % value = sum_int"); |
| 537 | else |
| 538 | result.add(var.getName() + " % two_dim(i2, j2) % value = sum_real"); |
| 539 | // calculate sum |
| 540 | |
| 541 | } |
| 542 | |
| 543 | } |
| 544 | result.add(""); |
| 545 | return result; |
| 546 | } |
| 547 | private static ArrayList<String> setFunctionalStartValue(Variable var){ |
| 548 | ArrayList<String> result = new ArrayList<String>(); |
| 549 | |
| 550 | String[] equation = var.getEquation().getEquation(); |
| 551 | /* the equation in parts: for example: |
| 552 | 3 * alpha + EXP(beta+gamma) -> |
| 553 | 3 * |
| 554 | alpha |
| 555 | + EXP( |
| 556 | beta |
| 557 | + |
| 558 | gamma |
| 559 | ) |
| 560 | */ |
| 561 | |
| 562 | Variable[] parameters = var.getEquation().getParameters(); |
| 563 | |
| 564 | for (int i=0; i<parameters.length; i++) { |
| 565 | // dealing with Variable parameters[i] |
| 566 | if (parameters[i].getEntity() == null) { // global |
| 567 | for (int j=0; j<equation.length; j++) { |
| 568 | if (equation[j].equals(parameters[i].getName())) { |
| 569 | equation[j] += " % one_dim(1) % value"; |
| 570 | } |
| 571 | } // end looping the equation |
| 572 | } else if (parameters[i].getEntity().isMatrix() == false) { // one-dimensional |
| 573 | if (parameters[i].getEntity().equals(var.getEntity().getXCoordinate())) { |
| 574 | for (int j=0; j<equation.length; j++) { |
| 575 | if (equation[j].equals(parameters[i].getName())) { |
| 576 | equation[j] += " % one_dim(j2) % value"; |
| 577 | } |
| 578 | } // end looping the equation |
| 579 | } else { // either equals YCoordinate or both belong to the same entity |
| 580 | for (int j=0; j<equation.length; j++) { |
| 581 | if (equation[j].equals(parameters[i].getName())) { |
| 582 | equation[j] += " % one_dim(i2) % value"; |
| 583 | |
| 584 | } |
| 585 | } // end looping the equation |
| 586 | } |
| 587 | |
| 588 | } else { // two-dimensional |
| 589 | for (int j=0; j<equation.length; j++) { |
| 590 | if (equation[j].equals(parameters[i].getName())) { |
| 591 | equation[j] += " % two_dim(i2, j2) % value"; |
| 592 | |
| 593 | } |
| 594 | } // end looping the equation |
| 595 | } |
| 596 | } // end looping the parameters[] |
| 597 | |
| 598 | String finalEquation = ""; |
| 599 | for (int k=0; k<equation.length; k++) { |
| 600 | finalEquation = finalEquation + equation[k]; |
| 601 | } |
| 602 | |
| 603 | if (var.getEntity() == null) { |
| 604 | result.add(var.getName() + " % one_dim(1) % value = " + finalEquation); |
| 605 | } else if (var.getEntity().isMatrix() == false) { |
| 606 | result.add(var.getName() + " % one_dim(i2) % value = " + finalEquation); |
| 607 | } else |
| 608 | result.add(var.getName() + " % two_dim(i2, j2) % value = " + finalEquation); |
| 609 | |
| 610 | return result; |
| 611 | } |
| 612 | |
| 613 | private static ArrayList<String> correctLooping(Variable var, boolean start){ |
| 614 | ArrayList<String> result = new ArrayList<String>(); |
| 615 | if (var.getEntity() == null) |
| 616 | result.add(""); |
| 617 | else if (!var.getEntity().isMatrix()){ |
| 618 | if (start) |
| 619 | result.add("DO i2 = 1, "+var.getEntity().getSize()); |
| 620 | else |
| 621 | result.add("END DO"); |
| 622 | } else{ |
| 623 | if (start){ |
| 624 | result.add("DO i2 = 1, "+ var.getEntity().getYCoordinate().getSize()); |
| 625 | result.add("DO j2 = 1, "+ var.getEntity().getXCoordinate().getSize()); |
| 626 | } else{ |
| 627 | result.add("END DO"); |
| 628 | result.add("END DO"); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | return result; |
| 633 | } |
| 634 | |
| 635 | private static String correctIndexing(Variable par, String spatial) |
| 636 | throws InvalidModelException{ |
| 637 | String parIndexing = null; |
| 638 | if (par.getEntity() == null) |
| 639 | throw new InvalidModelException("A spatial variable is affected by a global!"); |
| 640 | if (!par.getEntity().isMatrix()){ |
| 641 | parIndexing = " % one_dim ("+spatial+")"; |
| 642 | } else{ |
| 643 | if (par.getEntity().getYCoordinate().isSpatial()) |
| 644 | parIndexing = " % two_dim ("+spatial+", j2)"; |
| 645 | else |
| 646 | parIndexing = " % two_dim (i2, "+spatial+")"; |
| 647 | } |
| 648 | |
| 649 | return parIndexing; |
| 650 | } |
| 651 | } |