Issue inserting file into another file at specific line
Show older comments
I am attempting to generate an input file for another program. The input file consists of some parameters, a bunch of atomic positions and then two closing lines.
Following the advice from some previously answered questions, I attempted to open the first file, load each line into a new row of an array 'filecopy', write my the atomic positions, and write the last two rows into the 'filecopy'.
The problem arises when I insert the atomic positions. The atomic positions are stored in a nx4 array {Symbol,X,Y,Z}. When I write them to 'filecopy', each previous row in the array gets three empty cells. When I try to write 'filecopy', this screws up the file formatting by adding a bunch of extra commas at the end of each line.
So I need some help. I need to write these 47 lines:
! This is bulk hBN in A-A_prime stacking arrangement: (eclipsed with B over N)
&CONTROL
calculation = "vc-relax",
restart_mode= 'from_scratch'
!restart_mode= 'restart'
pseudo_dir = "/u/pdev/pwscf/PP_espresso/BN_PP",
outdir="/work/pdev/Graphene/hBN/unitcell/AAprime_LDA",
verbosity= 'high'
prefix='gr'
wf_collect=.true.
tstress = .true. ,
tprnfor = .true. ,
nstep = 100,
dt=20
etot_conv_thr = 1.0E-5 ,
forc_conv_thr = 1.0D-4 ,
/
&SYSTEM
ibrav = 4,
celldm(1) = 6.22853733740,
celldm(3) = 1.57964199029223,
nat = 4,
ntyp = 2,
ecutwfc = 50.0,
ecutrho = 350
occupations = "smearing",
smearing = "gaussian",
degauss = 0.001,
/
&ELECTRONS
conv_thr = 1.D-9,
mixing_beta = 0.,
/
&IONS
ion_dynamics='bfgs'
!pot_extrapolation = "second_order",
!wfc_extrapolation = "second_order",
/
&CELL
cell_dynamics='bfgs'
press=0.0
wmass=0.001
/
ATOMIC_SPECIES
Zn 65.39 Zn.pbe-van.UPF
O 15.9994 O.pbe-van_ak.UPF
ATOMIC_POSITIONS crystal
Then a list of atomic positions:
Zn 0.00000000000 0.00000000000 0.00000000000
Zn 0.33333333333 0.66666666667 0.50000000000
O 0.00000000000 0.00000000000 0.38250000000
O 0.33333333333 0.66666666667 0.88250000000
Followed by these two lines:
KPOINTS automatic
2424 18 0 0
Thanks!
3 Comments
Samuel Vergara
on 3 Jul 2016
Can you upload your code please?
Grant Cates
on 3 Jul 2016
Don't make more trouble for yourself than needed--don't need a memory copy, just write directly to the new file. That eliminates a bunch of the cell array "issues".
As far as the lines 8-13, you don't give line numbers and I'm too lazy to try to count, but that doesn't precisely align with your initial posting of simply needing to insert the array content at a point in the file...see my posting of code that does that, presuming as you said, you've got the new information in the Nx4 cell array.
If that's not the form, I'd suggest instead of asking others to try to debug your code, attach the input file and an explanation of what it actually is you've got to replace in that file if it is (as apparently from this followup?) indeed other than simply copying a first portion, inserting new section and then the closing coda.
Your attempt, while it's always good to try, looks way overly complex for the job at hand...
As for the printing of the element names/positions, here's a sample that reproduces your example for an arbitrary set of values...
>> A={'Zn' 1/3 1/3 1/3;'O' 1/3 2/3 1} % made up some numbers...
A =
'Zn' [0.3333] [0.3333] [0.3333]
'O' [0.3333] [0.6667] [ 1]
>> fmt=['%-10s' repmat('%20.11f',1,3) '\n']; % format string
>> for i=1:size(A,1)
fprintf(fmt,char(A(i,1)),A{i,2:end})
end
Zn 0.33333333333 0.33333333333 0.33333333333
O 0.33333333333 0.66666666667 1.00000000000
>>
Only difference is you'll add the fOut file handle; I just echo'ed to command window above...
Answers (1)
Keep the fixed portions A and B in separate files. Begin by copying part A to a new file, then write the data to that new file, followed by copying the last part B trailer.
fOut=fopen('NewFileName.dat','w'); % open a new file for output
fIn=fopen('PartA','r'); % open the Part A archive file
while ~feof(fIn) % copy Part A from archive to new file
l=fgets(fIn);
fprintf(fOut,'%s',l)
end
fclose(fIn) % done with Part A
for i=1:size(Array,1) % over number of elements in the array
fprintf(fOut,'%-14s %16.14f %16.14f\n',char(Array(i,1)), Array{i,2:end})
end
fIn=fopen('PartB','r'); % open the Part B archive file
while ~feof(fIn) % copy Part A from archive to new file
l=fgets(fIn);
fprintf(fOut,'%s',l)
end
fclose(fIn) % done with Part B
fclose(fOut) % done with output file, too
clear fIn fOut l % clean up handles, temp variables for neatness
ADDENDUM
For the case as you outlined...
fIn=fopen('test.in'); % Source File
fOut=fopen('NewFileName.dat','w'); % open a new file for output
% Copy the first 19 lines
for i=1:19
l=fgets(fIn); % keep \n when reading
fprintf(fOut,'%s',l)
end
% Write CellDim(1), CellDim(3), nat
fprintf(fOut,'celldm(1) = %f.11,\n',CD1)
fprintf(fOut,'celldm(3) = %f.13,\n',CD3)
fprintf(fOut,'nat = %d,\n',NAT)
% Advance three lines in source file
for i=1:3,fgetl(fIn);end
% Copy lines 23:47
for i=23:47
l=fgets(fIn);
fprintf(fOut,'%s',l)
end
% Write export positions
fmt=['%-10s' repmat('%20.11f',1,3) '\n']; % format string
for i=1:size(export_positions,1)
fprintf(fmt,char(atomic_positions{i,1}),atomic_positions{i,2:end}}
end
% Write last lines
fprintf(fOut,'KPOINTS automatic\n')
fprintf{fOut,'2424 18 0 0\n')
% Close source file
fIn=fclose(fIn);
fOut=fclose(fOut);
ADDENDUM 2
If you're insistent on building a copy in memory (useful, perhaps, if you're going to do this a number of times in short order with some modifications each time--if the same few lines are to be modified, no reason to read the fixed data over and over, granted...
file=textread('test.in','%s','delimiter','\n','whitespace','');
% modify lines 20-22
file(20)={sprintf(fOut,'celldm(1) = %f.11,',CD1)};
file(21)={sprintf(fOut,'celldm(3) = %f.13,',CD3)};
file(22)={sprintf(fOut,'nat = %d,',NAT)};
% save the last two lines for later
coda=file(end-1:end);
file(48:end)=[]; % clean out last portion; don't know how many lines were
% Write export positions
fmt=['%-10s' repmat('%20.11f',1,3) '\n']; % format string
for i=1:size(export_positions,1)
file(i+47)={sprintf(fmt,char(atomic_positions{i,1}),atomic_positions{i,2:end}}};
end
% Add back last two lines
file=[file;coda];
% At this point have a new file image in memory in cellstring array...
% Write a new file
fOut=fopen('NewFileName.dat','w'); % open a new file for output
for i=1:length(file)
fprintf(fOut,'%s\n',char(file(i)))
end
fOut=fclose(fOut);
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!