Написание Python результатов, чтобы преуспеть - PullRequest
0 голосов
/ 10 февраля 2020

Я новичок ie в python и был бы признателен за помощь с этим кодом. Моя цель - получить результаты (для pH и температуры), как в l oop, сделанном в начале - как показано ниже. Код должен работать для каждой температуры при разных значениях pH.

temp , pH
(10.0, 1.0)
(10.0, 2.4444444444444446)
(10.0, 3.888888888888889)
....
(16.11111111111111, 1.0)
(16.11111111111111, 2.4444444444444446)
(16.11111111111111, 3.888888888888889)
.....
(16.11111111111111, 14.0)
(22.22222222222222, 1.0)
(22.22222222222222, 2.4444444444444446)
....
(22.22222222222222, 14.0)

Но результат таков:

pH  temp
14  65
14  65
14  65
14  65

Ниже мой код:

import os
# Then get third party modules.
from win32com.client import Dispatch 
#import matplotlib.pyplot as plt 
import numpy as np 
from math import exp,sqrt,log10
import scipy.optimize as optimize
import xlsxwriter

def selected_array(db_path, input_string):
    """Load database via COM and run input string."""
    dbase = Dispatch('IPhreeqcCOM.Object')
    dbase.LoadDatabase(db_path)
    dbase.RunString(input_string)
    return dbase.GetSelectedOutputArray()

def phreecalc_pitz(input_string):
    """Get results from PHREEQC"""
    pitzer_result  = selected_array('pitzer.dat', input_string)
    return pitzer_result

def phreecalc_minteq(input_string):
    """Get results from PHREEQC"""
    result  = selected_array('minteq.v4.dat', input_string)
    return result

# pH and temperature nested loop
pH=np.linspace(1,14,10)
temp=np.linspace(10,65,10)

P=np.zeros(len(pH));Alkalinity=np.zeros(len(pH))
Ca=np.zeros(len(pH));Mg=np.zeros(len(pH));CaCO3=np.zeros(len(pH));CaH2PO4=np.zeros(len(pH));
CaHPO4=np.zeros(len(pH));CaPO4=np.zeros(len(pH))


for t in range(len(temp)):
    for i in range(len(pH-1)):
        print (temp[t],pH[i])

    input_string = """
        SOLUTION 1
            temp      %f
            pH        %f
            pe        4
            redox     pe
            units     mg/l
            density   1
            C         5
            P         1.2
            Mg        0.5
            Cl        12
            Na        20
            -water    1 # kg

        EQUILIBRIUM_PHASES 1
            Calcite   0 10

        REACTION 1
            CO2(g)     0.9
            1 millimoles in 1 steps
            SAVE solution 1
            END
            USE solution 1
            PHASES
            NaOH
            NaOH = Na+ + OH-
            log_k     100
        REACTION 2
            NaOH       1
            0.002 millimoles in 3 steps
        SELECTED_OUTPUT 1
            -file                 D:\Desktop
            -step                 true
            -pH                   false
            -temperature          false
            -reaction             false
            -alkalinity           false
            -totals               P  Ca  Mg  Alkalinity
            -molalities           CaCO3  CaH2PO4+  CaHPO4  CaPO4-
                                  H3PO4  PO4-3
            -equilibrium_phases   Calcite
            SAVE solution 1
            END
            USE solution 1
        EQUILIBRIUM_PHASES 2
            Calcite   0 0

            END"""%(temp[t],pH[i])

    sol= phreecalc_minteq(input_string)

    P[i]= sol[2][7]; Alkalinity[i]= sol[2][10]; Ca[i]= sol[2][8]; Mg[i]= sol[2][9]
    CaCO3[i]= sol[2][11]; CaH2PO4[i]= sol[2][12]; CaHPO4[i]= sol[2][13]; CaPO4[i]= sol[2][14]



"""write results to excel"""
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('Results.xlsx')
worksheet = workbook.add_worksheet()

#write titles
worksheet.write(0, 0, 'pH')
worksheet.write(0, 1, 'temp')
worksheet.write(0, 2, 'P')
worksheet.write(0, 3, 'Alkalinity')
worksheet.write(0, 4, 'Ca')
worksheet.write(0, 5, 'Mg')
worksheet.write(0, 6, 'm_CaCO3')
worksheet.write(0, 7, 'm_CaH2PO4+')
worksheet.write(0, 8, 'm_CaHPO4')
worksheet.write(0, 9, 'm_CaPO4-')


# Start from the first cell. Rows and columns are zero indexed.
row = 1

# Iterate over the data and write it out row by row.
while row <= len(pH):
    x = 1
    worksheet.write(row, 0, pH[row-1])

    worksheet.write(row, 1, temp[row-1])

    worksheet.write(row, 2, P[row-1])

    worksheet.write(row, 3, Alkalinity[row-1])

    worksheet.write(row, 4, Ca[row-1])

    worksheet.write(row, 5, Mg[row-1])

    worksheet.write(row, 6, CaCO3[row-1])

    worksheet.write(row, 7, CaH2PO4[row-1])

    worksheet.write(row, 8, CaHPO4[row-1])

    worksheet.write(row, 9, CaPO4[row-1])

    while x <= len(temp):

        worksheet.write(x, 0, pH[row-1])

        worksheet.write(x, 1, temp[row-1])

        worksheet.write(x, 2, P[row-1])

        worksheet.write(x, 3, Alkalinity[row-1])

        worksheet.write(x, 4, Ca[row-1])

        worksheet.write(x, 5, Mg[row-1])

        worksheet.write(x, 6, CaCO3[row-1])

        worksheet.write(x, 7, CaH2PO4[row-1])

        worksheet.write(x, 8, CaHPO4[row-1])

        worksheet.write(x, 9, CaPO4[row-1])

        x=x+1
    row=row+1

workbook.close()   
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...