ARC网页整合

本文介绍了如何使用Python的PyQt5库创建一个包含ARC基本功能计算的网页应用,包括两状态间的计算、Starkmap映射和C6计算。作者请求帮助将Starkmap的计算结果以图像形式反馈到网页上。

做网页可以自己搜索更多大牛的文章来学习,但是本人对于前端后端的知识尚浅不敢多言。

在此选用比较简单的python中的pyqt5来做,但是由于Stark map映射需要做图,目前我还没有把做图结果反馈到网页上,所以stark map的图片将会在原网页呈现。希望读者可以帮我完善一下,在我给出的压缩包里都有完整的代码。


import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel, QLineEdit, QTextEdit, QGroupBox, QHBoxLayout, QSizePolicy
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QImage
from arc import *
import re
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import io
import math
import os

pi = math.pi


class MyWindow(QWidget):
    def __init__(self):
        super().__init__()

        # 设置窗口属性
        self.setWindowTitle('ARC基本功能计算——博铭版')  # 设置窗口的标题,
        self.setGeometry(400, 400, 1200, 800)  # 窗口的位置和大小

        # 主布局
        main_layout = QVBoxLayout()  # 垂直布局

        # 添加图片和文本
        image_label = QLabel(self)
        pixmap = QPixmap('/Users/bomingzhang/00ARC/ARC.PNG')
        pixmap = pixmap.scaledToWidth(600)  # 调整图片大小
        image_label.setPixmap(pixmap)
        image_label.setAlignment(Qt.AlignCenter)  # 居中对齐
        main_layout.addWidget(image_label)
        main_layout.addWidget(QLabel('下面是你的计算选择:', self))  # 添加文本

        # 添加提示文本
        label1 = QLabel('两状态之间的计算格式:     Cs 30 P_{3/2}->30 S_{1/2}', self)
        label2 = QLabel('Stark map计算格式:        Cs 25 P_{3/2} 1/2  0-600 V/cm', self)
        label3 = QLabel('C3,C6计算格式:           Rb 82 D_{3/2} 1/2 3/2 C6', self)

        main_layout.addWidget(label1)
        main_layout.addWidget(label2)
        main_layout.addWidget(label3)

        # 计算选项布局
        options_layout = QHBoxLayout()

        # 第一个选项
        option1_group = QGroupBox('', self)
        option1_layout = QVBoxLayout()
        option1_button = QPushButton('两个状态之间的基本计算', self)
        option1_button.clicked.connect(self.calculate_option1)
        option1_layout.addWidget(option1_button)
        option1_group.setLayout(option1_layout)
        options_layout.addWidget(option1_group)

        # 第二个选项
        option2_group = QGroupBox('', self)
        option2_layout = QVBoxLayout()
        option2_button = QPushButton('斯塔克映射', self)
        option2_button.clicked.connect(self.calculate_option2)
        option2_layout.addWidget(option2_button)
        option2_group.setLayout(option2_layout)
        options_layout.addWidget(option2_group)

        # 第三个选项
        option3_group = QGroupBox('', self)
        option3_layout = QVBoxLayout()
        option3_button = QPushButton('C6', self)
        option3_button.clicked.connect(self.calculate_option3)
        option3_layout.addWidget(option3_button)
        option3_group.setLayout(option3_layout)
        options_layout.addWidget(option3_group)

        main_layout.addLayout(options_layout)
        
        # 输入框
        self.input_edit = QLineEdit(self)
        self.input_edit.setFixedSize(1200, 30)  # 设置输入框大小
        # 添加自定义样式
        self.input_edit.setStyleSheet("background-color: white; border: 1px solid black; padding: 5px;")

        main_layout.addWidget(self.input_edit)

        # 结果显示文本框
        self.result_text = QTextEdit(self)
        self.result_text.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Expanding)
        # 设置最小和最大高度
        self.result_text.setMinimumHeight(80)
        self.result_text.setMaximumHeight(1000)
        # 添加自定义样式
        self.result_text.setStyleSheet("background-color: lightgray; border: 1px solid black; padding: 5px;")

        main_layout.addWidget(self.result_text)

        # 设置布局
        self.setLayout(main_layout)

    def calculate_option1(self):
        try:
            input_str = self.input_edit.text()
            result = self.arc_calculate1(input_str)
            self.result_text.append(f'计算结果1: {result}')
        except ValueError:
            self.result_text.append('此为两状态间基本计算,请注意输入格式')

    def calculate_option2(self):
        try:
            input_str = self.input_edit.text()
            fig = self.arc_calculate2(input_str)
            self.display_figure(fig)
        except ValueError:
            self.result_text.append('此为Stark map计算,请注意输入格式')

    def calculate_option3(self):
        try:
            input_str = self.input_edit.text()
            result = self.arc_calculate3(input_str)
            self.result_text.append(f'计算结果3: {result}')
        except ValueError:
            self.result_text.append('此为C3、C6计算,请注意输入格式')

    def arc_calculate1(self, input_str):
            pattern = r"([A-Za-z]+) (\d+) ([A-Za-z]+)_(\{[0-9/]+\})->(\d+) ([A-Za-z]+)_(\{[0-9/]+\})"
            #范例:Cs 30 P_{3/2}->30 S_{1/2}
            match = re.match(pattern, input_str)

            
            element = match.group(1)
            n1 = int(match.group(2))
            l1 = match.group(3)
            j1 = match.group(4)
            n2 = int(match.group(5))
            l2 = match.group(6)
            j2 = match.group(7)
            
            if element == 'Cs':
                atom = Caesium()
            else:
                atom = Rubidium()

            if l1 == "S":
                l1 = 0
            elif l1 == "P":
                l1 = 1
            elif l1 == "D":
                l1 = 2
            elif l1 == "F":
                l1 = 3

            if l2 == "S":
                l2 = 0
            elif l2 == "P":
                l2 = 1
            elif l2 == "D":
                l2 = 2
            elif l2 == "F":
                l2 = 3

            match1 = re.match(r"\{(\d+)/(\d+)\}", j1)
            match2 = re.match(r"\{(\d+)/(\d+)\}", j2)

            numerator = int(match1.group(1))
            denominator = int(match1.group(2))
            j1 = numerator / denominator

            numerator1 = int(match2.group(1))
            denominator1 = int(match2.group(2))
            j2 = numerator1 / denominator1

            n1, l1, j1, n2, l2, j2 = map(int, [n1, l1, j1, n2, l2, j2])

            wavelength = atom.getTransitionWavelength(n1, l1, j1, n2, l2, j2)
            frequency = atom.getTransitionFrequency(n1, l1, j1, n2, l2, j2)
            radial_part = atom.getRadialMatrixElement(n1, l1, j1, n2, l2, j2)
            reduced_dipole_1 = atom.getReducedMatrixElementJ(n1, l1, j1, n2, l2, j2)
            reduced_dipole_2 = atom.getReducedMatrixElementJ_asymmetric(n1, l1, j1, n2, l2, j2)

            return f'Wavelength: {wavelength} m\nFrequency: {frequency} Hz\nRadial part: {radial_part}\n' \
                   f'Reduced dipole matrix element 1: {reduced_dipole_1}\n' \
                   f'Reduced dipole matrix element 2: {reduced_dipole_2}'


    def arc_calculate2(self, input_str):
        try:
            fig = plt.figure()
            ax = fig.add_subplot(111)
            pattern = r"([A-Za-z]+) (\d+) ([A-Za-z]+)_(\{[0-9/]+\}) (\d+)/(\d+)  (\d+)-(\d+) V/cm"
            match = re.match(pattern, input_str)

            element = match.group(1)
            n0 = int(match.group(2))
            l0 = match.group(3)
            j0 = match.group(4)
            mj0 = float(match.group(5))/float(match.group(6))
            Emin = int(match.group(7))
            Emax = int(match.group(8))

            if element=='Cs':    
                calc = StarkMap(Caesium())
            else:
                calc = StarkMap(Rubidium())

            if l0 == "S":
                l0 = 0
            elif l0 == "P":
                l0 = 1
            elif l0 == "D":
                l0 = 2
            elif l0 == "F":
                l0 = 3

            pattern = r"\{(\d+)/(\d+)\}"
            match = re.match(pattern, j0)
            numerator = int(match.group(1))
            denominator = int(match.group(2))
            j0 = numerator / denominator

            n0 = n0
            l0 = l0
            j0 = float(j0)
            mj0 = float(mj0)
            nmax = n0 + 5
            nmin = n0 - 5
            lmax = 20
            Emin = Emin * 100
            Emax = Emax * 100
            N = 1001

            calc.defineBasis(n0, l0, j0, mj0, nmin, nmax, lmax, progressOutput=True)
            calc.diagonalise(np.linspace(Emin, Emax, N), progressOutput=True)
            calc.plotLevelDiagram(progressOutput=True, units=1, highlightState=True)

            # Instead of directly showing the plot, save it to a temporary file
            # and return the file path
            temp_file = "/path/to/temp_plot.png"  # Change this path to your desired location
            fig.savefig(temp_file)

            plt.close(fig)  # Close the figure to release memory

            # Return the file path to the temporary plot
            return temp_file
        except Exception as e:
            print(f"Error: {e}")
            return None


    def arc_calculate3(self, input_str):
                
                # 定义匹配模式
                pattern = r"([A-Za-z]+) (\d+) ([A-Za-z]+)_{(\d+)/(\d+)} (\d+)/(\d+) (\d+)/(\d+) C6"
                #范例:Rb 82 D_{3/2} 1/2 3/2 C6
                # 使用 re.search 匹配模式
                match = re.match(pattern, input_str)

                # 提取匹配的信息
                element = match.group(1)
                n0 = int(match.group(2))
                l = match.group(3)
                j = float(match.group(4))/float(match.group(5))
                j1 = float(match.group(6))/float(match.group(7))
                j2 = float(match.group(8))/float(match.group(9))


                if element=='Cs':    
                    atom = Caesium()
                
                else:
                    atom = Rubidium()

                # 判断子能级字母并转换为相应的数值
                if l == "S":
                    l = 0
                elif l == "P":
                    l = 1
                elif l == "D":
                    l = 2
                elif l == "F":
                    l = 3

                l = int(l)
                j = float(j)
                j1 = float(j1)
                j2 = float(j2)
            

        
            
                #输入是Rb 82 D_{3/2} 3/2 3/2 C6
                #calculation = PairStateInteractions(Rubidium(), n0, l0, j0, n0, l0, j0, mj0, mj0)
                calc = PairStateInteractions(atom, n0, l, j, n0, l, j, j1, j2)
                #C6 = calculation.getC6perturbatively(theta, phi, dn, deltaMax)
                c6_0 = calc.getC6perturbatively(0, 0, 5, 3.000000e+10)
                c6_30 = calc.getC6perturbatively(pi/6., 0, 5, 3.000000e+10)
                c6_45 = calc.getC6perturbatively(pi/4., 0, 5, 3.000000e+10)
                c6_60 = calc.getC6perturbatively(pi/3., 0, 5, 3.000000e+10)
                c6_90 = calc.getC6perturbatively(pi/2., 0, 5, 3.000000e+10)

                return   f'C6(0°): {c6_0}GHz (mu m)^6\n' \
                         f'C6(30°): {c6_30}GHz (mu m)^6\n' \
                         f'C6(45°): {c6_45}GHz (mu m)^6\n' \
                         f'C6(60°): {c6_60}GHz (mu m)^6\n' \
                         f'C6(90°): {c6_90}GHz (mu m)^6'

    
    def display_figure(self, temp_file):
                # Clear the result text area
                self.result_text.clear()

                # Add a label to display the plot image
                image_label = QLabel(self)
                pixmap = QPixmap(temp_file)
                pixmap = pixmap.scaledToWidth(600)  # Adjust image size if necessary
                image_label.setPixmap(pixmap)
                self.result_text.setAlignment(Qt.AlignCenter)  # Align the label to center
                self.result_text.setReadOnly(True)  # Make the text area read-only
                self.result_text.append('Stark map calculation result:')  # Add a title
                self.result_text.append('\n')  # Add some space
                self.result_text.setAlignment(Qt.AlignCenter)  # Align the text to center
                self.result_text.verticalScrollBar().setValue(0)  # Scroll to the top



if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值