예제 #1
0
    def _getDiagonalElements(self, eigenvalues, twoform_vectors,
                             distribution_plan):

        x_coordinates = twoform_vectors.xCoordinates()
        y_coordinates = twoform_vectors.yCoordinates()

        diagonal_elements = np.zeros((len(x_coordinates), len(y_coordinates)),
                                     dtype=np.complex128)

        print("Determining diagonal elements")
        for i_mode in range(distribution_plan.totalShape()[0]):
            logAll("Processing mode: %i" % i_mode)
            new_mode = twoform_vectors.read(i_mode).coveredInterpolation(
                x_coordinates, y_coordinates)

            norm_mode = trapez2D(np.abs(new_mode)**2, 1, 1)**0.5
            if np.isnan(norm_mode):
                print("SKIP:", i_mode, norm_mode)
                sys.stdout.flush()
                continue

            diagonal_elements[:, :] += eigenvalues[i_mode] * np.abs(
                new_mode[:, :])**2

        #total_diagonal_elements = np.zeros_like(diagonal_elements)

        #mpi.COMM_WORLD.Allreduce(diagonal_elements, total_diagonal_elements, op=mpi.SUM)

        total_diagonal_elements = diagonal_elements
        return total_diagonal_elements, diagonal_elements
    def _getDiagonalElements(self, eigenvalues, twoform_vectors, distribution_plan):

        x_coordinates = twoform_vectors.xCoordinates()
        y_coordinates = twoform_vectors.yCoordinates()

        diagonal_elements = np.zeros((len(x_coordinates), len(y_coordinates)),
                                      dtype=np.complex128)

        print("Determining diagonal elements")
        for i_mode in range(distribution_plan.totalShape()[0]):
            logAll("Processing mode: %i" % i_mode)
            new_mode = twoform_vectors.read(i_mode).coveredInterpolation(x_coordinates, y_coordinates)

            norm_mode = trapez2D( np.abs(new_mode)**2, 1, 1)**0.5
            if np.isnan(norm_mode):
                print("SKIP:", i_mode,norm_mode)
                sys.stdout.flush()
                continue

            diagonal_elements[:, :] += eigenvalues[i_mode] * np.abs(new_mode[:, :])**2

        #total_diagonal_elements = np.zeros_like(diagonal_elements)

        #mpi.COMM_WORLD.Allreduce(diagonal_elements, total_diagonal_elements, op=mpi.SUM)

        total_diagonal_elements = diagonal_elements
        return total_diagonal_elements, diagonal_elements
예제 #3
0
    def propagate_af(self, autocorrelation_function,
                     directory_name="propagation_wofry",
                     af_output_file_root=None,
                     maximum_mode=None, # this is indeed the NUMBER OF MODES (maximum index plus one)
                     python_to_be_used="/users/srio/OASYS1.1/miniconda3/bin/python"):

        propagator = AutocorrelationFunctionPropagator(self)

        if maximum_mode is None:
            mode_distribution = autocorrelation_function.modeDistribution()
            maximum_mode = mode_distribution[abs(mode_distribution) > 0.00005].shape[0]

        propagator.setMaximumMode(maximum_mode)
        data_directory = "%s/%s" % (directory_name, "wavefronts")

        if isMaster():
            if not os.path.exists(directory_name):
                os.mkdir(directory_name)
            if not os.path.exists(data_directory):
                os.mkdir(data_directory)
        barrier()

        # propagated_filename = "%s/%s.npz" % (data_directory, af_name)
        propagated_filename = "%s/%s.npz" % (data_directory, "wavefront")
        af = propagator.propagate(autocorrelation_function, propagated_filename, method="WOFRY",
                                  python_to_be_used=python_to_be_used)

        barrier()
        if isMaster():
            if af_output_file_root is not None:
                af.save("%s.npz" % (af_output_file_root))
                logAll("File written to disk: %s.npz" % (af_output_file_root))

        return af
예제 #4
0
def propagateWavefrontWofry(beamline,
                            wavefront,
                            i_mode,
                            python_to_be_used="python",
                            keep_temp_files=False):

    s_id = str(mpi.COMM_WORLD.Get_rank()) + "_" + gethostname()

    wavefront.save("./tmp/tmp%s_in" % s_id)

    parameter_lines = "s_id=\"%s\"" % (s_id)
    pickle.dump(beamline, open("./tmp/tmp%s_beamline.p" % s_id, "wb"))
    lines = """
import pickle
from wofry.propagator.propagator import PropagationManager
from wofry.propagator.propagators2D.fresnel_zoom_xy import FresnelZoomXY2D
from comsyl.waveoptics.ComsylWofryBeamline import ComsylWofryBeamline

# initialize propagator
mypropagator = PropagationManager.Instance()
try:
    mypropagator.add_propagator(FresnelZoomXY2D())
except:
    print("May be you alreay initialized propagator and stored FresnelZoomXY2D")

beamline = pickle.load(open("./tmp/tmp%s_beamline.p"%s_id,"rb"))

ComsylWofryBeamline.propagate_numpy_wavefront(
    "./tmp/tmp%s_in.npz"%s_id,
    "./tmp/tmp%s_out.npz"%s_id,
    beamline,mypropagator)
"""

    file = open("./tmp/tmp%s.py" % s_id, "w")
    file.writelines(parameter_lines)
    file.writelines("\ni_mode=%d\n" % i_mode)  # added srio
    file.writelines(lines)
    file.close()

    os.system(python_to_be_used + " ./tmp/tmp%s.py" % s_id)
    if keep_temp_files:  # keep a copy of all files
        logAll("cp %s  %s" %
               ("./tmp/tmp%s_in.npz" % s_id, "./tmp/tmp%s_in_mode%d.npz" %
                (s_id, i_mode)))
        logAll("cp %s  %s" %
               ("./tmp/tmp%s_out.npz" % s_id, "./tmp/tmp%s_out_mode%d.npz" %
                (s_id, i_mode)))
        os.system("cp %s  %s" %
                  ("./tmp/tmp%s_in.npz" % s_id, "./tmp/tmp%s_in_mode%d.npz" %
                   (s_id, i_mode)))
        os.system("cp %s  %s" %
                  ("./tmp/tmp%s_out.npz" % s_id, "./tmp/tmp%s_out_mode%d.npz" %
                   (s_id, i_mode)))

    return NumpyWavefront.load("./tmp/tmp%s_out.npz" % s_id)
예제 #5
0
    def _adjustWavefrontSize(self, wavefront):
        min_size_dims = min(wavefront.dim_x(), wavefront.dim_y())

        i = 0
        max_size = 4096
        if min_size_dims > max_size:
            j = min_size_dims
            while j > max_size:
                j /= 2
                i += 1

        new_size_x = wavefront.dim_x() / 2**i
        new_size_y = wavefront.dim_y() / 2**i

        if new_size_x != wavefront.dim_x() or new_size_y != wavefront.dim_y():
            logAll("Resized to %i %i" % (new_size_x, new_size_y))
            return wavefront.asFixedSize(new_size_x, new_size_y)
        else:
            return wavefront
def propagateWavefrontWofry(beamline, wavefront, i_mode, python_to_be_used="python", keep_temp_files=False):

    s_id=str(mpi.COMM_WORLD.Get_rank())+"_"+gethostname()

    wavefront.save("./tmp/tmp%s_in"%s_id)

    parameter_lines = "s_id=\"%s\"" % (s_id)
    pickle.dump(beamline, open("./tmp/tmp%s_beamline.p"%s_id,"wb"))
    lines ="""
import pickle
from wofry.propagator.propagator import PropagationManager
from wofry.propagator.propagators2D.fresnel_zoom_xy import FresnelZoomXY2D
from comsyl.waveoptics.ComsylWofryBeamline import ComsylWofryBeamline

# initialize propagator
mypropagator = PropagationManager.Instance()
try:
    mypropagator.add_propagator(FresnelZoomXY2D())
except:
    print("May be you alreay initialized propagator and stored FresnelZoomXY2D")

beamline = pickle.load(open("./tmp/tmp%s_beamline.p"%s_id,"rb"))

ComsylWofryBeamline.propagate_numpy_wavefront(
    "./tmp/tmp%s_in.npz"%s_id,
    "./tmp/tmp%s_out.npz"%s_id,
    beamline,mypropagator)
"""

    file = open("./tmp/tmp%s.py"%s_id, "w")
    file.writelines(parameter_lines)
    file.writelines("\ni_mode=%d\n"%i_mode) # added srio
    file.writelines(lines)
    file.close()

    os.system(python_to_be_used+" ./tmp/tmp%s.py"%s_id)
    if keep_temp_files: # keep a copy of all files
        logAll("cp %s  %s" % ("./tmp/tmp%s_in.npz" % s_id,   "./tmp/tmp%s_in_mode%d.npz" % (s_id, i_mode)))
        logAll("cp %s  %s" % ("./tmp/tmp%s_out.npz" % s_id, "./tmp/tmp%s_out_mode%d.npz" % (s_id, i_mode)))
        os.system("cp %s  %s" % ("./tmp/tmp%s_in.npz" % s_id,  "./tmp/tmp%s_in_mode%d.npz" % (s_id, i_mode)))
        os.system("cp %s  %s" % ("./tmp/tmp%s_out.npz" % s_id, "./tmp/tmp%s_out_mode%d.npz" % (s_id, i_mode)))

    return NumpyWavefront.load("./tmp/tmp%s_out.npz"%s_id)
    def _adjustWavefrontSize(self, wavefront):
        min_size_dims = min(wavefront.dim_x(), wavefront.dim_y())

        i = 0
        max_size = 4096
        if min_size_dims > max_size:
            j = min_size_dims
            while j > max_size:
                j /= 2
                i += 1

        new_size_x = wavefront.dim_x() / 2**i
        new_size_y = wavefront.dim_y() / 2**i

        if new_size_x != wavefront.dim_x() or new_size_y != wavefront.dim_y():
            logAll("Resized to %i %i" %(new_size_x, new_size_y))
            return wavefront.asFixedSize(new_size_x, new_size_y)
        else:
            return wavefront
예제 #8
0
    def propagate_af(
        self,
        autocorrelation_function,
        directory_name="propagation_wofry",
        af_output_file_root=None,
        maximum_mode=None,  # this is indeed the NUMBER OF MODES (maximum index plus one)
        python_to_be_used="/users/srio/OASYS1.1/miniconda3/bin/python"):

        propagator = AutocorrelationFunctionPropagator(self)

        if maximum_mode is None:
            mode_distribution = autocorrelation_function.modeDistribution()
            maximum_mode = mode_distribution[
                abs(mode_distribution) > 0.00005].shape[0]

        propagator.setMaximumMode(maximum_mode)
        data_directory = "%s/%s" % (directory_name, "wavefronts")

        if isMaster():
            if not os.path.exists(directory_name):
                os.mkdir(directory_name)
            if not os.path.exists(data_directory):
                os.mkdir(data_directory)
        barrier()

        # propagated_filename = "%s/%s.npz" % (data_directory, af_name)
        propagated_filename = "%s/%s.npz" % (data_directory, "wavefront")
        af = propagator.propagate(autocorrelation_function,
                                  propagated_filename,
                                  method="WOFRY",
                                  python_to_be_used=python_to_be_used)

        barrier()
        if isMaster():
            if af_output_file_root is not None:
                af.save("%s.npz" % (af_output_file_root))
                logAll("File written to disk: %s.npz" % (af_output_file_root))

        return af
예제 #9
0
import mpi4py.MPI as mpi
from comsyl.utils.Logger import logAll, log
from comsyl.parallel.utils import isMaster, barrier
from socket import gethostname
from comsyl.parallel.DistributionPlan import DistributionPlan
import os

if isMaster():
    print("Hello from master")

if isMaster():
    if not os.path.exists("tmp"):
        os.mkdir("tmp")

logAll("Using LogAll")
log("Using Log")

s_id = str(mpi.COMM_WORLD.Get_rank()) + "_" + gethostname()

print("s_id: ", s_id)
print("str(mpi.COMM_WORLD.Get_rank()): ", str(mpi.COMM_WORLD.Get_rank()))

number_modes = 1000
distribution_plan = DistributionPlan(mpi.COMM_WORLD,
                                     n_rows=number_modes,
                                     n_columns=1)
print(distribution_plan)

f = open("./tmp/TMP%s_in" % s_id, 'w')
f.write(">>>>>>>>>")
f.close
예제 #10
0
    def propagate(self,
                  autocorrelation_function,
                  filename,
                  method='SRW',
                  python_to_be_used="python"):

        source_filename = autocorrelation_function._io.fromFile()

        try:
            source_uid = autocorrelation_function.info().uid()
        except:
            source_uid = "None"

        autocorrelation_function.info().logStart()

        logAll("Propagating %s (%s)" % (source_filename, source_uid))

        if self._maximum_mode is None:
            number_modes = autocorrelation_function.numberModes()
        else:
            number_modes = self._maximum_mode

        if isMaster():
            if not os.path.exists("tmp"):
                os.mkdir("tmp")

        distribution_plan = DistributionPlan(mpi.COMM_WORLD,
                                             n_rows=number_modes,
                                             n_columns=1)

        n_rank = mpi.COMM_WORLD.Get_rank()
        x_coordinates = []
        y_coordinates = []
        for i_mode in distribution_plan.localRows():

            for i in range(1):
                logAll("%i doing mode index: %i/%i (max mode index: %i)" %
                       (n_rank, i_mode, max(
                           distribution_plan.localRows()), number_modes - 1))
                if n_rank == 0:
                    sys.stdout.flush()

                wavefront = autocorrelation_function.coherentModeAsWavefront(
                    i_mode)
                #wavefront._e_field[np.abs(wavefront._e_field)<0.000001]=0.0

                if method == 'SRW':
                    # CHANGE THIS FOR WOFRY
                    srw_wavefront = propagateWavefront(
                        self.__srw_beamline,
                        wavefront,
                        autocorrelation_function.SRWWavefrontRx(),
                        autocorrelation_function.SRWWavefrontDRx(),
                        autocorrelation_function.SRWWavefrontRy(),
                        autocorrelation_function.SRWWavefrontDRy(),
                        1.0,
                        1.0,
                        i_mode,
                        python_to_be_used=python_to_be_used)
                elif method == 'WOFRY':
                    srw_wavefront = propagateWavefrontWofry(
                        self.__srw_beamline,
                        wavefront,
                        i_mode,
                        python_to_be_used=python_to_be_used)
                else:
                    raise Exception("Method not known: %s" % method)

                # norm_mode = trapez2D( np.abs(srw_wavefront.E_field_as_numpy()[0,:,:,0])**2, 1, 1)**0.5
                # if norm_mode > 1e2 or np.isnan(norm_mode):
                #     print("TRY %i AFTER PROPAGATION:" % i, i_mode,norm_mode)
                #     sys.stdout.flush()
                #else:
                #    break

            #if i==19:
            #    exit()

            # if np.any(norm_srw_wavefront > 10):
            #     exit()
            #
            # if np.any(norm_wavefront > 10):
            #     exit()

            adjusted_wavefront = self._adjustWavefrontSize(srw_wavefront)
            # norm_mode = trapez2D( np.abs(adjusted_wavefront.E_field_as_numpy()[0,:,:,0])**2, 1, 1)**0.5
            # if norm_mode > 1e2 or np.isnan(norm_mode):
            #     print("TRY %i AFTER ADJUSTMENT:" % i, i_mode,norm_mode)
            #     sys.stdout.flush()
            #     exit()

            # writes a file for every wavefront
            TwoformVectorsWavefronts.pushWavefront(filename,
                                                   adjusted_wavefront,
                                                   index=i_mode)
            #print("Saving wavefront %i" % i_mode)

            x_coordinates.append(
                adjusted_wavefront.absolute_x_coordinates().copy())
            y_coordinates.append(
                adjusted_wavefront.absolute_y_coordinates().copy())

        mpi.COMM_WORLD.barrier()

        # replace the wavefronts bu the propagated ones
        af = self._saveAutocorrelation(autocorrelation_function, number_modes,
                                       x_coordinates, y_coordinates, filename)

        # convert from one file per wavefront to one big array
        af.Twoform().convertToTwoformVectorsEigenvectors()
        af.info().setEndTime()

        filelist = glob.glob(filename + "*")
        for f in filelist:
            os.remove(f)

        return af
    def propagate(self, autocorrelation_function, filename, method='SRW', python_to_be_used="python"):

        source_filename = autocorrelation_function._io.fromFile()

        try:
            source_uid = autocorrelation_function.info().uid()
        except:
            source_uid = "None"

        autocorrelation_function.info().logStart()

        logAll("Propagating %s (%s)" % (source_filename, source_uid))


        if self._maximum_mode is None:
            number_modes = autocorrelation_function.numberModes()
        else:
            number_modes = self._maximum_mode

        if isMaster():
            if not os.path.exists("tmp"):
                os.mkdir("tmp")

        distribution_plan = DistributionPlan(mpi.COMM_WORLD, n_rows=number_modes, n_columns=1)

        n_rank = mpi.COMM_WORLD.Get_rank()
        x_coordinates = []
        y_coordinates = []
        for i_mode in distribution_plan.localRows():

            for i in range(1):
                logAll("%i doing mode index: %i/%i (max mode index: %i)" % (n_rank, i_mode, max(distribution_plan.localRows()), number_modes-1))
                if n_rank == 0:
                    sys.stdout.flush()

                wavefront = autocorrelation_function.coherentModeAsWavefront(i_mode)
                #wavefront._e_field[np.abs(wavefront._e_field)<0.000001]=0.0

                if method == 'SRW':
                    # CHANGE THIS FOR WOFRY
                    srw_wavefront = propagateWavefront(self.__srw_beamline,
                                                      wavefront,
                                                      autocorrelation_function.SRWWavefrontRx(),
                                                      autocorrelation_function.SRWWavefrontDRx(),
                                                      autocorrelation_function.SRWWavefrontRy(),
                                                      autocorrelation_function.SRWWavefrontDRy(), 1.0, 1.0, i_mode,
                                                      python_to_be_used=python_to_be_used)
                elif method == 'WOFRY':
                    srw_wavefront = propagateWavefrontWofry(self.__srw_beamline,wavefront,i_mode,python_to_be_used=python_to_be_used)
                else:
                    raise Exception("Method not known: %s"%method)

                # norm_mode = trapez2D( np.abs(srw_wavefront.E_field_as_numpy()[0,:,:,0])**2, 1, 1)**0.5
                # if norm_mode > 1e2 or np.isnan(norm_mode):
                #     print("TRY %i AFTER PROPAGATION:" % i, i_mode,norm_mode)
                #     sys.stdout.flush()
                #else:
                #    break

            #if i==19:
            #    exit()

            # if np.any(norm_srw_wavefront > 10):
            #     exit()
            #
            # if np.any(norm_wavefront > 10):
            #     exit()

            adjusted_wavefront = self._adjustWavefrontSize(srw_wavefront)
            # norm_mode = trapez2D( np.abs(adjusted_wavefront.E_field_as_numpy()[0,:,:,0])**2, 1, 1)**0.5
            # if norm_mode > 1e2 or np.isnan(norm_mode):
            #     print("TRY %i AFTER ADJUSTMENT:" % i, i_mode,norm_mode)
            #     sys.stdout.flush()
            #     exit()

            # writes a file for every wavefront
            TwoformVectorsWavefronts.pushWavefront(filename, adjusted_wavefront, index=i_mode)
            #print("Saving wavefront %i" % i_mode)

            x_coordinates.append(adjusted_wavefront.absolute_x_coordinates().copy())
            y_coordinates.append(adjusted_wavefront.absolute_y_coordinates().copy())

        mpi.COMM_WORLD.barrier()

        # replace the wavefronts bu the propagated ones
        af = self._saveAutocorrelation(autocorrelation_function, number_modes, x_coordinates, y_coordinates, filename)

        # convert from one file per wavefront to one big array
        af.Twoform().convertToTwoformVectorsEigenvectors()
        af.info().setEndTime()

        filelist = glob.glob(filename+"*")
        for f in filelist:
            os.remove(f)

        return af