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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
{% extends "base.html" %}
{% block content %}

<div class="container py-4">

  <!-- Header -->
  <div class="mb-4">
    <h2 class="fw-bold mb-2">Blend Component Generator</h2>
    <p class="page-intro-text mb-0">
      Generate candidate additive molecules for a selected base fuel and evaluate the resulting blend
      using mixture CN, mixture YSI, and supporting fuel-property constraints.
    </p>
  </div>

  <!-- Alerts -->
  {% if error %}
  <div class="alert alert-danger">
    <i class="bi bi-exclamation-triangle me-2"></i>{{ error }}
  </div>
  {% endif %}

  {% if info %}
  <div class="alert alert-info">
    <i class="bi bi-info-circle me-2"></i>{{ info }}
  </div>
  {% endif %}

  <!-- Configuration Form -->
  <div class="card card-metric p-4 mb-4">
    <h5 class="fw-bold mb-3">Mixture Generator Configuration</h5>

    <form method="POST" id="mixture-gen-form">

      <div class="row g-3 align-items-end">

        <div class="col-md-4">
          <label class="form-label fw-semibold">Optimisation Mode</label>
          <select name="mode" id="mixtureGenMode" class="form-select" onchange="toggleMixtureTarget()">
            <option value="target" {% if form_values.mode == "target" %}selected{% endif %}>
              Target a specific mixture CN
            </option>
            <option value="maximize" {% if form_values.mode == "maximize" %}selected{% endif %}>
              Maximise mixture CN
            </option>
          </select>
        </div>

        <div class="col-md-3" id="targetDcnBox">
          <label class="form-label fw-semibold">Target Mixture CN</label>
          <input
            type="number"
            step="0.01"
            name="target_dcn"
            class="form-control"
            value="{{ form_values.target_dcn }}"
          >
        </div>

        <div class="col-md-3">
          <label class="form-label fw-semibold">Additive Mole Fraction</label>
          <input
            type="number"
            step="0.01"
            min="0.01"
            max="0.99"
            name="additive_fraction"
            class="form-control"
            value="{{ form_values.additive_fraction }}"
          >
          <div class="form-text">
            Example: 0.15 means 15% additive and 85% base fuel.
          </div>
        </div>

        <div class="col-md-2">
          <button id="mixture-generate-btn" type="submit" class="btn btn-primary w-100">
            Run Generator
          </button>
        </div>

      </div>

      <hr class="my-4">

      <div class="row g-3 align-items-start">

        <div class="col-md-4">
          <label class="form-label fw-semibold">Base Fuel</label>
          <select name="base_fuel_type" id="baseFuelType" class="form-select" onchange="toggleCustomBaseFuel()">
            <option value="fossil_diesel" {% if form_values.base_fuel_type == "fossil_diesel" %}selected{% endif %}>
              Fossil diesel
            </option>
            <option value="biodiesel" {% if form_values.base_fuel_type == "biodiesel" %}selected{% endif %}>
              Biodiesel
            </option>
            <option value="custom" {% if form_values.base_fuel_type == "custom" %}selected{% endif %}>
              Custom base fuel
            </option>
          </select>
          <div class="form-text">
            Fossil diesel and biodiesel use predefined representative compositions.
          </div>
        </div>

        <div class="col-md-4">
          <label class="form-label fw-semibold">YSI Objective</label>
          <div class="form-check mt-2">
            <input
              class="form-check-input"
              type="checkbox"
              name="minimize_ysi"
              id="minimizeMixtureYsi"
              {% if form_values.minimize_ysi %}checked{% endif %}
            >
            <label class="form-check-label" for="minimizeMixtureYsi">
              Minimise mixture YSI
            </label>
          </div>
          <div class="form-text">
            Enables Pareto optimisation for mixture DCN and smoke tendency.
          </div>
        </div>

      </div>

      <!-- Custom Base Fuel -->
      <div id="customBaseFuelBox" class="border rounded-3 p-3 mt-4">
        <div class="d-flex justify-content-between align-items-center mb-3">
          <div>
            <h6 class="fw-bold mb-1">Custom Base Fuel Composition</h6>
            <p class="text-muted small mb-0">
              Enter at least two base fuel components. Fractions will be normalised automatically.
            </p>
          </div>

          <button type="button" class="btn btn-outline-secondary btn-sm" onclick="addCustomBaseRow()">
            Add Component
          </button>
        </div>

        <div id="customBaseRows">
          <div class="custom-base-row row g-3 mb-2">
            <div class="col-md-8">
              <label class="form-label small fw-semibold">SMILES</label>
              <input type="text" name="custom_smiles[]" class="form-control" placeholder="e.g. CCCCCCCCCCCCCCCC">
            </div>
            <div class="col-md-4">
              <label class="form-label small fw-semibold">Mole Fraction</label>
              <input type="number" step="0.01" min="0" name="custom_fraction[]" class="form-control" placeholder="0.5">
            </div>
          </div>

          <div class="custom-base-row row g-3 mb-2">
            <div class="col-md-8">
              <input type="text" name="custom_smiles[]" class="form-control" placeholder="e.g. CCCCCCCCCCCC">
            </div>
            <div class="col-md-4">
              <input type="number" step="0.01" min="0" name="custom_fraction[]" class="form-control" placeholder="0.5">
            </div>
          </div>
        </div>

        <button type="button" class="btn btn-outline-danger btn-sm mt-2" onclick="removeCustomBaseRow()">
          Remove Last Component
        </button>
      </div>

      <div id="mixture-loading" class="mt-3" style="display:none;">
        <div class="spinner-border text-primary" role="status"></div>
        <span class="ms-2">Generating blend candidates… this may take a few minutes.</span>
      </div>

    </form>
  </div>

  <!-- Run Summary -->
  {% if run_info %}
  <div class="card card-metric p-4 mb-4">
    <h5 class="fw-bold mb-3">Generation Summary</h5>

    <div class="row g-3">

      <div class="col-md-3">
        <div class="border rounded-3 p-3 bg-white">
          <p class="text-muted small mb-1">Mode</p>
          <h6 class="fw-bold mb-0">{{ run_info.mode }}</h6>
        </div>
      </div>

      <div class="col-md-2">
        <div class="border rounded-3 p-3 bg-white">
          <p class="text-muted small mb-1">Target CN</p>
          <h6 class="fw-bold mb-0">
            {% if run_info.target_dcn is not none %}
              {{ run_info.target_dcn }}
            {% else %}
              —
            {% endif %}
          </h6>
        </div>
      </div>

      <div class="col-md-2">
        <div class="border rounded-3 p-3 bg-white">
          <p class="text-muted small mb-1">Base Fuel</p>
          <h6 class="fw-bold mb-0">{{ run_info.base_fuel_type }}</h6>
        </div>
      </div>

      <div class="col-md-2">
        <div class="border rounded-3 p-3 bg-white">
          <p class="text-muted small mb-1">Additive Fraction</p>
          <h6 class="fw-bold mb-0">{{ run_info.additive_fraction }}</h6>
        </div>
      </div>

      <div class="col-md-3">
        <div class="border rounded-3 p-3 bg-white">
          <p class="text-muted small mb-1">Objective</p>
          <h6 class="fw-bold mb-0">{{ run_info.objective }}</h6>
        </div>
      </div>

    </div>

    <div class="row g-3 mt-1">
      <div class="col-md-4">
        <div class="border rounded-3 p-3 bg-white">
          <p class="text-muted small mb-1">Filtered Candidates</p>
          <h6 class="fw-bold mb-0">{{ run_info.n_final }}</h6>
        </div>
      </div>

      <div class="col-md-4">
        <div class="border rounded-3 p-3 bg-white">
          <p class="text-muted small mb-1">Unfiltered Candidates</p>
          <h6 class="fw-bold mb-0">{{ run_info.n_unfiltered }}</h6>
        </div>
      </div>

      <div class="col-md-4">
        <div class="border rounded-3 p-3 bg-white">
          <p class="text-muted small mb-1">Pareto Candidates</p>
          <h6 class="fw-bold mb-0">{{ run_info.n_pareto }}</h6>
        </div>
      </div>
    </div>
  </div>
  {% endif %}

  <!-- Filtered Results -->
  {% if final_table %}
  <div class="card card-metric p-4 mb-4">
    <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
      <div>
        <h5 class="fw-bold mb-1">Best Blend Candidates with Property Constraints</h5>
        <p class="text-muted small mb-0">
          Generated additive molecules ranked after applying mixture and property constraints.
        </p>
      </div>

      <form method="POST" action="{{ url_for('download_mixture_generator_result', result_type='final') }}">
        <input type="hidden" name="results_data" value='{{ final_records | tojson }}'>
        <button type="submit" class="btn btn-outline-success btn-sm">
          Download Filtered CSV
        </button>
      </form>
    </div>

    <div class="table-responsive app-table-wrap molecule-table">
      {{ final_table | safe }}
    </div>
  </div>
  {% endif %}

  <!-- Unfiltered Results -->
  {% if unfiltered_table %}
  <div class="card card-metric p-4 mb-4">
    <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
      <div>
        <h5 class="fw-bold mb-1">Best Blend Candidates without Property Constraints</h5>
        <p class="text-muted small mb-0">
          Raw generated candidates before final property filtering.
        </p>
      </div>

      <form method="POST" action="{{ url_for('download_mixture_generator_result', result_type='unfiltered') }}">
        <input type="hidden" name="results_data" value='{{ unfiltered_records | tojson }}'>
        <button type="submit" class="btn btn-outline-secondary btn-sm">
          Download Unfiltered CSV
        </button>
      </form>
    </div>

    <div class="table-responsive app-table-wrap molecule-table">
      {{ unfiltered_table | safe }}
    </div>
  </div>
  {% endif %}

  <!-- Pareto Results -->
  {% if pareto_table %}
  <div class="card card-metric p-4 mb-4">
    <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
      <div>
        <h5 class="fw-bold mb-1">Pareto Front</h5>
        <p class="text-muted small mb-0">
          Non-dominated blend candidates balancing mixture CN and mixture YSI.
        </p>
      </div>

      <form method="POST" action="{{ url_for('download_mixture_generator_result', result_type='pareto') }}">
        <input type="hidden" name="results_data" value='{{ pareto_records | tojson }}'>
        <button type="submit" class="btn btn-outline-primary btn-sm">
          Download Pareto CSV
        </button>
      </form>
    </div>

    <div class="table-responsive app-table-wrap molecule-table">
      {{ pareto_table | safe }}
    </div>
  </div>
  {% endif %}

</div>

<script>
const mixtureForm = document.getElementById("mixture-gen-form");
const mixtureBtn = document.getElementById("mixture-generate-btn");
const mixtureLoading = document.getElementById("mixture-loading");

function toggleMixtureTarget() {
  const mode = document.getElementById("mixtureGenMode").value;
  const targetBox = document.getElementById("targetDcnBox");

  if (mode === "maximize") {
    targetBox.style.display = "none";
  } else {
    targetBox.style.display = "block";
  }
}

function toggleCustomBaseFuel() {
  const baseFuel = document.getElementById("baseFuelType").value;
  const customBox = document.getElementById("customBaseFuelBox");

  if (baseFuel === "custom") {
    customBox.style.display = "block";
  } else {
    customBox.style.display = "none";
  }
}

function addCustomBaseRow() {
  const container = document.getElementById("customBaseRows");
  const row = document.createElement("div");

  row.className = "custom-base-row row g-3 mb-2";
  row.innerHTML = `
    <div class="col-md-8">
      <input type="text" name="custom_smiles[]" class="form-control" placeholder="Component SMILES">
    </div>
    <div class="col-md-4">
      <input type="number" step="0.01" min="0" name="custom_fraction[]" class="form-control" placeholder="Mole fraction">
    </div>
  `;

  container.appendChild(row);
}

function removeCustomBaseRow() {
  const rows = document.querySelectorAll(".custom-base-row");

  if (rows.length > 2) {
    rows[rows.length - 1].remove();
  }
}

document.addEventListener("DOMContentLoaded", function () {
  toggleMixtureTarget();
  toggleCustomBaseFuel();
});

if (mixtureForm) {
  mixtureForm.addEventListener("submit", () => {
    mixtureBtn.disabled = true;
    mixtureLoading.style.display = "block";
  });
}
</script>

{% endblock %}