## 10.4 Database Operations

As `NanoCap` uses an sqlite database, the database can be browsed from the command line:

### Saving structures to the local database

`nanocap_add_structure_to_db.py` is an example script to save a fullerene structure to disk. The code is shown below.

    '''
    -=-=-=-=-=-=-=-=-=-=-=-=-=NanoCap=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    Copyright Marc Robinson  2014
    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    A script to construct and add a fullerene to the local database

    Input:
        N_carbon = Number of carbon atoms
        dual_lattice_force_field = force field
                                   for dual lattice
        carbon_force_field = force field
                            for carbon lattice
        dual_lattice_mintol= energy tolerance for
                             dual lattice optimisation
        dual_lattice_minsteps= steps for dual lattice
                                optimisation
        carbon_lattice_mintol=as above for carbon lattice
        carbon_lattice_minsteps=as above for carbon lattice
        optimiser=optimsation algorithm
        seed = seed for initial cap generation

    Output:
        structure is added to local database

    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    '''

    import sys,os,random,numpy
    from nanocap.core.minimisation import DualLatticeMinimiser, \
                                          CarbonLatticeMinimiser
    from nanocap.structures.fullerene import Fullerene
    from nanocap.db.database import Database
    from nanocap.core.output import write_xyz

    N_carbon = 200

    dual_force_field = "Thomson"
    carbon_force_field = "EDIP"
    dual_lattice_mintol=1e-10
    dual_lattice_minsteps=100
    carbon_lattice_mintol=1e-10
    carbon_lattice_minsteps=100
    optimiser="LBFGS"
    seed = 12345

    my_fullerene = Fullerene()
    my_fullerene.construct_dual_lattice(N_carbon=N_carbon,seed=seed)
    my_fullerene.set_fix_pole(False)
    my_fullerene.set_nfixed_to_equator(0)

    Dminimiser = DualLatticeMinimiser(FFID=dual_force_field,
                                      structure = my_fullerene)
    Dminimiser.minimise(my_fullerene.dual_lattice,
                        min_type=optimiser,
                        ftol=dual_lattice_mintol,
                        min_steps=dual_lattice_minsteps)

    outfilename = "C{}_dual_lattice.xyz".format(N_carbon)
    write_xyz(outfilename,my_fullerene.dual_lattice)

    my_fullerene.construct_carbon_lattice()

    Cminimiser = CarbonLatticeMinimiser(FFID=carbon_force_field,
                                        structure = my_fullerene)

    Cminimiser.minimise_scale(my_fullerene.carbon_lattice)
    Cminimiser.minimise(my_fullerene.carbon_lattice,
                        min_type=optimiser,
                        ftol=carbon_lattice_mintol,
                        min_steps=carbon_lattice_minsteps)

    my_db = Database()
    my_db.init()
    my_db.add_structure(my_fullerene,
                        add_dual_lattice=True,
                        add_carbon_lattice=True)

### Loading structures from the local database

`nanocap_load_from_db.py` is an example script to load a set of fullerene dual lattices and save to disk. The code is shown below.

    '''
    -=-=-=-=-=-=-=-=-=-=-=-=-=NanoCap=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    Created: May 23, 2014
    Copyright Marc Robinson  2014
    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    Example script showing how to load from a database.
    Simple example showing how to load fullerene dual lattice

    Input:
        type - the type of structure to return the dual
               for = "Fullerene","Capped Nanotube","Nanotube"

    Output:
        folders for each structure containing xyz files and
        info files.

    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    '''
    import sys,os
    sys.path.append(os.path.abspath(os.path.dirname(__file__)+"/../"))
    from nanocap.db.database import Database
    from nanocap.core.output import write_xyz

    type = "Fullerene"

    my_db = Database()
    my_db.init()

    #let's query for all fullerene dual lattices

    tables = ["dual_lattices"]
    selects = ["id",]
    checks  = { "dual_lattices" : ['type',] }
    data = {"dual_lattices" : {"type" : type}}

    sql,data = my_db.construct_query(data,tables,selects,checks)
    results  = my_db.query(sql,data)
    #out now contains dual lattice IDs of fullerenes
    print results

    for result in results:
        id = result[0]
        structure = my_db.construct_structure(id)
        Nd = structure.dual_lattice.npoints
        folder = "Fullerene_dual_lattice_id_{}_N_{}".format(id,Nd)
        structure.export(  folder=".",
                           save_info=True,
                           save_image=False,
                           save_video=False,
                           save_carbon_lattice=False,
                           save_con_carbon_lattice=False,
                           info_file='structure_info.txt',
                           save_dual_lattice=True,
                           formats=['xyz',])