{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "74ca04f1-f627-474a-a811-9fd56164748d",
   "metadata": {},
   "source": [
    "Copyright (c) 2025 Mitsuru Ohno  \n",
    "Use of this source code is governed by a BSD-3-style  \n",
    "license that can be found in the LICENSE file.  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6468ebc-80e6-4d7c-96dd-ee09caa1bb96",
   "metadata": {},
   "source": [
    "## モジュールrxn_reader内の関数の使用例  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0cdcff18-5f1a-49a2-aadc-119c00bdf6c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = './sample_data/sample_rxn_ref1.csv'  # CSVファイルのパスを指定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d2f7b3ca-0192-46c2-b657-b4f42932fe3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用例\n",
    "from rxnfit import (\n",
    "    get_reactions,\n",
    "    to_chempy_style,\n",
    "    reactant_consumption,\n",
    "    generate_sys_ode,\n",
    "    get_unique_species,\n",
    "    rate_constants,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dccb0d07-0bfc-4c71-9526-e30cab3d9f6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[['1', 0.054], [['', 'AcOEt'], ['', 'OHa1']], [['', 'AcOa1'], ['', 'EtOH']], []], [['2', 0.044], [['', 'AcOiPr'], ['', 'OHa1']], [['', 'AcOa1'], ['', 'iPrOH']], []], [['3', 0.031], [['', 'EGOAc2'], ['2', 'OHa1']], [['2', 'AcOa1'], ['', 'EG']], []]]\n"
     ]
    }
   ],
   "source": [
    "# 取得した反応を表示\n",
    "reactions = get_reactions(file_path)\n",
    "print(reactions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "adc443d6-bc1d-46d0-bde0-d3c995644a3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[['1', 0.054], {'AcOEt': 1, 'OHa1': 1}, {'AcOa1': 1, 'EtOH': 1}],\n",
       " [['2', 0.044], {'AcOiPr': 1, 'OHa1': 1}, {'AcOa1': 1, 'iPrOH': 1}],\n",
       " [['3', 0.031], {'EGOAc2': 1, 'OHa1': 2.0}, {'AcOa1': 2.0, 'EG': 1}]]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 反応の表記をChemPyで用いる辞書形式に変換\n",
    "list(map(to_chempy_style, reactions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "403c69d8-6875-4648-b085-f791f6144330",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[['1', 0.054],\n",
       "  [['', 'AcOEt'], ['', 'OHa1']],\n",
       "  [['', 'AcOa1'], ['', 'EtOH']],\n",
       "  [],\n",
       "  ['k1*AcOEt(t)*OHa1(t)', ['1/1', '1/1'], ['1/1', '1/1']]],\n",
       " [['2', 0.044],\n",
       "  [['', 'AcOiPr'], ['', 'OHa1']],\n",
       "  [['', 'AcOa1'], ['', 'iPrOH']],\n",
       "  [],\n",
       "  ['k2*AcOiPr(t)*OHa1(t)', ['1/1', '1/1'], ['1/1', '1/1']]],\n",
       " [['3', 0.031],\n",
       "  [['', 'EGOAc2'], ['2', 'OHa1']],\n",
       "  [['2', 'AcOa1'], ['', 'EG']],\n",
       "  [],\n",
       "  ['k3*EGOAc2(t)*OHa1(t)**2', ['1/1', '2/1'], ['2/1', '1/1']]]]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取得した反応のリストに、速度式、係数を追記\n",
    "reactant_eq = list(map(reactant_consumption, reactions))\n",
    "reactant_eq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1c196049-67b7-4530-a797-5d275a485244",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'AcOEt': '-1/1*k1*AcOEt(t)*OHa1(t)',\n",
       " 'OHa1': '-1/1*k1*AcOEt(t)*OHa1(t)-1/1*k2*AcOiPr(t)*OHa1(t)-2/1*k3*EGOAc2(t)*OHa1(t)**2',\n",
       " 'AcOa1': '1/1*k1*AcOEt(t)*OHa1(t)+1/1*k2*AcOiPr(t)*OHa1(t)+2/1*k3*EGOAc2(t)*OHa1(t)**2',\n",
       " 'EtOH': '1/1*k1*AcOEt(t)*OHa1(t)',\n",
       " 'AcOiPr': '-1/1*k2*AcOiPr(t)*OHa1(t)',\n",
       " 'iPrOH': '1/1*k2*AcOiPr(t)*OHa1(t)',\n",
       " 'EGOAc2': '-1/1*k3*EGOAc2(t)*OHa1(t)**2',\n",
       " 'EG': '1/1*k3*EGOAc2(t)*OHa1(t)**2'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# generate_ode(reaction)\n",
    "sys_odes_dict = generate_sys_ode(reactant_eq)\n",
    "sys_odes_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8e7efbd0-cd20-48f5-a70f-f03a15b0e7cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['AcOEt', 'OHa1', 'AcOa1', 'EtOH', 'AcOiPr', 'iPrOH', 'EGOAc2', 'EG']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 反応に関与する化学種を列挙\n",
    "unique_species_list = get_unique_species(reactant_eq)\n",
    "unique_species_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7af6a6cb-a8d7-4bfc-a7f4-044dad74b886",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'k1': 0.054, 'k2': 0.044, 'k3': 0.031}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 速度定数を辞書形式で列挙\n",
    "rate_constants_dict = rate_constants(reactant_eq)\n",
    "rate_constants_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33bc715a-6e87-4e9f-ba5e-9080317d44a9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.14.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
