1 | package PIANOS.generator; |
2 | |
3 | import PIANOS.datastructures.*; |
4 | import PIANOS.exceptions.*; |
5 | import PIANOS.io.*; |
6 | |
7 | import java.util.*; |
8 | import java.io.*; |
9 | |
10 | /** |
11 | * This class is used to generate the Fortran module "proposal". |
12 | */ |
13 | public class Proposal { |
14 | |
15 | /** |
16 | * Generates and writes the Fortran module proposal.f90. |
17 | * |
18 | * @param model The model this simulation is related to |
19 | */ |
20 | public static void generateProposal(ComputationalModel model) throws InvalidProposalException, |
21 | IllegalParametersException, MissingFunctionException, IOException { |
22 | FortranWriter out = new FortranWriter("proposal.f90"); |
23 | |
24 | ArrayList <String> result = new ArrayList<String>(); |
25 | |
26 | result.add("MODULE proposal"); |
27 | result.add("USE user_dist"); |
28 | result.add("IMPLICIT NONE"); |
29 | result.add("PRIVATE"); |
30 | result.add("INTERFACE generate"); |
31 | result.add("MODULE PROCEDURE generate_int, generate_real"); |
32 | result.add("END INTERFACE"); |
33 | result.add("PUBLIC :: generate "); |
34 | result.add("CONTAINS"); |
35 | |
36 | |
37 | result.add("SUBROUTINE generate_int(name, buffer)"); |
38 | result.add("IMPLICIT NONE"); |
39 | result.add("CHARACTER(LEN=*), INTENT(IN) :: name"); |
40 | result.add("INTEGER, DIMENSION(:), INTENT(INOUT) :: buffer"); |
41 | result.add(""); |
42 | result.add("INTEGER :: k, ifail"); |
43 | result.add("INTEGER, PARAMETER :: dp = SELECTED_REAL_KIND(8, 0)"); |
44 | result.add("REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: binomial_reference"); |
45 | result.add("REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: poisson_reference"); |
46 | result.add(""); |
47 | result.add("EXTERNAL G05ECF"); |
48 | result.add("INTEGER G05EYF"); |
49 | result.add("EXTERNAL G05EYF"); |
50 | result.add("INTEGER G05DYF"); |
51 | result.add("EXTERNAL G05DYF"); |
52 | result.add("EXTERNAL G05FAF"); |
53 | result.add("EXTERNAL G05EDF"); |
54 | result.add("EXTERNAL G05FEF"); |
55 | result.add(""); |
56 | result.add("SELECT CASE (name)"); |
57 | |
58 | LinkedList<Variable> allVariables = new LinkedList<Variable>(); |
59 | allVariables.addAll(model.getVariableList()); |
60 | |
61 | LinkedList<Entity> entities = model.getEntityList(); |
62 | |
63 | for (Entity e : entities ) { |
64 | allVariables.addAll(e.getVariableList()); |
65 | } |
66 | |
67 | for (Variable v : allVariables) { |
68 | if (v.isData() && v.getMissingValueCount() == 0) { |
69 | continue; |
70 | } |
71 | if (v.isFunctional()) { |
72 | continue; |
73 | } |
74 | if (v.isInteger()) { |
75 | result.add("CASE('" + v.getName() + "')"); |
76 | |
77 | int paramCount = v.getProposal().getNumberOfParameters(); |
78 | String[] parameters = new String[paramCount+1]; |
79 | |
80 | for (int i=0; i<paramCount; i++) { |
81 | Object par = v.getProposal().getParameter(i); |
82 | |
83 | if (par instanceof Integer) { |
84 | parameters[i] = ((Integer)par).toString(); |
85 | } |
86 | else if (par instanceof Double) { |
87 | parameters[i] = ((Double)par).toString() + "_dp"; |
88 | } |
89 | else throw new InvalidProposalException("The proposal distribution of " |
90 | + v.getName() + " contains reference parameters."); |
91 | } |
92 | parameters[paramCount] ="buffer"; |
93 | |
94 | result.addAll(v.getProposal().getGenCode(parameters)); |
95 | } |
96 | } |
97 | |
98 | LinkedList<Variable> variables = model.getVariableList(); |
99 | |
100 | // DEBUG |
101 | /* |
102 | System.out.println("GENERATOR DEBUG Proposal.java: Global variable list after variable cases"); |
103 | for (Variable v: variables) |
104 | System.out.println(v.getName()); |
105 | */ |
106 | |
107 | result.add("END SELECT"); |
108 | |
109 | result.add("END SUBROUTINE generate_int"); |
110 | result.add(""); |
111 | result.add("SUBROUTINE generate_real(name, buffer)"); |
112 | result.add("IMPLICIT NONE"); |
113 | result.add("CHARACTER(LEN=*), INTENT(IN) :: name"); |
114 | result.add("REAL(KIND=SELECTED_REAL_KIND(8, 0)), DIMENSION(:), INTENT(INOUT) :: buffer"); |
115 | result.add("INTEGER, PARAMETER :: dp = SELECTED_REAL_KIND(8, 0)"); |
116 | result.add(""); |
117 | result.add("INTEGER :: ifail, k"); |
118 | result.add(""); |
119 | result.add("EXTERNAL G05ECF"); |
120 | result.add("INTEGER G05EYF"); |
121 | result.add("EXTERNAL G05EYF"); |
122 | result.add("INTEGER G05DYF"); |
123 | result.add("EXTERNAL G05DYF"); |
124 | result.add("EXTERNAL G05FAF"); |
125 | result.add("EXTERNAL G05EDF"); |
126 | result.add("EXTERNAL G05FEF"); |
127 | result.add(""); |
128 | result.add("SELECT CASE (name)"); |
129 | |
130 | for (Variable v : allVariables) { |
131 | if (v.isData() && v.getMissingValueCount() == 0) { |
132 | continue; |
133 | } |
134 | if (v.isFunctional()) { |
135 | continue; |
136 | } |
137 | if (v.isInteger() == false) { |
138 | result.add("CASE('" + v.getName() + "')"); |
139 | |
140 | int paramCount = v.getProposal().getNumberOfParameters(); |
141 | String[] parameters = new String[paramCount + 1]; |
142 | |
143 | for (int i=0; i<paramCount; i++) { |
144 | Object par = v.getProposal().getParameter(i); |
145 | |
146 | if (par instanceof Integer) { |
147 | parameters[i] = ((Integer)par).toString(); |
148 | } |
149 | else if (par instanceof Double) { |
150 | parameters[i] = ((Double)par).toString() + "_dp"; |
151 | } |
152 | else throw new InvalidProposalException("The proposal distribution of " |
153 | + v.getName() + " contains reference parameters."); |
154 | } |
155 | |
156 | parameters[paramCount] = "buffer"; |
157 | |
158 | result.addAll(v.getProposal().getGenCode(parameters)); |
159 | } |
160 | |
161 | } |
162 | result.add("END SELECT"); |
163 | |
164 | |
165 | result.add("END SUBROUTINE generate_real"); |
166 | result.add("END MODULE proposal"); |
167 | |
168 | out.write(result); |
169 | } |
170 | |
171 | } |