32 H2O water molecules

cp2k keywords

1
2
3
4
5
6
7
8
9
10
11
12
13
14
&GLOBAL
PRINT_LEVEL MEDIUM
PROJECT cuttoff-test
RUN_TYPE ENERGY_FORCE
&END GLOBAL

&FORCE_EVAL
METHOD Quickstep
&DFT
BASIS_SET_FILE_NAME BASIS_MOLOPT
POTENTIAL_FILE_NAME GTH_POTENTIALS
WFN_RESTART_FILE_NAME ../cuttoff-test-RESTART.wfn
CHARGE 0
MULTIPLICITY 1

WFN_RESTART_FILE_NAME #读取波函数文件,需要同时在DFT/&SCF中开启SCF_GUESS RESTART
CHARGE #体系整体电荷
MULTIPLICITY #体系的整体自旋多重度,2n+1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
&MGRID
NGRIDS 4
CUTOFF LT_cutoff
REL_CUTOFF LT_rel_cutoff
&END
&QS
EPS_DEFAULT 1.0E-12
METHOD GPW
&END

&SCF
SCF_GUESS RESTART
EPS_SCF 5.e-7
MAX_SCF 15
&OT
PRECONDITIONER FULL_ALL
MINIMIZER DIIS
&END OT
&OUTER_SCF
EPS_SCF 5.0E-7
MAX_SCF 1
&END OUTER_SCF
&END SCF

OT #OT算法
PRECONDITIONER #
MINIMIZER #
OUTER_SCF #
EPS_SCF #
MAX_SCF #

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
    &XC
&XC_FUNCTIONAL PBE
&END XC_FUNCTIONAL
&XC_GRID
! defaults
XC_SMOOTH_RHO NONE
XC_DERIV PW
&END XC_GRID
&END XC

&END DFT
&SUBSYS
&CELL
ABC 9.8528 9.8528 9.8528
PERIODIC XYZ
&END CELL

&KIND H
BASIS_SET DZVP-MOLOPT-SR-GTH-q1
POTENTIAL GTH-PBE-q1
&END

&KIND O
BASIS_SET DZVP-MOLOPT-SR-GTH-q6
POTENTIAL GTH-PBE-q6
&END KIND

&TOPOLOGY
COORDINATE XYZ
COORD_FILE_NAME ../structure.xyz
CONNECTIVITY OFF
&END TOPOLOGY
&END SUBSYS

&PRINT
&FORCES
&END
&END
&END FORCE_EVAL

Lammps, DFT and Gaussian Paper Online

Table

Gaussian
MD+AIMD
cp2k
Other
高被引
顶刊
Machine-Learning

作者

  1. Gaussian

Gaussian

  1. MD+AIMD

MD+AIMD


以往读过的重要MD文献


以下主要为势参数
4.Behavior and State of Boron in CaO–SiO2 Slags during Refining of Solar Grade Silicon 论文 链接
5.Effects of B2O3 on the structure and properties of blast furnace slag by molecular dynamics simulation
6.First principle study of electronic structural and physical properties of CaO-SiO2-Al2O3 ternary slag system by using Ab Initio molecular and lattice dynamics
8.The effect ofCaO(MgO) on the structure and properties ofaluminosilicate system by molecular dynamics simulation
9.The Importance of Slag Structure to Boron Removal from Silicon during the Refining Process: Insights from Raman and Nuclear Magnetic Resonance Spectroscopy Study
10.Design of Refining Slag Based on Raman and NMR Spectroscopy Study for Removing Phosphorus for SoG-Si
11.Calculated role of aluminum in the incorporation of ferric iron into magnesium silicate perovskite
12.A Molecular Dynamics Study of the Atomic Structure of (CaO)x(SiO2)1-x Glasses
14.谷歌学术Molecular dynamics study of the structures and bulk moduli of crystals in the system CaO-MgO-Al2O3-SiO2
15.Development of empirical potentials for sodium borosilicate glass systems
16.Structural Role of Sodium in Borosilicate, Phosphosilicate, and Borophosphosilicate Glasses Unveiled by Solid-State NMR and MD Simulations


以下主要为分子模拟

  1. cp2k

cp2k

cp2k经典文献

  1. Other

other

  1. 高被引

高被引

  1. 顶刊

顶刊

  1. Machine-Learning

机器学习

Calculate Si_bulk8 Energy and Forces

庚子计算-AIMD与CP2K讲义3

How to Calculate Energy and Forces

Table of Contents

Introduction
Input Files
Running the Calculation
Obtaining the Results
Adding Smearing

  1. Introduction
  • In this tutorial, we are going to show the reader how to perform a simple static self-consistent Kohn-Sham Density Functional Theory energy and force calculation on a system using QUICKSTEP.
  • We will use face centred cubic bulk Si, with 8 atoms in a cubic unit cell as an example.
  • The example files are contained in static_calculation.tgz.
  • The calculations were carried out with CP2K version 2.4.
  • The reader should note that the integration grid settings used in the example calculations have already chosen to be sufficient for the given accuracy.
  • A separate tutorial is available to show how to achieve this.
  1. Input Files
  • We first look at the input files required for this calculation. The necessary input files are:

    • Si_bulk8.inp: the main input file for the calculation, which defines the system and the job parameters
    • BASIS_SET: file containing parameters for the basis sets that can be used by CP2K for this calculation
    • GTH_POTENTIALS: file containing parameters for the pseudopotentials that can be used by CP2K for this calculation
  • A list of basis set and pseudopotential files may be found in cp2k/data/ that comes with a cp2k source release.

  • These should cover most of the commonly used elements.

  • The user will, however, need to produce their own main input file for a given calculation.

  • Let us look at the main input: Si_bulk8.inp in detail.

  • The name of this file can be arbitrary, so is the file extension.

  • The input file is structured into ordered blocks and keywords, the order of which are unimportant.

  • Each input block is referred to as a “section” in this tutorial, and some sections are “subsections” of other sections.

  • Full definitions of the input file format and keywords is available via the ‘’CP2K’’ input reference manual.

  • The input file is shown below:

    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
    &GLOBAL
    PROJECT Si_bulk8
    RUN_TYPE ENERGY_FORCE
    PRINT_LEVEL LOW
    &END GLOBAL
    &FORCE_EVAL
    METHOD Quickstep
    &SUBSYS
    &KIND Si
    ELEMENT Si
    BASIS_SET DZVP-GTH-PADE
    POTENTIAL GTH-PADE-q4
    &END KIND
    &CELL
    A 5.430697500 0.000000000 0.000000000
    B 0.000000000 5.430697500 0.000000000
    C 0.000000000 0.000000000 5.430697500
    &END CELL
    &COORD
    Si 0.000000000 0.000000000 0.000000000
    Si 0.000000000 2.715348700 2.715348700
    Si 2.715348700 2.715348700 0.000000000
    Si 2.715348700 0.000000000 2.715348700
    Si 4.073023100 1.357674400 4.073023100
    Si 1.357674400 1.357674400 1.357674400
    Si 1.357674400 4.073023100 4.073023100
    Si 4.073023100 4.073023100 1.357674400
    &END COORD
    &END SUBSYS
    &DFT
    BASIS_SET_FILE_NAME BASIS_SET
    POTENTIAL_FILE_NAME GTH_POTENTIALS
    &QS
    EPS_DEFAULT 1.0E-10
    &END QS
    &MGRID
    NGRIDS 4
    CUTOFF 300
    REL_CUTOFF 60
    &END MGRID
    &XC
    &XC_FUNCTIONAL PADE
    &END XC_FUNCTIONAL
    &END XC
    &SCF
    SCF_GUESS ATOMIC
    EPS_SCF 1.0E-7
    MAX_SCF 300
    &DIAGONALIZATION ON
    ALGORITHM STANDARD
    &END DIAGONALIZATION
    &MIXING T
    METHOD BROYDEN_MIXING
    ALPHA 0.4
    NBROYDEN 8
    &END MIXING
    &END SCF
    &END DFT
    &PRINT
    &FORCES ON
    &END FORCES
    &END PRINT
    &END FORCE_EVAL
  • The main sections in the input file are:

  • ‘’GLOBAL’’: contains general options for the CP2K run, such as the name of the job, the type of run etc.

  • ‘’FORCE_EVAL’’: contains all parameters associated with the evaluation of forces on atoms, this includes the initial atomic coordinates.

  • We look at each section in detail. The GLOBAL section in Si_bulk8.inp is presented below

  • We will be doing a static energy and force calculation, in this case, we must set ‘’RUN_TYPE’’ to ENERGY_FORCE.

  • Keyword ‘’PROJECT’’ is an alias for PROJECT_NAME, which sets the root-name of the calculation, in this case Si_bulk8.

  • Any output files automatically generated by CP2K will have the name prefixed by Si_bulk8.

  • ‘’PRINT_LEVEL’’ controls the default verbosity of the main output of CP2K, in this example, it is set to “low”.

  • The verbosity of the output can be fine-tuned by overriding this setting in each individual subsection of the input.

  • We now explain the section FORCE_EVAL line-by-line.

  • The keyword ‘’METHOD’’ chooses the method for evaluating the forces on atoms to QUICKSTEP, i.e.

  • Density Functional Theory using the Gaussian and Planewaves (GPW) method.

  • The subsection ‘’SUBSYS’’ defines the simulation unit cell and the initial coordinates of atoms in the calculation.

  • The subsection ‘’KIND’’ gives definitions of elements in the calculation.

  • There must be one KIND subsection per element.

  • In this example, for Si, we have defined the basis set to be used: DZVP-GTH-PADE (double-ζ with polarisation basis optimised for Geodecker-Teter-Hutter PADE LDA pseudopotential); and the pseudopotential: GTH-PADE-q4 (Geodecker-Teter-Hutter PADE LDA pseudopotential with 4 valence electrons).

  • The basis set and pseudopotential names must correspond to an existing entry in the corresponding basis set and pseudopotential files defined by ‘’BASIS_SET_FILE_NAME’’ and ‘’POTENTIAL_FILE_NAME’’ keywords in ‘’DFT’’ subsection, in FORCE_EVAL section.

  • The chosen basis for Si corresponds to parameters:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    Si DZVP-GTH-PADE
    2
    3 0 1 4 2 2
    1.2032422345 0.3290350445 0.0000000000 0.0474539126 0.0000000000
    0.4688409786 -0.2533118323 0.0000000000 -0.2594473573 0.0000000000
    0.1679863234 -0.7870946277 0.0000000000 -0.5440929303 0.0000000000
    0.0575619526 -0.1909898479 1.0000000000 -0.3624010364 1.0000000000
    3 2 2 1 1
    0.4500000000 1.0000000000
  • in file BASIS_SET; and the chosen pseudopotential corresponds to parameters:

    1
    2
    3
    4
    5
    6
    7
    Si GTH-PADE-q4 GTH-LDA-q4
    2 2
    0.44000000 1 -7.33610297
    2
    0.42273813 2 5.90692831 -1.26189397
    3.25819622
    0.48427842 1 2.72701346
  • in file GTH_POTENTIALS.

  • The subsection ‘’CELL’’ defines the simulation unit cell used in a calculation.

  • In this example, we define the unit cell as cubic, with lattice constant equal to 5.4306975 Angstroms.

  • “Angstrom” is the default unit for cell vectors.

  • ‘’A’’, ‘’B’’ and ‘’C’’ are the first, second and third lattice (cell) vectors.

  • There are many ways to define the cell, see ‘’CP2K’’ input reference manual for more details.

  • The initial atomic coordinates are specified in the ‘’COORD’’ subsection.

  • The default input format for atomic coordinates in CP2K is:

    1
    <ATOM_KIND> X Y Z
  • where X, Y and Z are Cartesian coordinates in Angstroms.

  • This can be changed by configuring keyword ‘’SCALED’’ to .TRUE., in the COORD subsection, which makes the coordinate input X Y Z to be fractional with respect to the lattice vectors.

  • One can also change the unit for the Cartesian coordinates by setting the keyword ‘’UNIT’’ with in the subsection.

  • should be a label that corresponds to the definition of the elements in the ‘’KIND’’ subsections.

  • After the SUBSYS section in the input file Si_bulk8.inp follows the ‘’DFT’’ subsection, which controls all aspects of the self-consistent Kohn-Sham Density Functional Theory calculation.

  • This subsection is only relevant if and only if the METHOD keyword in FORCE_EVAL is set to QUICKSTEP.

    1
    2
    BASIS_SET_FILE_NAME  BASIS_SET
    POTENTIAL_FILE_NAME GTH_POTENTIALS
  • As already mentioned above, the keywords ‘’BASIS_SET_FILE_NAME’’ and ‘’POTENTIAL_FILE_NAME’’ set the files that contains basis set and pseudopotential parameters.

    1
    2
    3
    &QS
    EPS_DEFAULT 1.0E-10
    &END QS
  • The ‘’QS’’ subsection contains general control parameters used by QUICKSTEP.

  • ‘’EPS_DEFAULT’’ sets the default value for all tolerances used within QUICKSTEP.

  • The individual tolerances (EPS_*) can be set, and they will override the EPS_DEFAULT value.

    1
    2
    3
    4
    5
    &MGRID
    NGRIDS 4
    CUTOFF 300
    REL_CUTOFF 60
    &END MGRID
  • The ‘’MGRID’’ subsection defines how the integration grid used in QUICKSTEP calculations should be setup.

  • QUICKSTEP uses a multi-grid method for representing Gaussian functions numerically on the grid.

  • Narrow and sharp Gaussians are mapped onto a finer grid than wider and smoother Gaussians.

  • In this case, we are telling the code to set up 4 levels of multi-grids,

  • with the planewave cutoff of the finest grid set to be 300 Ry,

  • and with the grid spacing underneath any Gaussian functions to be finer than the equivalent planewave cutoff of 60 Ry.

  • The users should read the tutorial “Converging the CUTOFF and REL_CUTOFF” for details on how these parameters affect the grid constructed, and how to define a sufficient grid for their calculation.

  • In this example, the grid defined has already been found to be sufficient for the energy and force calculation.

  • The ‘’XC’’ subsection follows:

    1
    2
    3
    4
    &XC
    &XC_FUNCTIONAL PADE
    &END XC_FUNCTIONAL
    &END XC
  • This defines which exchange-correlation density functional we want to use.

  • In this we choose PADE LDA functional, which is consistent with the basis set and pseudopotential we have chosen.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    &SCF
    SCF_GUESS ATOMIC
    EPS_SCF 1.0E-7
    MAX_SCF 300
    &DIAGONALIZATION
    ALGORITHM STANDARD
    &END DIAGONALIZATION
    &MIXING
    METHOD BROYDEN_MIXING
    ALPHA 0.4
    NBROYDEN 8
    &END MIXING
    &END SCF
  • The ‘’SCF’’ subsection defines all the settings related to methods used to find a self-consistent solution of the Kohn-Sham DFT formalism.

  • ‘’SCF_GUESS’’ sets how the initial trial electron density function ρ(→r) is to be generated.

  • In this example (ATOMIC), the initial density is to be generated using overlapping of atomic charge densities.

  • A good starting point for the electron density in the self-consistency loop is important in obtaining a convergent result quickly.

  • ‘’EPS_SCF’’ sets the tolerance of the charge density residual.

  • This overrides the EPS_DEFAULT value set in QS subsection.

  • ‘’MAX_SCF’’ sets the maximum number of self-consistency loops QUICKSTEP is allowed to perform for each ground-state energy calculation.

    1
    2
    3
    &DIAGONALIZATION  ON
    ALGORITHM STANDARD
    &END DIAGONALIZATION
  • The ‘’DIAGONALIZATION ‘’ subsection tells the code to use the traditional diagonalisation method for finding the ground state Kohn-Sham energy and electron density.

  • The subsection heading also takes an argument, and in this case is set to “ON”, which equivalent to “.TRUE.” or “T”, and indicates that the diagonalisation method is turned on.

  • One can also omit the value of the subsection heading, which defaults to “.TRUE.”.

  • The alternative to diagonalisation is to use the Orbital Transform (OT) method, in which case, the user should either delete the DIAGONALIZATION block or change “ON” to “OFF” (or “.FALSE.”), and add the ‘’OT’’ subsection instead.

  • The ‘’ALGORITHM’’ keyword sets the algorithm to use for diagonalisation of the Kohn-Sham Hamiltonian.

  • “STANDARD” means the standard LAPACK/SCALAPACK subroutines are to be used for diagonalisation.

    1
    2
    3
    4
    5
    &MIXING  T
    METHOD BROYDEN_MIXING
    ALPHA 0.4
    NBROYDEN 8
    &END MIXING
  • The ‘’MIXING’’ subsection contains all the parameters associated with charge mixing in a self-consistency calculation.

  • The subsection also admits a value, which can be either .TRUE. (T) or .FALSE. (F), which switches charge mixing on or off.

  • The default is .TRUE.. Note that this subsection only applies to the traditional diagonalisation method.

  • The OT method uses a different approach for charge mixing, and is explained in other tutorials.

  • The keyword ‘’ALPHA’’ sets the mixing parameter; in this example 0.4 of the output density will be mixed with 0.6 of the input density to form the new input density in the next SCF iteration.

  • The keyword ‘’METHOD’’ sets the mixing method; in this case, we will use Broyden mixing.

  • The keyword ‘’NBROYDEN’’ is an alias to the parameter NBUFFER, and it sets the number of histories to be used in the Broyden mixing algorithm.

  • The final ‘’PRINT’’ subsection in FORCE_EVAL section:

    1
    2
    3
    4
    &PRINT
    &FORCES ON
    &END FORCES
    &END PRINT
  • tells CP2K, in this case, to print out atomic forces in the main output of the calculation.

  1. Running the Calculation
  • To run the calculation, the reader needs to have a working CP2K package compiled, and with the path to its binaries in the system PATH.
  • The files Si_bulk8.inp, BASIS_SET and GTH_POTENTIAL should be in the same working directory.
  • In this example, we will use the MPI version of CP2K. Type command:
    1
    mpirun -n 2 cp2k.popt -o Si_bulk8.out Si_bulk8.inp &
  • in the working directory to run CP2K in parallel with 2 MPI processes in the background.
  • The -o option redirects the CP2K output to file Si_bulk8.out.
  • Note that the -o option appends output of successive runs to Si_bulk8.out, so if the reader wants to start completely from afresh, they must delete Si_bulk8.out before running a new calculation.
  1. Obtaining the Results
  • After the job has finished, we should obtain the following files:
    • Si_bulk8.out
    • Si_bulk8-RESTART.wfn
    • Si_bulk8-RESTART.wfn.bak-1
  • The file Si_bulk8.out contains the main output of the job.
  • Si_bulk8-RESTART.wfn is the final Kohn-Sham wavefunctions from the calculation.
  • Si_bulk8-RESTART.wfn.bak- records the Kohn-Sham wavefunctions obtained from the -th previous SCF step; in this case, Si_bulk8-RESTART.wfn.bak-1 contains the wavefunctions obtained from the last SCF step.
  • Should the reader want to start a new calculation from the previous calculated wavefunctions, they need to change the SCF_GUESS keyword in SCF subsection of FORCE_EVAL section to RESTART:
    1
    SCF_GUESS RESTART
    provided that the new calculation shares the same PROJECT_NAME as the one that generated the wavefunctions.
  • Otherwise, we would need to rename the restart wavefunction files to correspond to the project name of the new calculation.
  • We now look at the main CP2K output in more detail.
    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
    Number of electrons:                                                         32
    Number of occupied orbitals: 16
    Number of molecular orbitals: 16

    Number of orbital functions: 104
    Number of independent orbital functions: 104

    Extrapolation method: initial_guess


    SCF WAVEFUNCTION OPTIMIZATION

    Step Update method Time Convergence Total energy Change
    ------------------------------------------------------------------------------
    1 NoMix/Diag. 0.40E+00 0.6 0.75558724 -32.2320848878 -3.22E+01
    2 Broy./Diag. 0.40E+00 1.1 0.05667976 -31.1418135481 1.09E+00
    3 Broy./Diag. 0.40E+00 1.1 0.09691469 -31.1974003416 -5.56E-02
    4 Broy./Diag. 0.40E+00 1.1 0.00245608 -31.3378474040 -1.40E-01
    5 Broy./Diag. 0.40E+00 1.1 0.00235460 -31.3009654398 3.69E-02
    6 Broy./Diag. 0.40E+00 1.1 0.00007565 -31.2972158934 3.75E-03
    7 Broy./Diag. 0.40E+00 1.1 0.00009004 -31.2977293749 -5.13E-04
    8 Broy./Diag. 0.40E+00 1.1 0.00000186 -31.2978454163 -1.16E-04
    9 Broy./Diag. 0.40E+00 1.1 0.00000252 -31.2978835492 -3.81E-05
    10 Broy./Diag. 0.40E+00 1.1 5.6405E-09 -31.2978852054 -1.66E-06

    *** SCF run converged in 10 steps ***
  • The above shows a typical output from a self-consistent Kohn-Sham ground state calculation.
  • It states that we are using diagonalisation method with Broyden charge mixing, and it took 10 Broyden mixing steps (each containing a diagonalisation process to obtain the wavefunctions) to reach the required tolerance for self-consistency.
    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
     Electronic density on regular grids:        -31.9999999889        0.0000000111
    Core density on regular grids: 31.9999999939 -0.0000000061
    Total charge density on r-space grids: 0.0000000051
    Total charge density g-space grids: 0.0000000051

    Overlap energy of the core charge distribution: 0.00000000005320
    Self energy of the core charge distribution: -82.06393942512820
    Core Hamiltonian energy: 18.06858429706010
    Hartree energy: 42.41172824581682
    Exchange-correlation energy: -9.71425832315952

    Total energy: -31.29788520535761

    ENERGY| Total FORCE_EVAL ( QS ) energy (a.u.): -31.297885372811002


    ATOMIC FORCES in [a.u.]

    # Atom Kind Element X Y Z
    1 1 Si 0.00000000 0.00000000 0.00000000
    2 1 Si 0.00000000 0.00000001 0.00000001
    3 1 Si 0.00000001 0.00000001 0.00000000
    4 1 Si 0.00000001 0.00000000 0.00000001
    5 1 Si -0.00000001 -0.00000001 -0.00000001
    6 1 Si -0.00000001 -0.00000001 -0.00000001
    7 1 Si -0.00000001 -0.00000001 -0.00000001
    8 1 Si -0.00000001 -0.00000001 -0.00000001
    SUM OF ATOMIC FORCES -0.00000000 -0.00000000 -0.00000000 0.00000000
  • The above shows the results on final energies and forces.
  • One should always check if the total number of electrons calculated from the final electron density, in this case 31.9999999939, is correct.
  • The results show that the force on the atoms are almost zero.
  • This means the system is more or less relaxed, and its geometry is close to its optimal at ground state.
  1. Adding Smearing
  • In the example so far, we have not used any smearing on electron occupation.
  • This is fine for system with a large band gap.
  • However, for metals or systems with a small gap, this may cause the calculation to be unstable, and the self-consistency loop may never converge, due to the discontinuity in the electron occupation function.
  • To add smearing, we need to add the subsection ‘’SMEAR’’ inside subsection SCF:
    1
    2
    3
    4
    &SMEAR ON
    METHOD FERMI_DIRAC
    ELECTRONIC_TEMPERATURE [K] 300
    &END SMEAR
  • This tells QUICKSTEP to use a smearing function for the electron occupation.
  • In this example, we use the Fermi-Dirac smearing function, with the electron temperature being set to 300 K.
  • Note that, in CP2K, one can explicitly define the unit of a given input value by using a unit enclosed in square bracket, such as “[K]”, before the value.
  • This is not all, since smearing may lead to occupation of molecular orbitals in the conduction band, we must tell CP2K to include extra, empty, molecular orbitals into the calculation, which otherwise would be omitted (for reducing computational cost).
  • To do this, we need to add the keyword ‘’ADDED_MOS’’ in the SCF subsection:
    1
    ADDED_MOS 10
  • In this example, we have asked CP2K to not to omit 10 of the lowest empty molecular orbitals in the calculation.
  • It should be noted that given a chosen basis set, there is a maximum number of molecular orbitals, i.e. the number of eigenvectors of the Hamiltonian, one can generate.
  • In theory, the maximum should be the rank of the Hamiltonian generated in the calculation.
  • In the output of the calculation using smearing, we first notice that:
    1
    2
    3
    4
    5
    6
    Number of electrons:                                                         32
    Number of occupied orbitals: 16
    Number of molecular orbitals: 26

    Number of orbital functions: 104
    Number of independent orbital functions: 104
  • unlike in the previous case with no smearing, now 26 molecular orbitals have been used during the calculation.
  • There are a total of 104 basis functions (“atom centred orbitals” spanned by Cartesian Gaussians) used in the calculation for the given basis set, so 26 is well within the limit of the calculation.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     Electronic density on regular grids:        -31.9999999889        0.0000000111
    Core density on regular grids: 31.9999999939 -0.0000000061
    Total charge density on r-space grids: 0.0000000051
    Total charge density g-space grids: 0.0000000051

    Overlap energy of the core charge distribution: 0.00000000005320
    Self energy of the core charge distribution: -82.06393942512820
    Core Hamiltonian energy: 18.06842027191411
    Hartree energy: 42.41184371469986
    Exchange-correlation energy: -9.71419454555998
    Electronic entropic energy: -0.00001687947145
    Fermi energy: 0.20867150262130

    Total energy: -31.29788686349247

    ENERGY| Total FORCE_EVAL ( QS ) energy (a.u.): -31.297887031736590
  • In the final energy section of the output, we notice that there is an extra entropy (TS) term:
    1
    Electronic entropic energy:                                  -0.00001687947145
  • This should be small for the calculation to be a reliable approximation to the zero electron temperature result.
  • The final free energy is the sum of the total DFT energy and the entropic energy. The total DFT energy is given by:
    1
    Total energy:                                               -31.29788686349247
    and the final free energy extrapolated for TS→0 is given by:
    1
    ENERGY| Total FORCE_EVAL ( QS ) energy (a.u.):              -31.297887031736590
  • This is the energy to be quoted as the final result.

cp2k计算Si的单点能

keywords list

cp2k input file

1
2
3
4
5
&GLOBAL
PROJECT Si_bulk8
RUN_TYPE ENERGY_FORCE
PRINT_LEVEL LOW
&END GLOBAL

GLOBAL
PROJECT Si_bulk8
RUN_TYPE ENERGY_FORCE
PRINT_LEVEL LOW

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
&FORCE_EVAL
METHOD Quickstep
&SUBSYS
&KIND Si
ELEMENT Si
BASIS_SET DZVP-GTH-PADE
POTENTIAL GTH-PADE-q4
&END KIND
&CELL
A 5.430697500 0.000000000 0.000000000
B 0.000000000 5.430697500 0.000000000
C 0.000000000 0.000000000 5.430697500
&END CELL
&COORD
Si 0.000000000 0.000000000 0.000000000
Si 0.000000000 2.715348700 2.715348700
Si 2.715348700 2.715348700 0.000000000
Si 2.715348700 0.000000000 2.715348700
Si 4.073023100 1.357674400 4.073023100
Si 1.357674400 1.357674400 1.357674400
Si 1.357674400 4.073023100 4.073023100
Si 4.073023100 4.073023100 1.357674400
&END COORD
&END SUBSYS

FORCE_EVAL #计算能量和受力的部分
METHOD #计算受力的方法,包括FIST,QMMM,QS,NNP,EIP等
STRESS_TENSOR #是否计算晶胞的应力
SUBSYS #控制原子坐标、晶胞参数、元素/原子的基组赝势等信息
CELL #定义晶胞边长,也可以从其他文件里读取晶胞的信息,比如CIF、XSC文件,通过CELL_FILE_FORMATCELL_FILE_NAME
COORD
KIND
ELEMENT
BASIS_SET
POTENTIAL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
&DFT
BASIS_SET_FILE_NAME BASIS_SET
POTENTIAL_FILE_NAME GTH_POTENTIALS
&QS
EPS_DEFAULT 1.0E-10
&END QS
&MGRID
NGRIDS 4
CUTOFF 300
REL_CUTOFF 60
&END MGRID
&XC
&XC_FUNCTIONAL PADE
&END XC_FUNCTIONAL
&END XC

DFT
BASIS_SET_FILE_NAME
POTENTIAL_FILE_NAME
QS #控制DFT等计算的一些参数精度
EPS_DEFAULT
EXTRAPOLATION #控制波函数外推的一些方法,建议用ASPC
EXTRAPOLATION_ORDER #一般用默认值3
MGRID
NGRIDS #NGRIDS控制的就是几套网格,每套网格的截断能不一样,一般用默认值NGRIDS 4
CUTOFF #CUTOFF是整体网格精度的最高值,单位是Ry(Rydberg),设置取决于体系中元素的种类,默认280 Ry
REL_CUTOFF #REL_CUTOFF 参数控制有多少网格点落到最精细的级别,默认为40 Ry,一般设置到50 或60 Ry
XC
XC_FUNCTIONAL
PADE
PBE
VDW_POTENTIAL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    &SCF
SCF_GUESS ATOMIC
EPS_SCF 1.0E-7
MAX_SCF 300
&DIAGONALIZATION ON
ALGORITHM STANDARD
&END DIAGONALIZATION
&MIXING T
METHOD BROYDEN_MIXING
ALPHA 0.4
NBROYDEN 8
&END MIXING
&END SCF
&END DFT
&PRINT
&FORCES ON
&END FORCES
&END PRINT
&END FORCE_EVAL

SCF
SCF_GUESS
EPS_SCF
MAX_SCF
DIAGONALIZATION
ALGORITHM
MIXING
METHOD
ALPHA
PRINT
FORCES
DOS
PDOS
MULLIKEN
ELF_CUBE
MO_CUBES

Collection of CP2K Exercises

已经完成翻译和学习的CP2K exercises

  1. WO3的投影态密度和能带结构(2017_uzh_cmest)Projected density of states and Band structure for WO3
  2. 石墨烯和h-BN的投影态密度(2016_uzh_cmest)Projected density of states for graphene and h-BN

cp2k练习

经典分子动力学模拟NaCl在水中的溶解
Si单胞的单点能计算
单个水分子的几何优化
NaC团簇的几何优化
乙酸与锐钛矿 TiO2 的两种可能结合模式
大量液态水的AIMD

CP2K Exercises

2021

Statistical Mechanics and Molecular Simulations (UZH)

2020

International Winter School on Electronic Structure Calculations (UPB,PC2)

2019

CONEXS Summer School (Newcastle University)

2018

CP2K-UK Summer School (UoL,STFC)

Introductory exercises:

Calculating Energy and Forces using QuickStep
Accuracy parameters: Converging CUTOFF and REL_CUTOFF
Converging CUTOFF for a more realistic problem
Sensible SCF setups
Geometry optimisation in CP2K

Intermediate exercises:

Geometry and cell optimization of NaCl clusters
‘Surface Science’ using local GGA functionals
Ab initio MD of bulk liquid water
Hybrid functionals and ADMM
Wavefunction correlation: MP2 & RPA
Linear scaling DFT
QMMM using GEEP
Excited States

Extended exercises:

Metadynamics
QM/MM
Nudged Elastic Band
Classical forcefields (CHARMM)

2017

Molecular and Materials Modelling (ETHZ)

2016

CP2K Summer School 2016 (KCL)

Introduction and Overview of CP2K
Running CP2K Calculations
Introduction to Exercises
Moving atoms - AIMD, geometry optimisation

For CP2K Beginners

Short ‘HowTo’ exercises on various basic functions of CP2K:
Single-point energy & force calculation using DFT
How to converge the total energy w.r.t. the CUTOFF and REL_CUTOFF
Experiment with SCF settings:
How to run geometry optimisation

Intermediate Exercises

Geometry and cell optimisation of NaCl clusters
‘Surface Science’ using local DFT
Running ab initio MD of liquid water
Hybrid functional calculations and dispersion corrections
Linear Scaling DFT
Electron correlation: MP2 and RPA
QM/MM using GEEP
Excited state calculations

Extended Exercises

Metadynamics calculations
QM/MM of Urea in water
Adsorption on metallic surfaces (Nudged Elastic Band)
Force-field calculations on a protein
• Also VIBRATIONAL_ANALYSIS, NMR, X-Ray, DFT+U
• In /home/y14/shared/cp2k

2015

Molecular and Materials Modelling (ETHZ)

2014

Molecular and Materials Modelling (ETHZ)

Lecture 5

Determination of melting temperature of copper from molecular dynamics

Molecular Simulations (UZH)

Math Formula

量子力学假设

参考资料 量子化学教程 黄明宝 p65
假设1 体系的状态用坐标和时间的函数$\Psi$来描述。这个函数称为状态函数或波函数,它包含关于体系的可确定的全部知识。
波函数应是单值的、连续的和平方可积的品优函数。这些数学要求是关于坐标变量的。关于波函数随时间的变化见“假设6”及有关论述。假设中的最后一句话的含义是:可以由波函数来计算体系的各种性质(见假设5)。由于波函数描述的是体系的状态(如基态和激发态),严格来说应是:可以计算波函数所描述的体系的那个状态的各种性质。如果涉及微观粒子的自旋,波函数还要包含自旋坐标。

假设2 每个物理量对应一个线性厄米算子。这个算子是这样构造的:用笛卡尔坐标和对应的(线)动量的分量写出物理量的经典力学表达式,然后把每个坐标$x$代以算子$x$,而每个动量分量$p_{x}$代以算子“$-i\hbar \dfrac{\partial }{\partial x}$”。
对算子厄米的要求来源于“物理量的测量平均值应是实数”的要求;对算子的线性要求与“态的叠加”相关联。量子力学的算子之间存在着“可对易”或“不可对易的关系”。

假设3 从物理量$A$的测量可得到的仅有可能值是下列本征方程中的本征值$a_{i}$:
$ A \varphi {i}=a{i}\varphi _{i}$
式中算子$A$对应于物理量$A$。如果体系的状态函数$ \Psi= varphi_{i}$,则测量有确定值——本征值$a_{i}$;如果$\Psi$不是算子$A$的本征函数,则测量没有确定值(不能确定得到哪个本征值)。

$$

01 算子
1.1 括号标记法
一个波函数取复共轭再与另一个波函数相乘后的积分,可使用一种括号标记
$ \left \langle \psi _{1} | \psi _{2} \right \rangle = \int \psi _{1} ^{\ast } \psi _{2} \mathrm{d} \tau $
括号运算规则
$\left \langle \psi | \psi \right \rangle \ge 0$
$ \left \langle \psi {\tiny 2} | \psi {\tiny 1} \right \rangle = \left \langle \psi {\tiny 1} | \psi {\tiny 2} \right \rangle ^{*} $
$ \left \langle a | b+c \right \rangle = \left \langle a | b \right \rangle+\left \langle a | c \right \rangle $
$ \left \langle a +b | c \right \rangle = \left \langle a | c \right \rangle+\left \langle b | c \right \rangle $

$ \left \langle a | \lambda b \right \rangle = \lambda \left \langle a | b \right \rangle $
$ \left \langle \lambda a | b \right \rangle = \lambda ^{*} \left \langle a | b \right \rangle $

算子是一种规则,它把给出的某个函数变成另外的对应函数
$ Au(x)=v(x) $
平方根算子SQRT, $ SQRTu(x)=[u(x)]^{1/2} $

微分算子, $ D=\mathrm{\frac{d}{dx} }u(x) $

Laplace算子$ \bigtriangledown ^{2} $, $ \bigtriangledown ^{2} u(x,y,z) = \partial ^{2} u/\partial x ^{2} + \partial ^{2} u/\partial y ^{2} + \partial ^{2} u/\partial z ^{2} $

$ $

$\mathit{\hat{H}\Psi } \left ( r,R \right ) =\mathit{E\Psi } \left ( r,R \right ) $

波函数归一化
$\int_{-\infty }^{\infty } \left | \Psi (x,t) \right |^{2} \mathrm{ dx} =1$

一维空间单粒子的含时薛定谔方程
$ -\dfrac{\hbar }{i}\dfrac{\Psi \left( x,t\right) }{\partial t}=\dfrac{\hbar ^{2}}{2m}\dfrac{\partial ^{2}\Psi \left( x,t\right) }{\partial x^{2}}+V\left( x,t\right) \cdot \psi \left( x,t\right)$

方程归一化条件
$ \int ^{\infty }_{-\infty }\left| \Psi \left( x,t\right) \right| ^{2}dx=1 $

引进哈密顿算子
$ H=-\dfrac{\hbar ^{2}}{2m}\dfrac{\partial ^{2}}{\partial x^{2}}+V\left( x,t\right) $

含时薛定谔方程可以写成
$ -\dfrac{\hbar }{i}\dfrac{\partial \Psi \left( x,t\right) }{\partial t}=H\Psi \left( x,t\right) $

假设势能函数不含时间,上述方程进行以下分离变量处理
设 $ \Psi {}\left( x,t\right) =f\left( t\right) \psi \left( x\right) $

$ -\dfrac{\hbar }{i}\dfrac{df\left( t\right) }{dt}\cdot \psi \left( x\right) =-\dfrac{\hbar ^{2}}{2m}\dfrac{d^{2}\psi \left( x\right) }{dx^{2}}\cdot f\left( t\right) +V\left( x\right) f\left( t\right) \psi \left( x\right) $

$ $

$ $

$ $

$ $

$ $

$ $

Main Page

谷歌—-必应—-BingChat—-知乎—-小红书—-搜狗—-搜狗微信—-百度—-Wikipedia—-DuckDuckGo—-今日头条—-Yandex—-Yahoo—-Duckduckgo—-DeepL翻译—-谷歌翻译—-百度翻译—-有道翻译—-阿里翻译—-必应翻译—-彩云小译—-一帆文档翻译—-福昕翻译—-全国图书馆参考咨询联盟—-长春数字图书馆—-谷歌图书—-拍照翻译精灵—-cnki学术翻译助手—-letpub期刊查询—-ACS Publishing Center—-Materials Project—-journal of molecular liquids—-aweill_homepage—-Docs.google—-telegra.ph博客—-Gmail—-protonMail—-WordPress登录—-pubpeer—-github中文社区—-TOEFL托福报名

chatGPT—-poe—-Notion AI—-you.com—-forefront—-Phind—-Bard—-claude—-bingImage—-Perplexity.AI—-Andi—-steamship—-cursor—-文心一言—-通义千问—-星火—-chatpdf—-discord_openai—-opeai-API—-cloudflare—-vercel—-namesilo—-namecheap—-domains.google—-squarespace—-DNS查询—-腾讯域名注册控制台—-腾讯DNSPod 管理控制台—-telegram—-labs.openai.—-ip138—-网站标识符iconfinder—-ipapi.co—-ipdata.co—-whoer.net—-哪煮米—-ipaddress—-ip-skk—-hyperbeam.com—-在线云浏览器—-similarsites.com—-tcp.ping.pe—-VirusTotal

vercel-index—-github-index—-chrome-extension—-firefox-extension—-edge-extension

Khan Academy—-TED-Ed—-duolingo—-Vox

scikit-learn教程—-vue.js

ISBN生成—-paypal—-Google Pay—-京东—-amazon—-Google play
quillbot—-句子改写wordtune—-freethesaurus—-turnitin—-期刊缩写—-迅捷—-CAS期刊缩写

Latex公式编辑器—-GoJS图表组件—-MyscriptLatex—-HTML在线工具—-知犀思维导图—-coggle—-xmind—-常用LaTex字符与公式速查—-MathpixSnip—-白描网页版—-sms-activate—-拾色器—-RGB-HEX—-SVG在线查看器

多功能元素周期表—-单质密度—-双原子键长—-原子范德华半径—-相对原子质量—-离子半径查询—-空间群查询—-CRC_Handbook_Chem.&Phys.103rd—-量化常用单位换算—-ValenceElectrons

谷歌学术—-crossref—-doi.org—-PubMed—-科研通—-科研通Ablesci—-药研导航—-iData谷歌学术—-必应学术—-Semantic学术—-X-mol—-百度学术—-ResearchGate—-proquest—-ORCiD—-AA—-学术搜索—-谷禾信息—-MaiPDF—-微信公众平台—-寇享直播—-微信网页版—-pdf合并_smallpdf—-图片转Excel—-localhost:2000

TheHiddenWiki—-OnionLinks—-DuckDuckGo—-Z-Library—-Facebook—-NewYorkTimes—-SecureDrop—-onion666—-

腾讯ECS导航—-digitalocean—-aws.amazon—-微软Azure—-cloudcone—-racknerd—-racknerd控制面板—-my.vultr—-阿里云开发者社区—-阿里云控制台—-nextCloud—-腾讯云控制台—-腾讯云服务器—-华为云服务器—-Ucloud优刻得—-甲骨文云服务器—-外网面板地址—-百度云服务器—-腾讯云根目录—-云音乐—-doVPS

科学网博客—-education.github—-Github网站—-Gitee—-GiteeCodes—-MathFormula—-DownGit—-aweill_github—-Fx Coudert
gitbook网站—-gitbook_home—-gitbook_github—-github—-腾讯微云—-腾讯文档—-北京超算—-中科曙光

TIOBE—-鱼C论坛—-吾爱破解论坛—-CSDN社区—-github—-GitLab—-hostloc—-mjj-bbs—-博客园—-Leetcode—-力扣—-kaggle—-Coggle数据科学—-Stackoverflow—-OSCHINA社区—-Segmentfault社区—-Docker端口IP—-Dockerhub—-Python包索引—-python3文档—-php手册—-正则表达式在线测试—-yescaptcha—-ipip查询—-bejson—-豌豆代理—-蜜蜂代理—-崔庆才博客—-Pexels—-开放代理—-崔庆才代理池

Runoob_Java—-Java博客学习_Github—-Java后端知识图谱_Github—-CyC2018_Github_Java—-JavaGuide_Github—-AdvancedJava_Github—-JavaFamily_Github—-LeetCode_Github—-AwesomeJava_Github—-ToBeJaver_Github—-IntelliJ教程—-Java_Github—-FullstackTutorial_Github—-CS-Book—-Java程序员眼中的Linux—-Java-Tutorial_Github—-FreeBooks_Github—-科技周刊_github

VMess博客—-阮一峰_科技爱好者周刊—-中科院电子杂志

perl菜鸟教程—-w3schools

美团民宿—-携程民宿—-

Sentences—-Papers—-MD-Note—-AIMD-Note—-Machine Learning-Note—-Gaussian-Note—-思想家—-学者方向—-Archives

SCIHUB_1 2 3 4—-iData—-读秀—-知网—-知网海外版专利下载—-大木虫—-sci-hub—-Library Genesis—-libgen.fun——–z-library—-ITeBooks—-Z-Lib+30—-读秀—-pdfDrive—-科学文库—-浙江图书馆—-广西图书馆—-首都图书馆—-广图电子资源—-E-book—-资源帝—-罗马盘—-图书创世纪—-Annas-Archive—-cse.google—-myanonamouse—-zlib_freedit—-pirateBay
如风搜—-鸠摩搜书
ScienceDirect—-ACS—-Springer—-RSC—-Wliey—-Web of Science

Nature—-Science—-JACS—-Angewandte—-PRL—-Separation and Purification Technology—-

Dr. Michele Parrinello—-Jürg Hutter Group—-法国-Fx Coudert—-德国-Prof. Dr. Stefan Grimme—-瑞典-Mattias Edén—-美国-Mathieu Bauchy—-美国-Jincheng Du—-澳大利亚-Yuan-Mei—-波兰-Paweł Stoch—-美国伍斯特理工Deskins—-MindSpore昇思

中科大杨金龙组—-北师方维海组—-北大吴云冬组—-北大数院鄂维南主页—-港中文帅志刚组—-吉大马琰铭组—-中科大李震宇组—-化物所韩克利组—-过程所李静海组—-厦大程俊组—-杜克杨伟涛

Resources Policy—-The Extractive Industries and Society—-Asian Studies Review—-MMTB检索—-Read

材料基因组计划—-清华大学—-北京大学—-北京市通州区委—-诺贝尔奖官网—-中国科学院—-国家自然科学基金委—-中国载人航天工程—-中国化学会—-IUPAC国际纯粹与应用化学联合会—-中国材料研究学会—-中国金属学会—-环球科学—-领研网—-国科大信息网—-中科大教务处培养方案—-过程所—-国科大教育云系统—-中科院教育云—-中科院地质与地球物理研究所—-中科院邮件系统—-网易邮箱—-139邮箱—-Microsoft账户—-icloud—-outlook邮件—-gmail—-yahoo邮箱—-世界时间—-谷歌地图—-onedrive—-中国商标网
—-tesla官网

计算化学公社—-思想家公社的门口—-Multiwfn—-量子化学软件中文网—-Gaussian—-expchem3—-墨灵格
VASP官网—-VASP国内代理源资科技—-VASP manual—-vaspkit中文手册—-Vaspkit论坛—-vaspkit官网—-vaspkit手册ECS

庚子计算—-cp2k官网—-Matsci论坛—-cp2k谷歌论坛—-cp2k Exercises—-cp2k关键词—-单点能计算—-cp2k/Fist—-cp2k/QS—-cp2k/QMMM—-cp2k官网论文—-知乎cp2k入门

Plumed—-CALYPSO—-DeePMD-kit

lammps—-模拟小窝—-Interatomic Potentials Repository—-ReaxFF_Dr.Adri—-哲●科●文Jerkwin_李继存

Quantum-Espresso官网—-2019-Summer-School—-TUTORIALS—-ICTP官网—-ORCA官网

Factsage官网—-科学计算软件网—-HSC-Chemistry—-Thermo-Calc—-科研工具—-科研工具ECS

CPMD官网—-CPMD介绍—-

世事如棋—-归档—-Learn Vasp the Hard Way—-开发者文档

中科大李震宇《量子化学》课程

全球高清摄像头—-抖音—-Tiktok—-抖音视频在线解析—-B站—-Spotify—-Google-Podcast—-BBC sounds—-music.youtube—-Youtube—-Youtube字幕下载—-Netflix奈飞—-IMDb—-RottenTomatoes—-油管视频下载—-TED—-TED下载—-qq音乐—-网易云音乐—-喜马拉雅—-蜻蜓FM—-MP3音乐下载—-可可英语—-ANG动漫—-微博—-腾讯视频—-88影视—-豆瓣—-高德地图—-腾讯地图—-在线调色—-Hexo主题

百度云—-蓝奏云—-阿里云盘

维基百科—-twitter—-V2EX—-facebook—-Instagram—-Quora—-Pinterest—-telegram—-tumblr—-Reddit—-OpenAI—-互联网档案馆—-emergentmind—-1link.fun—-Hacker News—-ScientificAmerican

鸟哥的Linux私房菜—-git教程—-Shell教程—-MarkDown语法—-W3school-Python语法—-RUNOOB-Python3教程—-matlab语法—-Mathworks中国官网—-《Python Cookbook》3rd Edition—-简单教程—-scikit-learn—-计算机书单:小资源栈—-Linux命令大全—-图灵社区—-人民邮电出版社—-异步社区—-NoStarch—-O’Reilly—-oreilly—-manning
—-Packt—-Apress-springer—-千锋教育—-科学出版社—-华章教育

ASE—-moltemplate—-Gnuplot—-ISAACS—-R.I.N.G.S.—-Jmol—-VESTA—-OVITO—-VMD—-Chrimerax—-ChemDraw官网—-Packmol—-ABCluster—-Mercury—-知云翻译—-WebPlotDigitizer—-Anaconda官网—-USPEX结构预测—-MDtraj—-Atomsk官网—-MD Analysis—-Pymol—-Jmol-manual—-
esaygui模块—-Processing—-Fiddler4—-PyCharm—-Eclipse-Java—-Oracle-JDK—-MinGW-W64 GCC—-strawberryperl—-WinSCP官网—-Open_MPI—-Intel—MKL官网—-Dev-C++—-Rstudio—-R语言—-MySQL—-jetbrains—-MSYS2—-Java-JDK

Sublime Text—-Notepad++—-TRAVIS—-ISAACS—-git—-Node.js—-Typora—-VS Code—-KMCounter键盘统计—-WinDjView—-SumatraPDF—-FastStone Capture

Yandex—-Opera浏览器—-火狐浏览器—-chrome—-Edge—-沙盘软件sandboxie—-TorBrowser—-TorBridges—-adspower

Bing Chat for All Browsers—-Proxy SwitchyOmega

MaterialsSpringer材料数据库—-AMSD-美国矿物学家晶体结构数据库—-晶体学开放数据库COD—-费米维基—-ICSD无机晶体结构数据库—-Materials Project
NIST美国国家标准与技术研究院
NIST 标准参考数据库物理和化学参考数据期刊重印本—-JARVIS—-Atomly—-OQMD—-AFLOW

TOEFL官网—-微推

W—-lantern—-kingfast—-panda—-leiting—-蚂蚁—-一支红杏—-dukou—-clash for windows—-夏时国际加速器—-quickq—-v2fly用户手册—-v2ray高级篇

古诗文网—-知犀思维导图模板—-—-

3D小车

中科院科创计划—-实验室安全问答库—-南开大学结构化学课—-基金查询

BSE基组数据库—-Turbomole程序的基组库—-Gaussian基组

黄柳青—-Joshua Goings

课题组

—-天然产物研究与开发课题组,中国药科大学天然药物化学系,王小兵
—-计算分子与材料科学课题组,合肥微尺度物质科学国家研究中心,李震宇,化学反应和物理过程(特别是成核和生长)的机理,功能材料的理论设计和计算表征,以及电子结构计算和分子模拟的方法
—-地质与地球物理研究所行星内部物质成分与性质组,张毅刚,地球早期核幔分异过程元素配分的第一原理分子动力学研究
—-
—-
—-
—-
—-
—-
—-
—-

沧海桑田
路虽远行则将至,事虽难做则必成
真正的英雄主义只有一种,那就是在认清生活真相之后依然热爱生活。–罗曼 罗兰
不思,故有惑;不求,故无得;不问,故不知。 ——曾国藩
盛年不重来,一日难再晨,及时当勉励,岁月不待人。 ——陶渊明
古人不见今时月 今月曾经照古人。

惊喜,热爱,努力,快乐

人有逆天之时,天无绝人之路。 ——冯梦龙《醒世恒言》
不是一番寒彻骨,怎得梅花扑鼻香。——冯梦龙《醒世恒言》
无论什么事,如果不断收集材料,积之十年,总可成一学者。——鲁迅

生活会有望穿秋水的等待,也会有意想不到的惊喜。
学以致用才是学知识的真正目的
无论何人无论何时,人们总要在乌云周围寻索着浪漫的微光活下去。–村上春树

如果要用的知识量是一定的话,会的越多,不会的就越少,人生应该是越来越容易的。
失败越多,离成功就越近,你所要做的就是从失败中学习,然后加快脚步。

创建一个元组很简单,只要在( )中添加元素,并使用( )隔开即可。(括号,逗号)
可以直接使用一对( )创建一个空元祖。(小括号)
元组与字符串类似,下标索引从( )开始,可以进行截取,组合等。( 0 )
元组中的元素是不可变的,当修改元组的元素时会抛出( )异常。( TypeError )

函数、传参、返回值,和代码复用

函数、传参、返回值,和代码复用

在这一章节中,我们主要学习函数(Function),我们知道,”function” 一词本身就有“功能”的意思,便于理解,可以直接将“函数”视为“功能”。出于严谨,下文中我们仍然统一称之为“函数”。

内置函数

我们之前已经使用过一些函数,如 printinput,它们属于内置函数,BIF(built-in functions),顾名思义,是 Python 自带的一些函数,我们可以直接使用它们。以 print 函数为例,包括但不限于以下的调用形式都是合法的。

1
2
3
4
5
6
7
8
9
10
11
>>> print("hello, world!")
hello, world!
>>> print(1024)
1024
print(6 + 4)
10
>>> print(True)
True
word = "hello"
print(word)
hello

自定义函数

既然存在内置函数,那意味着也存在自定义函数,确实是这样,本章节也围绕自定义函数展开。

定义

一般的,通过 def 关键字定义函数,括号 () 内为其参数 (Param),return 关键字用于返回值。

先来看一个最简单的自定义函数:

1
2
3
def add(a, b): # 不要忽略冒号
result = a + b # 用 result 变量保存参数的和
return result

def 定义了一个函数名为 add 的自定义函数,它有名为 a 和 b 的两个参数,并返回 result 的值。

对于这种逻辑简单的函数,还可以直接写成这样:

1
2
def add(a, b):
return a + b

函数光是定义是永远不会运行的,还需要我们调用:

1
2
3
4
def add(a, b):
return a + b

print(add(4 + 6)) # 10

函数体内还可以做更多的逻辑判断,比如我们实现一个求绝对值的函数:

1
2
3
4
5
6
7
8
9
def abs(a):
if a >= 0:
return a
else:
return -a

print(abs(10)) # 10
print(abs(-8)) # 8
print(abs(0)) # 0

这个函数还可以写成这样:

1
2
3
4
def abs(a):
if a < 0:
a = -a
return a

如果函数不需要返回值,则 return 关键字不是必须的:

1
2
3
4
def greet():
print("Hello!")

greet() # Hello!

在这个问好函数 greet 中,由于不需要传参,所以括号内没有参数。

形参和实参

对于函数的“参数”,还有“形参”和“实参”的概念,即形式参数和实际参数。我们仍然以上文的 abs 函数为例:

1
2
3
4
5
6
7
def abs(a):
if a < 0:
a = -a
return a

number = -10
abs(number) # 10

在这个例子中,abs(number)abs 函数传入了 number 的值,即 -10 现在是 abs 函数的实参;定义时,abs(a) 中的 a 也是参数,但为形参,仅用来代表实际传入的 -10

简单来说,形参的值是不固定的,由传入的实参确定。

明白了这一点后,我们也能理解下面的代码了:

1
2
3
4
5
6
7
8
def fun(a):
a = 10

a = 8
print(a) # 8

fun(a)
print(a) # 8

若让改变变量 a 起效,应这样写:

1
2
3
4
5
6
def fun(a):
return 10

a = 8
a = fun(a)
print(a) # 10

代码复用

自定义函数最常被用作封装功能,便于代码复用。

在上一章节的最后,我们提出前台登录的程序还有很大的改进空间,其中关键的一点就是代码可维护性差。现在,我们用新学的知识继续改进它:

1
2
3
4
5
6
7
8
9
def get_age_from_id():
id = input("请输入你的身份证号:")

while not id.isdigit() or len(id) != 18:
id = input("输入有误,请重新输入身份证号:")

birth_year = int(id[6:10])
age = 2020 - birth_year
return age

我们将从身份证获取年龄的代码抽象出来,封装成一个自定义函数,这样,这样,我们只需要关注业务逻辑部分,而无需在意具体的实现细节:

1
2
3
4
5
6
7
8
9
10
11
12
13
name = input("请输入你的姓名:")
age = get_age_from_id()

if name == "莉莉":
print("小姐里面请!")
elif age > 60:
print("大爷先回家歇歇吧!")
elif age == 40:
print("贡献您,今日消费全免,里面请!")
elif age >= 18: # 当 age 小于等于 60 并且 大于等于 18 时
print("欢迎您的到来,里面请!")
else: # 其余情况
print("小孩子回家玩去")

这样做的好处是很多的。如果我们后面的代码中需要重复使用这个功能,不用每次都拷贝一遍代码,若是需要修改其中的某些细节,也不需要挨个修改。

实战

在上一章节中,我留了一个课后练习题:实现一个猜数字的小游戏。你不知道有练习题?练习题会在编程小窝群中发布,我会挨个批改,帮助大家在实战中精进。

有一个同学交了这样的一份答卷:

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
import random

given_number = random.randint(0, 9)
input_number = input("请输入您猜的数字:")

while not input_number.isdigit() or len(input_number) > 1:
if not input_number.isdigit():
input_number = input("输入有误,请重新输入:")
else:
input_number = input("输入有误,请输入 0 到 9 之间的个位数:")

input_number = int(input_number)

count = 1

while input_number != given_number:
if input_number > given_number:
input_number = input("猜大了,你已经输错了 %d 次" % count)

while not input_number.isdigit() or len(input_number) > 1:
if not input_number.isdigit():
input_number = input("输入有误,请重新输入:")
else:
input_number = input("输入有误,请输入 0 到 9 之间的个位数:")
input_number = int(input_number)
else:
input_number = input("猜小了,你已经输错了 %d 次" % count)

while not input_number.isdigit() or len(input_number) > 1:
if not input_number.isdigit():
input_number = input("输入有误,请重新输入:")
else:
input_number = input("输入有误,请输入 0 到 9 之间的个位数:")
input_number = int(input_number)

count += 1

print("猜测正确")

本来是很简单的一个功能,写了这么一大坨,这也难怪,因为他当时还不会使用自定义函数,现在我们着手改进它:

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
import random


def get_number():
number = input("请输入您猜的数字:")

while not number.isdigit() or len(number) > 1:
if not number.isdigit():
number = input("输入有误,请重新输入:")
else:
number = input("输入有误,请输入 0 到 9 之间的个位数:")

return int(number)


given_number = random.randint(0, 9)
input_number = get_number()

count = 1

while input_number != given_number:
if input_number > given_number:
print("猜大了,你已经输错了 %d 次" % count)

input_number = get_number()
else:
print("猜小了,你已经输错了 %d 次" % count)

input_number = get_number()

count += 1

print("猜测正确")

是不是一下子就简洁了许多呢? 👏

扩充功能

函数非常灵活,我们可以通过传入多个参数扩充功能:

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
def get_id():
id = input("请输入身份证号:")

while not id.isdigit() or len(id) != 18:
id = input("输入有误,请重新输入身份证号:")

return id


def get_info_from_id(id, info):
if info == "age":
birth_year = int(id[6:10])
return 2020 - birth_year
elif info == "from":
return id[0:6]
elif info == "sex":
sex = int(id[-2])
if sex % 2 == 0: # 为偶数时
return "女"
else:
return "男"


id = get_id()
print(id)

print("年龄:", get_info_from_id(id, "age"))
print("性别:", get_info_from_id(id, "sex"))
print("出身地编号:", get_info_from_id(id, "from"))

小结

利用自定义函数,对代码进行封装,代码更加简洁,并且业务逻辑部分与技术实现部分相解耦,降低我们组织代码时的心智负担,编写更具有可读性,并且高度可维护的代码。

函数非常灵活,功能也无比强大。在今后的代码编写中,要多加思考,是否可以利用函数优化代码。

用新知识解决旧问题

用新知识解决旧问题

在上一章节中,我们编写的程序还有很大的改进空间,在这一章节中,我们将逐一解决这些问题。

首先,之前的程序永远只会执行一次判断,如果用户输错了格式,也没有机会修正。我们应该要让程序不断的尝试获取输入,直到输入格式确实符合要求为止。我们在编写时并不知道何时输入值是合法的,这时就需要循环语句了。

循环语句

先来看一个最简单的 while 循环示例:

1
2
3
4
5
6
7
i = 1 # 初始化申明一个变量

while(i <= 3): # 当 i 的值小于等于 3 时
print("这是第 %d 次打印" % i)
i = i + 1 # 让 i 增加,如果没有这一句,将成为死循环

print("循环已完毕!")

程序的运行结果为:

1
2
3
4
这是第 1 次打印
这是第 2 次打印
这是第 3 次打印
循环已完毕!

为了便于理解 while 循环,我做了个图解:

利用循环语句,我们将之前的程序改写为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
name = input("请输入你的姓名:")
age = input("请输入你的年龄:")

while not age.isdigit(): # 当 age 不为纯数字时
age = input("输入有误,请重新输入你的年龄:") # 重新获取 age 的值

age = int(age)

if name == "莉莉":
print("小姐里面请!")
elif age > 60:
print("大爷先回家歇歇吧!")
elif age == 40:
print("贡献您,今日消费全免,里面请!")
elif age >= 18: # 当 age 小于等于 60 并且 大于等于 18 时
print("欢迎您的到来,里面请!")
else: # 其余情况
print("小孩子回家玩去")

逻辑运算符

while not age.isdigit(): 语句表示当 age 不为纯数字时循环。

在编程语言中,通常用 ! 表示”否“,如用 != 表示“不等于”,相应的,上面的语句则要用 while (!age.isdigit()) 表示;而 Python 则支持用 not 关键字表示“否”,这非常直观并且人性化,同时体现了 Python 的优雅之处。

除了 not 外,Python 中还有“并”: and 、“或”: or,分别对应 &&||,这些被称为逻辑运算符。

经过循环语句后,我们可以确保 age 的值已为纯数字,可以放心的使用 int() 函数将其转换为整数类型;原本用于处理格式不正确的 else 语句也要去掉。

解决了上面的问题,我们再来看如何更严谨的确定年龄。

直接让用户输入年龄的形式并不合适,一种更好的方法是从身份证号中确定年龄。我们知道,身份证号由确定的格式组成,从身份证号中我们可以获取很多信息。具体来说,身份证号前六位表示地址,接下来的八位表示出生日期,再接下来三位为顺序码,最后一位为校验码。

身份证的最后一位是根据前 17 位通过校验算法计算出来的,改变原本的任何一位都会导致校验码不符。通过身份证号,我们就可以较严谨的确定年龄了。

程序处理时间和日期,可能会比大多数人想象的要复杂,为了降低学习的心智负担,这里我们会尽可能简化从身份证号中获取年龄的算法。

假定现在获取的身份证号为 510101200106180395,我们只提取 2001 这个年份,直接与当前的年份 2020 相减计算出年龄。

字符串截取

该如何获取 "510101200106180395" 中的字符呢,其实“字符串”顾名思义就是由“字符”连成的串,我们直接通过位次就可以获取到其中的字符

1
2
3
4
5
6
word = "hello"
print(word[0]) # h
print(word[1]) # e
print(word[2]) # l
print(word[3]) # l
print(word[4]) # o

没错,程序员就是从 0 开始数数的 😅

可以看到,上面的代码非常繁琐,而且假如需要提取 1000 位,难道还写 1000 行吗?这显然是不可能的,我们可以借助刚学的循环来完成:

1
2
3
4
5
word = "hello"
i = 0
while i < len(word):
print(word[i])
i += 1
  • len() 函数用于获取数据长度,len("hello") 的结果自然是 5
  • i += 1i = i + 1 的简写,二者等价。

那么,结合之前所学的条件语句,提取出生年份的代码就可以这样写:

1
2
3
4
5
6
7
id = "510101200106180395"
i = 0

while i < len(id):
if i in (6, 7, 8, 9): # 当 i 为 6, 7, 8, 9 中的任何一个时
print(id[i])
i += 1

if i in (6, 7, 8, 9): 语句用到了 Python 的一些高级特性,我们还没有学,没关系,光这么看就已经非常直观。它和下面的代码等价:

1
2
if i == 6 or i == 7 or i == 8 or i == 9:
pass # 表示忽略

高下立判, 再一次赞叹 Python 的简洁优雅 👏

能不能再简洁一点,更优雅一点?

Python 告诉你:还真能!

1
2
3
id = "510101200106180395"

print(id[6:10]) # 2001

id[6:10] 用到了 Python 的“切片”操作符,它可以用于截取字符串。为了便于理解,我又做了个图解:

现在,改写之前的程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
name = input("请输入你的姓名:")
id = input("请输入你的身份证号:")

while not id.isdigit() or len(id) != 18: # 当 id 不为纯数字时或不满 18 位时
id = input("输入有误,请重新输入身份证号:") # 重新获取 id 的值

birth_year = int(id[6:10])

age = 2020 - birth_year

if name == "莉莉":
print("小姐里面请!")
elif age > 60:
print("大爷先回家歇歇吧!")
elif age == 40:
print("恭喜您,今日消费全免,里面请!")
elif age >= 18: # 当 age 小于等于 60 并且 大于等于 18 时
print("欢迎您的到来,里面请!")
else: # 其余情况
print("小孩子回家玩去")
1
2
3
> 请输入你的姓名:王花花
> 请输入你的身份证号:510101200106180395
欢迎您的到来,里面请!

很好的实现了我们的需求 🥳

小结

不要忘了,我们将判断年龄的算法尽可能简化了,如果以后我们想让算法更完善,或是给程序添加更多的功能,就需要不断的在中间新插入代码,其中有实现我们业务逻辑的部分,也有具体算法实现的部分,相互耦合在一起,非常琐碎复杂。

管理和维护这样的代码,是我们不愿接受的,该如何解决这种问题呢?

将在下一章节中详细展开 😘

解决场景问题

解决场景问题

现在我们来看一个具体场景下的 Python 代码编写。

在上一章节中,我们编写了一段这样的代码:

1
2
3
name = input("请输入你的姓名:")
age = input("请输入你的年龄:")
print("你好,%s 岁的%s!" % (age, name))

执行这段代码时,程序将获取我们输入的名字和岁数,并且按照某种形式打印出来。

场景问题

假设,现在有这么一种场景:某娱乐场所的前台登记系统,需要用户输入自己的姓名和年龄,系统将判断用户是否成年,如果成年则欢迎并放行,否则让用户回家。我们该如何用 Python 来实现这个需求呢?

条件语句

很显然,我们需要对用户输入的年龄进行判断,这就要有请判断语句:ifelse 出场了

1
2
3
4
5
6
age = 10

if age >= 18: # 当 age 大于等于 18 时
print("成年")
else: # 其它情况时
print("未成年")

# 表示注释,Python 中只有单行注释,即与 # 在同一行并在它后面的都是注释,程序在执行时将忽略掉注释,注释的存在是便于程序的编写者和阅读者理解代码的含义与用途。

上面的代码将判断 age 变量的值是否大于等于 18,如果是则打印“成年”,否则打印“未成年”,不要漏掉那个冒号 :

Python 的一大特点是用缩进表示语句块,建议使用四个空格缩进而不是按 tab 键,出于方便,我们可以把 tab 替换成四个空格:

img

通常情况下这就是默认配置

现在我们再利用判断语句将之前的代码改进一下:

1
2
3
4
5
6
7
name = input("请输入你的姓名:")
age = int(input("请输入你的年龄:"))

if age >= 18:
print("欢迎您的到来,里面请!")
else:
print("小孩子回家玩去")

之所以 input("请输入你的年龄:")int() 包裹,是因为 input() 在这里实际上是获取到一个字符串,所以用 int() 强制转换为数字,方便我们下面的判断。我们测试一下:

1
2
3
4
5
6
7
> 输入你的姓名:谭小蝶
> 请输入你的年龄:18
欢迎您的到来,里面请!
=============================
> 输入你的姓名:莉莉
> 请输入你的年龄:8
小孩子回家玩去

可以看到成功了,非常棒。如果这个时候,又有新需求,考虑到身体健康问题,大于 60 岁的用户就不再放行。我们可以改写成这样:

1
2
3
4
5
6
7
8
9
name = input("请输入你的姓名:")
age = int(input("请输入你的年龄:"))

if age > 60: # 当 age 大于 60 时
print("大爷先回家歇歇吧!")
elif age >= 18: # 当 age 小于等于 60 并且 大于等于 18 时
print("欢迎您的到来,里面请!")
else: # 其余情况
print("小孩子回家玩去")

elifelse if 的简写,顾名思义,表示“否则,如果……”,程序会自上而下的依次判断是否符合条件,如果遇到条件符合时,将执行相应的语句,并不再判断接下来的条件,所以先判断的条件要放到更上面。

1
2
3
4
5
6
7
8
9
10
11
> 输入你的姓名:谭小蝶
> 请输入你的年龄:18
欢迎您的到来,里面请!
=============================
> 输入你的姓名:莉莉
> 请输入你的年龄:8
小孩子回家玩去
=============================
> 输入你的姓名:王大爷
> 请输入你的年龄:80
大爷先回家歇歇吧!

测试了一下,符合我们的预期,非常棒哈。

但是,在程序就有非常有名的一句话: 永远不要相信用户的输入 ,万一上面的这个登记系统,用户傻傻的把名字和年龄填反了怎么办?如果遇见捣蛋鬼存心乱填怎么办?

1
2
3
4
> 请输入你的年龄: 莉莉
> 请输入你的年龄: 二十八岁
> 请输入你的年龄: 不便透露
> 请输入你的年龄: bilibili

合法性验证

事实上,仅凭之前的代码,根本无法应对这些情况,所以,我们需要对用户的输入合法性(合理性)也进行判断,改进后的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
name = input("请输入你的姓名:")
age = input("请输入你的年龄:")

if age.isdigit(): # 当 age 由纯数字构成时
age = int(age)
if age > 60: # 当 age 大于 60 时
print("大爷先回家歇歇吧!")
elif age >= 18: # 当 age 小于等于 60 并且 大于等于 18 时
print("欢迎您的到来,里面请!")
else: # 其余情况
print("小孩子回家玩去")
else:
print("你丫的输入了个啥?")

我们对新代码逐一分析,首先在第二行中,我们去掉了 int() ,因为在这里我们还无法判断用户输入的是否为纯数字,如果不是纯数字却执行 int() 可能会报错。

虽然用户也有可能输入诸如 -10 或者 2333 这种显然不可能存在的岁数,不过程序不会报错,而且反正也通不过我们原有的判断,所以暂时不做处理。

age.isdigit() 用于获取是否 age 是否为纯数字,如果是返回 True,否者返回 FalseTrueFalse 都属于“布尔值(Boolean)“数据类型,用于表示真假;理解了布尔值的概念后,我们回过头再去看看 if

1
2
3
4
5
6
7
8
9
10
11
age = "18"
if (age.isdigit()): print("执行啦")
if (5 > 4): print("执行啦")
if True: print("执行啦")
# 以上都为真,都会执行冒号后面的语句

name = "王小二"
if (name.isdigit()): print("我永远不会被调用,哭唧唧")
if (4 > 5): print("我永远不会被调用,哭唧唧")
if False: print("我永远不会被调用,哭唧唧")
# 以上都为假,都看不到它们哭唧唧

实际上,if: 之间的语句被程序判断,返回一个布尔值,最终等价于 if True:if False,前者会执行之后的语句。

另外,在计算机中我们常常用 1 表示”真“,0 表示 ”假“,所以 True 也等于 1,相应的 False 也等于 0

1
2
3
4
>>> True == 1
True
>>> False == 0
True

正如你所注意的,在代码中通常用两个等号 == 表示通常意义上的”相等“,这是避免和一个等号 = 表示的赋值相冲突。

回到之前的前台登记需求,现在我们已经能安全的判断用户输入的年龄,符合我们准入规则的才放行。现在,又有新情况:老板 40 岁生日,他大手一挥,宣布今天跟他同岁的顾客统统免单,并且他年仅 8 岁的小女儿莉莉也允许进入。

还能咋办?有需求咱就改呗:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
name = input("请输入你的姓名:")
age = input("请输入你的年龄:")

if age.isdigit(): # 当 age 由纯数字构成时
age = int(age)
if name == "莉莉":
print("小姐里面请!")
elif age > 60: # 当 age 大于 60 时
print("大爷先回家歇歇吧!")
elif age == 40:
print("贡献您,今日消费全免,里面请!")
elif age >= 18: # 当 age 小于等于 60 并且 大于等于 18 时
print("欢迎您的到来,里面请!")
else: # 其余情况
print("小孩子回家玩去")
else:
print("你丫的输入了个啥?")

由于 if……elif……else…… 自上而下判断,所以我们需要根据情况合理的调整判断顺序,然后测试一下:

1
2
3
4
5
6
7
> 请输入你的姓名:莉莉
> 请输入你的年龄:8
小姐里面请!
=============================
> 请输入你的姓名:葛蛋蛋
> 请输入你的年龄:40
恭喜您,今日消费全面,里面请!

非常好,实现了需求。

更多的思考

老板满意的看着你,表示你可以下班回家休息了。你心满意足的回到家,洗澡的时候脑子里突然冒出了今天写的代码,你突然觉得大段的 if……elif……else…… 看起来啰嗦累赘,还一不小心没理清逻辑就写错条件。

继而你接着想到,给用户输入的机会只有一次,要是用户一不小心输错了,就直接被拒之门外了,这样的用户体验也不好。并且如果有人输小姐的名字假冒她,也被直接放进来了,这显然不行;再者,直接让用户输年龄,不排除有人谎报的,应该用更严谨的方式确定年龄。

该怎么改进上面的问题呢,将在下一章娓娓道来。