File size: 8,068 Bytes
78fefe8
 
 
 
 
 
 
 
 
af492db
e5aa007
af492db
78fefe8
 
 
 
 
 
 
 
 
 
 
 
 
b286843
 
 
 
 
78fefe8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3443f4
78fefe8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
from typing import Dict, Tuple
import google.generativeai as genai
import json
import faiss
from openai import OpenAI
import numpy as np
import os
import pandas as pd

genai.configure(
    api_key=os.environ.get("gemini-2.5-flash")
)
model = genai.GenerativeModel("gemini-2.5-pro")

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
csv_path = os.path.join(BASE_DIR, "..", "data", "CATHODES_DATASET.csv")
csv_path = os.path.abspath(csv_path)

# Load cathode dataset once (adjust path if needed)
df_base = pd.read_csv(csv_path)

def query_faiss_index(query_text, 
                      faiss_index_path=None,
                      metadata_path=None,
                      top_k=5):
    
    openai_api_key = os.getenv("OPENAI_API_KEY")
    if not openai_api_key:
        raise ValueError("OPENAI_API_KEY is not set")

    client = OpenAI(api_key=openai_api_key)

    BASE_DIR = os.path.dirname(os.path.abspath(__file__))

    if faiss_index_path is None:
        faiss_index_path = os.path.join(BASE_DIR, "../sentiment/faiss_index.idx")
    if metadata_path is None:
        metadata_path = os.path.join(BASE_DIR, "../sentiment/metadata.json")

                        
    # Load index and metadata
    index = faiss.read_index(faiss_index_path)
    with open(metadata_path, "r", encoding="utf-8") as f:
        metadata = json.load(f)

    # Get embedding for query
    response = client.embeddings.create(
        input=query_text.lower(),
        model="text-embedding-3-large"
    )
    query_embedding = response.data[0].embedding
    query_embedding_np = np.array([query_embedding]).astype("float32")
    faiss.normalize_L2(query_embedding_np)

    # Search index
    distances, indices = index.search(query_embedding_np, top_k)
    results = []
    for dist, idx in zip(distances[0], indices[0]):
        meta = metadata[idx]
        results.append({
            "score": float(dist),
            "source_pdf": meta["source_pdf"],
            "page": meta["page"],
            "chunk_index": meta["chunk_index"],
            "text_snippet": meta["text"]
        })
    return results

def calculate_np_ratio(
    Q_anode_raw: float,
    m_anode: float,
    SEI_loss_fraction: float,
    Q_cathode_raw: float,
    m_cathode: float,
    vacancy_loss_fraction: float
) -> float:
    """
    N/P = (Q_anode_raw * m_anode * (1 - SEI_loss)) /
          (Q_cathode_raw * m_cathode * (1 - vacancy_loss))
    """
    Q_anode_usable = Q_anode_raw * m_anode * (1 - SEI_loss_fraction)
    Q_cathode_usable = Q_cathode_raw * m_cathode * (1 - vacancy_loss_fraction)
    return Q_anode_usable / Q_cathode_usable

def recommended_mass_loading_areal(
    Q_areal: float,
    Q_anode: float,
    Q_cathode: float,
    NP_ratio: float
) -> Tuple[float, float]:
    """
    Returns (m_cathode_mg_cm2, m_anode_mg_cm2)
    """
    m_cathode = Q_areal / Q_cathode
    m_anode   = (Q_areal / Q_anode) * NP_ratio
    # convert g/cm² → mg/cm²
    return m_cathode * 1000, m_anode * 1000

def calculate_first_cycle_ce(
    Q_charge_anode: float,
    Q_discharge_anode: float,
    Q_charge_cathode: float,
    Q_discharge_cathode: float,
    Q_charge_full: float,
    Q_discharge_full: float
) -> Dict[str, float]:
    ce_anode  = (Q_discharge_anode / Q_charge_anode) * 100
    ce_cathode= (Q_discharge_cathode / Q_charge_cathode) * 100
    ce_full   = (Q_discharge_full    / Q_charge_full)    * 100
    return {
        "CE_anode (%)": ce_anode,
        "CE_cathode (%)": ce_cathode,
        "CE_full_cell (%)": ce_full
    }

def estimate_irreversible_na_loss(
    Q_charge_full: float,
    Q_discharge_full: float
) -> float:
    """
    Irreversible Na⁺ loss per gram (mAh/g) on first cycle
    """
    return Q_charge_full - Q_discharge_full

def generate_gemini_insight(input_data: Dict, results: Dict, faiss_results: list, cathode_name: str) -> str:
    prompt = f"""Remove the underscores and .pdf from the source_pdf field in the RAG section below and put it as References at the end of the explanation.
    You are to explain the results of the calculations of a sodium-ion full cell battery using hard carbon and {cathode_name}. You are a RAG system that takes information only from the RAG section below.
    Respond with an EXTENSIVE/LONG EXPLANATIONS, scientific, and straight-to-the-point explanation without any additional opinions, explaining only the results of the calculations.
    Lastly, briefly analyze the performance of the full cell battery.
    
    ### Input Data:
    {json.dumps(input_data, indent=2)}
    
    ### Calculated Results:
    {json.dumps(results, indent=2)}
    
    ### Formulas Used:
        - N/P Ratio = (Q_anode_raw × m_anode × (1 − SEI_loss_fraction)) ÷ (Q_cathode_raw × m_cathode × (1 − vacancy_loss_fraction))\n"
        - Mass Loading Areal (mg/cm²):\n"
            - m_cathode = Q_areal ÷ Q_cathode
            - m_anode = (Q_areal ÷ Q_anode) × N/P Ratio
        - First Cycle Coulombic Efficiency (CE):
            - CE_anode = (Q_discharge_anode ÷ Q_charge_anode) × 100
            - CE_cathode = (Q_discharge_cathode ÷ Q_charge_cathode) × 100
            - CE_full = (Q_discharge_full ÷ Q_charge_full) × 100
        - Irreversible Na⁺ Loss = Q_charge_full − Q_discharge_full
    
    Remove the underscores and .pdf from the source_pdf field in the RAG section below and put it as References at the end of the explanation.
    Do not include the PDF file name directly in the explanation.
    At the end of the explanation, list the full source_pdf file names used as references.
    
    RAG Section, use only the information from this section to explain the results:
    {json.dumps(faiss_results, indent=2)}
    """
    try:
        response = model.generate_content(prompt)

        if response.candidates:
            parts = response.candidates[0].content.parts
            text_output = " ".join(
                p.text for p in parts if hasattr(p, "text") and p.text
            )
            return text_output.strip()
        else:
            return "Gemini returned no candidates."

    except Exception as e:
        return f"Gemini Error: {str(e)}"

def calculate_all_d(cathode_name: str, input_data: Dict) -> Dict:
    # 1) N/P ratio
    np_ratio = calculate_np_ratio(
        Q_anode_raw = input_data["Q_anode_raw"],
        m_anode = input_data["m_anode"],
        SEI_loss_fraction = input_data["SEI_loss_fraction"],
        Q_cathode_raw = input_data["Q_cathode_raw"],
        m_cathode = input_data["m_cathode"],
        vacancy_loss_fraction = input_data["vacancy_loss_fraction"]
    )

    # 2) Recommended mass loading
    m_cathode_mg_cm2, m_anode_mg_cm2 = recommended_mass_loading_areal(
        Q_areal = input_data["Q_areal"],
        Q_anode = input_data["Q_anode_raw"],
        Q_cathode = input_data["Q_cathode_raw"],
        NP_ratio = np_ratio
    )

    # 3) First‑cycle CE
    ce = calculate_first_cycle_ce(
        Q_charge_anode = input_data["Q_charge_anode"],
        Q_discharge_anode = input_data["Q_discharge_anode"],
        Q_charge_cathode = input_data["Q_charge_cathode"],
        Q_discharge_cathode = input_data["Q_discharge_cathode"],
        Q_charge_full = input_data["Q_charge_full"],
        Q_discharge_full = input_data["Q_discharge_full"]
    )

    # 4) Irreversible Na⁺ loss
    ir_loss = estimate_irreversible_na_loss(
        Q_charge_full = input_data["Q_charge_full"],
        Q_discharge_full = input_data["Q_discharge_full"]
    )


    results = {
        "NP_ratio": np_ratio,
        "m_cathode_mg_per_cm2": m_cathode_mg_cm2,
        "m_anode_mg_per_cm2": m_anode_mg_cm2,
        **ce,
        "Irreversible_Na_loss (mAh/g)": ir_loss
    }

    query_text = (
        f"Sodium-ion battery with hard carbon anode and cathode {cathode_name}. "
    )

    faiss_results = query_faiss_index(query_text, top_k=5)

    # 5) Generate Gemini insight
    gemini_summary = generate_gemini_insight(input_data, results, faiss_results, cathode_name)

    return {
        "results": results,
        "Gemini_Explanation": gemini_summary
    }