def write_LGFCAR(G,mapping,size_1,size_12,size_123,header):
    
    """
    Write a string containing the data for a VASP LGFCAR file. 
    
    Parameters
    ---------- 
    G : LGF matrix (numpy array of shape (size_123,size_2))
    mapping : list, in which mapping[LGF_index] = DFT_index
    size_1 : number of atoms in reg 1
    size_12 : number of atoms in reg 1+2
    size_123 : number of atoms in reg 1+2+3
    header : comment string

    Returns
    -------
    s : string containing the data for a VASP LGFCAR file
    
    """
    
    size_2 = size_12-size_1 

    ## create string which will be written to LGFCAR file
    s = header + '\n'
    ## this next line must be:
    ## <min DFT index reg 2> <max DFT index reg 2> <min DFT index reg 1> <max DFT index reg 3> <total # entries>
    s += '%d %d %d %d %d\n'%(int(min(mapping[size_1:size_12])),int(max(mapping[size_1:size_12])),
                                1,size_123,size_2*size_123)
    
    ## determine the order to output LGF entries
    indexlist = []
    for j in range(size_2):
        for i in range(size_123):
            ## [DFT atom index j, DFT atom index i, LGF row index i, LGF col index j]
            indexlist.append([mapping[j+size_1],mapping[i],i,j])
            
    ## sort indexlist based on DFT atom index j first, then DFT atom index i
    indexlist = sorted(indexlist)
    
    for DFTj,DFTi,row,col in indexlist:
        ## <DFT atom index j> <DFT atom index i> <Gxx> <Gxy> <Gxz> <Gyx> <Gyy> <Gyz> <Gzx> <Gzy> <Gzz>
        Gij = G[row*3:(row+1)*3,col*3:(col+1)*3]
        s += '%d %d %.12f %.12f %.12f %.12f %.12f %.12f %.12f %.12f %.12f\n'%(int(DFTj),int(DFTi),
                Gij[0,0],Gij[0,1],Gij[0,2],Gij[1,0],Gij[1,1],Gij[1,2],Gij[2,0],Gij[2,1],Gij[2,2])
    
    return s