Page MenuHomec4science

potential_shortening.py
No OneTemporary

File Metadata

Created
Wed, Nov 27, 05:03

potential_shortening.py

import numpy as np
from collections import defaultdict
class Consts:
def __init__(self, fname):
self.nlayer, self.nsp, self.nhl0, self.nhl1, self.lines = self.read(fname)
def read(self, fname):
with open(fname) as fh:
line = fh.__next__()
line_read = tuple((int(item) for item in line.split()))
nlayer, nsp, nhl0, nhl1 = line_read
lines = []
for i in range(nhl0):
lines.append(fh.__next__().rstrip())
return nlayer, nsp, nhl0, nhl1, lines
def reformulate(self, outfname, nhl0_div, nhl1_div):
with open(outfname, 'w') as fh:
print(
" {} {} {} {}".format(
self.nlayer, self.nsp,
int(self.nhl0/nhl0_div), int(self.nhl1/nhl1_div)),
file=fh)
for i in range(int(self.nhl0/nhl0_div)):
print(self.lines[i*nhl0_div], file=fh)
class Params:
def __init__(self, fname, nhl0, nhl1):
self.nb_comb, self.r1, self.r2, self.lines = self.read(fname)
self.nhl0 = nhl0
self.nhl1 = nhl1
def read(self, fname):
with open(fname) as fh:
line = fh.__next__()
ls = line.split()
n_comb, r1, r2 = int(ls[0]), float(ls[1]), float(ls[2])
lines = [line.rstrip() for line in fh]
return n_comb, r1, r2, lines
def reformulate(self, outfname, nhl0_div, nhl1_div):
with open(outfname, 'w') as fh:
print(
" {} {} {}".format(
int((self.nhl0/nhl0_div+1)*self.nhl1/nhl1_div),
self.r1, self.r2), file=fh)
n0 = int(self.nhl0/nhl0_div)
n1 = int(self.nhl1/nhl1_div)
for i0 in range(n0):
for i1 in range(n1):
print(self.lines[i0*nhl0_div*self.nhl1 + i1*nhl1_div],
file= fh)
for i1 in range(n1):
print(self.lines[self.nhl0*self.nhl1 + i1*nhl1_div],
file=fh)
def main():
cfname = 'in.const.NN'
pfname = 'in.params.NN'
suffix = '_short'
ocfname = cfname + suffix
opfname = pfname + suffix
div0 = 5
div1 = 10
c = Consts(cfname)
c.reformulate(ocfname, div0, div1)
Params(pfname, c.nhl0, c.nhl1).reformulate(opfname, div0, div1)
if __name__ == "__main__":
main()

Event Timeline