text:
"""
Module with function for running in parallel and doing cleanups after runs etc.

BE AWARE: This module is not completely safe for use on multiple computers
with shared file system. In particular, it's not safe to start init or
cleanup at the same time as anything else.

@author = Joel
"""
import numpy as np
import warnings
import shutil
import os
from os.path import join, isfile, isdir
from multiprocessing import Pool
from time import perf_counter, sleep
from datetime import datetime
from uuid import getnode as get_mac
from pathlib import Path
from core import data
from constants import ROOT_DIR

max_num_workers = os.cpu_count()
base_dir = join(ROOT_DIR, 'data_ignore')
mac = get_mac()

class Wrap:
    """
    Class for wrapping simulate (need pickable object for multiprocess Pool)
    """
    def __init__(self, simulate, debug=False):
        self.simulate = simulate
        self.debug = debug

    def __call__(self, x):
        # Use a broad try-except to not crash if we don't have to
        try:
            return x[0], self.simulate(*x[0], *x[1])

        except Exception as e:
            # This will be saved in the metadata file.
            if self.debug:
                raise e
            return x[0], e

class Bookkeeper:
    """
    Class for keeping track of what's been done and only assign new tasks.
    """

    def __init__(self, iterator, book, output_calc=None, bounds=None):
        """

        :param iterator: Iterable
        :param set book: Set of identifiers corresponding to previously
            completed tasks.
        :param output_calc: List of functions
        :param bounds: Bounds of elements to return from iterator
        """
        if output_calc is None:
            output_calc = {}
        if bounds is None:
            bounds = [0, np.inf]
        self.iterator = iterator
        self.book = book
        self.output_calc = output_calc
        self.bounds = bounds
        self.count = -1  # to start from 0 (see += 1 below)

    def __iter__(self):
        return self

    def __next__(self):
        while True:
            x = self.iterator.__next__()
            self.count += 1

            if self.count >= self.bounds[1]:
                raise StopIteration

            if x not in self.book and self.count >= self.bounds[0]:
                output = []
                for i in range(len(x) + 1):
                    if i in self.output_calc:
                        output.extend(self.output_calc[i](
                            *[y for j, y in enumerate(x) if j < i]))
                return x, output

def script_input(args):
    """
    Parse script inputs to parallel.run

    :param args: Input sys.argv
    :return: kwargs to parallel.run
    """
    # Handle cleanup and init
    if len(args) == 2 and args[1] in ['init', 'cleanup', 'debug']:
        print(f'\nStarting {args[1]}.\n')
        return {args[1]: True}

    # Input number of workers
    if len(args) <= 1:
        num_workers = max_num_workers
    else:
        try:
            num_workers = int(args[1])
        except ValueError:
            num_workers = max_num_workers
            warnings.warn(f'Could not parse input parameter 1 num_workers.')

    # Input start of range
    if len(args) <= 2:
        start_range = 0
    else:
        try:
            start_range = int(args[2])
        except ValueError:
            start_range = 0
            warnings.warn(f'Could not parse input parameter 2 start_range.')

    # Input end of range
    if len(args) <= 3:
        stop_range = np.inf
    else:
        try:
            stop_range = int(args[3])
        except ValueError:
            stop_range = np.inf
            warnings.warn(f'Could not parse input parameter 3 stop_range.')

    print(f'\nStarting simulation with num_workers = {num_workers}, and range ='
          f' [{start_range}, {stop_range})\n')

    return {'num_workers': num_workers,
            'start_range': start_range,
            'stop_range': stop_range}

def run(simulate,
        identifier_generator,
        input_functions,
        directory,
        version,
        script_file,
        file_from_id,
        metadata_from_id,
        num_workers=max_num_workers,
        start_range=0,
        stop_range=np.inf,
        max_task=1,
        chunksize=1,
        restart=True,
        delay=5,
        init=False,
        cleanup=False,
        debug=False):
    """
    Run simulations, metadata initialization or cleanup.
    # TODO: param doc

    :param simulate:
    :param identifier_generator:
    :param input_functions:
    :param directory:
    :param version:
    :param script_file:
    :param file_from_id:
    :param metadata_from_id:
    :param num_workers:
    :param start_range:
    :param stop_range:
    :param max_task:
    :param chunksize:
    :param restart:
    :param delay:
    :param init:
    :param cleanup:
    :param debug:
    :return:
    """
    directory = join(directory, f'v{version}')
    task = 'run'*(not init and not cleanup) \
           + 'cleanup'*(not init and cleanup) \
           + 'init'*init
    _mark_running(directory, task)
    try:
        if init:
            if _is_running(directory, ('run', 'cleanup', 'init')):
                raise RuntimeError("Can't run init when anyone else is "
                                   "running.")
            _init_metadata(identifier_generator(),
                           directory,
                           script_file)
        elif cleanup:
            if _is_running(directory, ('run', 'cleanup', 'init')):
                raise RuntimeError("Can't run cleanup when anyone else is "
                                   "running.")
            _cleanup_big(identifier_generator(), directory, script_file)
        else:
            if _is_running(directory, ('cleanup', 'init')):
                raise RuntimeError("Can't run simulation when someone else is "
                                   "initializing metadata or cleaning up.")
            while restart and _run_internal(
                    simulate,
                    identifier_generator(),
                    input_functions,
                    directory,
                    script_file,
                    file_from_id,
                    metadata_from_id,
                    num_workers,
                    start_range,
                    stop_range,
                    max_task,
                    chunksize,
                    debug
                    ):
                print(f'\nRestarting in {delay} s.\n')
                sleep(delay)
    finally:
        _mark_not_running(directory, task)

def _run_internal(simulate,
                  identifier_generator,
                  input_functions,
                  directory,
                  script_file,
                  file_from_id,
                  metadata_from_id,
                  num_workers,
                  start_range,
                  stop_range,
                  max_task,
                  chunksize,
                  debug):
    """
    Internal parallel run.

    :return: number of remaining tasks.
    """
    # Files and paths
    directory_nomac = directory
    directory = join(directory, str(mac))
    path_metadata = join(directory, 'metadata')
    try:
        # Try to load the file (will raise FileNotFoundError if not existing)
        metadata, metametadata = data.load(path_metadata, base_dir=base_dir)
    except FileNotFoundError:
        metadata = _get_metadata(directory_nomac)[0]
        metametadata = {
            'description': "File that keeps track of what's been done "
                           "previously in this script "
            f"({script_file}).",
            'run_time': [],
            'num_workers': [],
            'num_tasks_completed': [],
            'success_rate': [],
            'created_from': script_file}
        data.save(file=path_metadata, data=metadata, metadata=metametadata,
                  extract=True, base_dir=base_dir)

    # Extract identifiers to previously completed simulations
    ids = set()
    for id_, value in metadata:
        if value is True:
            if id_ in ids:
                raise KeyError('Multiple entries for same id in metadata.')
            ids.add(id_)

    # Cleanup (metadata can contain thousands of Exception objects)
    del metadata, metametadata

    # Wrap simulate to get expected input/output and handle exceptions
    wrap = Wrap(simulate, debug=debug)

    # Generator for pool
    generator = Bookkeeper(identifier_generator, ids, input_functions,
                           [start_range, stop_range])

    # Counters and such
    files = set()
    success = 0
    fail = 0
    start_time = perf_counter()

    # Actual run
    try:
        with Pool(num_workers, maxtasksperchild=max_task) as p:
            result_generator = p.imap_unordered(wrap, generator,
                                                chunksize=chunksize)
            for identifier, result in result_generator:
                # Handle exceptions:
                if isinstance(result, Exception):
                    fail += 1
                    # Save the error
                    data.append(path_metadata, [identifier, result],
                                base_dir=base_dir)
                else:
                    success += 1
                    file = file_from_id(identifier)
                    if file not in files:
                        files.add(file)
                        if not isfile(join(base_dir, directory,
                                           file + '.pkl')):
                            # Create file
                            metadata = metadata_from_id(identifier)
                            data.save(join(directory, file), [], metadata,
                                      extract=True, base_dir=base_dir)

                    data.append(join(directory, file), [identifier, result],
                                base_dir=base_dir)

                    # Mark the task as completed (last in the else,
                    # after saving result)
                    data.append(path_metadata, [identifier, True],
                                base_dir=base_dir)
    except Exception as e:
        if debug:
            raise e
    finally:
        stop_time = perf_counter()
        total = success + fail
        if total == 0:
            total = 1  # To avoid division by zero

        # Post simulation.
        metadata, metametadata = data.load(path_metadata, base_dir=base_dir)
        metadata = _cleanup_small(metadata)
        metametadata['run_time'].append(stop_time - start_time)
        metametadata['num_workers'].append((num_workers, max_num_workers))
        metametadata['num_tasks_completed'].append(success)
        metametadata['success_rate'].append(success / total)
        data.save(file=path_metadata, data=metadata, metadata=metametadata,
                  extract=True, base_dir=base_dir)

        # Print some stats
        print('\nSimulation completed.')
        print(f'Total number of tasks this far: {len(metadata)}')
        print(f'Completed tasks this run: {success}')
        print(f'Success rate this run: {success / total}')
        remaining = len(metadata) - sum(x[1] for x in metadata if x[1] is True)
        print(f'Minimum number of tasks remaining for this run: {fail}')
        print(f'Total number of tasks remaining: {remaining}')

        # No success and no fail => no restart
        if not debug:
            return success + fail

def _init_metadata(identifier_generator, directory, script_file, force=False):
    """
    Initialize metadata.

    :param identifier_generator: id generator
    :param directory: directory
    :param script_file: name of script
    :param force: if False raise RuntimeError if metadata is already existing.
    :return:
    """
    if not force and _get_metadata(directory, warn=False) != ([], {}):
        raise RuntimeError('Metadata has already been initialized.')
    start_time = perf_counter()

    # Try to load from data/directory
    metadata, metametadata = _get_metadata(directory, False, data.BASE_DIR)
    # Fix directory and path
    path_metadata = join(directory, 'total', 'metadata')

    if (metadata, metametadata) == ([], {}):
        # Initialize from scratch

        # Save metadata file
        metadata = []
        metametadata = {
            'description': "File that keeps track of what's been done "
                           "previously in this script "
                           f"({script_file}).",
            'created_from': script_file}
        data.save(file=path_metadata, data=metadata, metadata=metametadata,
                  extract=True, base_dir=base_dir)

        # Add identifiers
        count = 0

        for identifier in identifier_generator:
            count += 1
            if count % 1e4 == 0:
                print(f'{count} identifiers saved.')
            data.append(path_metadata, [identifier, False], base_dir=base_dir)
    else:
        print(f'Initializing metadata from {join(data.BASE_DIR, directory)}.')
        count = len(metadata)
        data.save(file=path_metadata, data=metadata, metadata=metametadata,
                  extract=True, base_dir=base_dir)

    stop_time = perf_counter()

    print(f'\nMetadata initialization completed in'
          f'{stop_time - start_time: .1f} s with {count} identifiers.\n')

def _cleanup_big(identifier_generator, directory, script_file):
    """
    Cleanup directory by going trough all subdirectories, collect results and
    fix metadata.

    :param directory: Directory of cleanup.
    :return:
    """
    # TODO: collect metadata regarding time, success rate, etc. from
    #  mac-subdirs to total/metadata, save as dict with mac as key(?)

    start_time = perf_counter()
    # Get total/metadata
    metadata, metametadata = _get_metadata(directory, warn=False)
    if (metadata, metametadata) == ([], {}):
        _init_metadata(identifier_generator, directory, script_file)

        # Try again
        metadata, metametadata = _get_metadata(directory, warn=False)
        if (metadata, metametadata) == ([], {}):
            raise ValueError("_init_metadata doesn't work")

    # Convert metadata to dict
    meta_dict = {}
    for x in metadata:
        # Keep only the last exception for given identifier.
        if x[0] not in meta_dict or meta_dict[x[0]] is not True:
            meta_dict[x[0]] = x[1]
    del metadata

    # Find mac-subdirectories
    subdirs = set()
    with os.scandir(join(base_dir, directory)) as it:
        for entry in it:
            if entry.is_dir() and entry.name.isdigit():
                subdirs.add(join(base_dir, directory, entry.name))
    # Add data/directory to subdirs to not overwrite data from other
    # file-systems
    data_dir = join(data.BASE_DIR, directory)
    if isdir(data_dir):
        subdirs.add(data_dir)

    # Find data-files and keep track of which exists in which subdir
    files = {}
    for subdir in subdirs:
        with os.scandir(subdir) as it:
            for entry in it:
                if entry.is_file() and entry.name != 'metadata.pkl':
                    if entry.name not in files:
                        files[entry.name] = []
                    files[entry.name].append(subdir)

    # Go through files, create file in total (if not existing), add data
    # from files in subdirs and update meta_dict
    count = 0
    metadata = None  # To not crash at del metadata if no file.
    for file in files:
        print(f"\nSaving results in {join('total', file)}."
              "\nUsing data from the following directories:")
        # Load file from total
        try:
            content, metadata = data.load(file=join(directory, 'total', file),
                                          base_dir=base_dir)
        except FileNotFoundError:
            content, metadata = [], {}

        # Convert content to dict
        content_dict = _add_result_to_dict(content, {})
        del content

        # Add content from other files
        for subdir in files[file]:
            print(subdir)

            content_new, metadata_new = data.load(
                file=join(subdir, file), base_dir='')

            content_dict = _add_result_to_dict(content_new, content_dict)

            # Change metadata if no previous
            if metadata == {}:
                metadata = metadata_new
                metadata['created_by'] = data.get_name()
                metadata['created_datetime'] = datetime.now().\
                    strftime("%Y-%m-%d, %H:%M:%S")

        # Convert content_dict back to list and update meta_dict
        content = []
        for id_ in content_dict:
            if id_ not in meta_dict or meta_dict[id_] is not True:
                meta_dict[id_] = True

            for result in content_dict[id_]:
                count += 1
                content.append([id_, result])

        # Save file
        data.save(file=join(directory, 'total', file), data=content,
                  metadata=metadata, extract=True, base_dir=base_dir,
                  disp=False)
    del metadata

    print('\nFinishing cleanup.')

    # Convert meta_dict back to list and save
    metadata = []
    for id_ in meta_dict:
        metadata.append([id_, meta_dict[id_]])

    data.save(file=join(directory, 'total', 'metadata'), base_dir=base_dir,
              data=metadata, metadata=metametadata, extract=True, disp=False)

    # Update metadata in subdirs
    for subdir in subdirs:
        try:
            metametadata_new = data.load(
                file=join(subdir, 'metadata'), base_dir='')[1]
        except FileNotFoundError:
            metametadata_new = metametadata

        data.save(file=join(subdir, 'metadata'), base_dir='',
                  data=metadata, metadata=metametadata_new, extract=True,
                  disp=False)

    # Copy content of base_dir/directory/total to data.BASE_DIR/directory
    destination = join(data.BASE_DIR, directory)
    os.makedirs(destination, exist_ok=True)
    with os.scandir(join(base_dir, directory, 'total')) as it:
        for entry in it:
            if entry.is_file():
                shutil.copy(entry.path, destination)

    remaining = sum(x is not True for x in meta_dict.values())

    # Print some stats
    stop_time = perf_counter()
    print(f'\nCleanup completed in {stop_time - start_time:.1f} s.')
    print(f'A total of {len(metadata)} identifiers where handled, {count} '
          f'results saved and {remaining} tasks remaining.\n')

def _add_result_to_dict(content, content_dict):
    """
    Adds content to dict.

    :param content: Content to add.
    :param content_dict: Dict to add to.
    :return: Updated dict.
    """
    for id_, result in content:
        if id_ not in content_dict:
            content_dict[id_] = []
        if not _result_in_results(result, content_dict[id_]):
            content_dict[id_].append(result)
    return content_dict

def _result_in_results(result, results):
    """
    Checks if result is in results (OptResults objects can't be compared).

    :param result: result object to check for.
    :param list results: list of results to check in.
    :return:
    """
    return any(_cmp(result, x) for x in results)

def _cmp(x, y):
    """
    Used to compare objects containing (among other things) numpy arrays.

    TODO: better solution?

    :param x:
    :param y:
    :return:
    :rtype: bool
    """
    try:
        np.testing.assert_equal(x, y, verbose=False)
        return True
    except AssertionError:
        return False

def _cleanup_small(metadata):
    """
    Cleanup metadata.

    :param metadata: unclean metadata
    :return: cleaned metadata
    """
    meta_dict = {}
    for x in metadata:
        # Keep only the last exception for given identifier.
        if x[0] not in meta_dict or meta_dict[x[0]] is not True:
            meta_dict[x[0]] = x[1]
    return [[x, meta_dict[x]] for x in meta_dict]

def _get_metadata(directory, warn=True, base=base_dir):
    """
    Look for metadata in directory. Returns [], {} if not found.

    :param directory: Directory to search through.
    :return: Metadata and meta-metadata.
    """
    try:
        return data.load(join(directory, 'total', 'metadata'),
                         base_dir=base)
    except FileNotFoundError:
        if warn:
            warnings.warn('Metadata has not been initialized correctly, '
                          'run parallel.run with init=True and then with '
                          'cleanup=True.')
        return [], {}

def _mark_running(directory, task):
    """
    Mark as running.

    :param directory: Directory to save file in.
    :param task: Task (run, init, or cleanup)
    :return:
    """
    dir_complete = join(base_dir, directory)
    os.makedirs(dir_complete, exist_ok=True)
    Path(join(dir_complete, f'{task}_{mac}')).touch()

def _mark_not_running(directory, task):
    """
    Mark as not running (by removing the file created by _mark_running).

    :param directory: Directory to remove file from.
    :param task: Task (run, init, or cleanup)
    :return:
    """
    try:
        Path(join(base_dir, directory, f'{task}_{mac}')).unlink()
    except FileNotFoundError:
        pass

def _is_running(directory, tasks):
    """
    Check if anyone is running.

    :param directory: Directory to check in.
    :param tasks: tuple with tasks to check for.
    :return:
    """
    with os.scandir(join(base_dir, directory)) as it:
        for entry in it:
            if entry.is_file() and entry.name.startswith(tasks) \
                    and not entry.name.endswith(str(mac)):
                return True
    return False


text:
'''
Twitter sentiment analysis/sarcasm trend detection

@author: Ryan Schubert
'''

#!/usr/bin/env python

import sys
import string
import simplejson
from twython import Twython
import nltk
from nltk import sent_tokenize, word_tokenize, pos_tag
from sentiment_module import sentiment
import scipy
import numpy
import matplotlib.pyplot as plt
import pandas
import sklearn
import unicodedata
import itertools
from numpy.dual import svd
import time
from posix import fpathconf

reload(sys)
sys.setdefaultencoding('utf8')

def remove_all(element, list):
    return filter(lambda x: x != element, list)

#OAUTH

t = Twython(app_key='UIdeItVBO6TK1HUDFRwmySCNw',
    app_secret='X1SKdQnIqoMO9ZOD4ib3ZdlC2RFCy8tl5QsPHQrrPpjoqzamOy',
    oauth_token='104657169-euvKE5fpqLr6p3zk72Of5XylMMtw9NvWxtBiCGk4',
    oauth_token_secret='y1pzipRKGtwqoEm3MOugDBukIertbV1myCwx5Fy63hsPJ')

#setup query

query = 'sarcasticTweet'
hashtag = '#' + query

#search
search = t.search(q=query, count=100, lang='en', result_type='recent')

#init list that will store tweets
tweets = []

#parse query results for just the tweet body and store in list.
'''for tweet in search['statuses']:
    if tweet['text'].partition(' ')[0] != 'RT' and tweet['text'] != query and tweet['text'] != hashtag and tweet['retweeted'] == False:
        tweets.append(tweet['text'])

time.sleep(10)mystr[-4:]mystr[-4:]mystr[-4:]mystr[-4:]

search = t.search(q=query, count=100, lang='en', result_type='recent')
for tweet in search['statuses']:
    if tweet['text'].partition(' ')[0] != 'RT' and tweet['text'] != query and tweet['text'] != hashtag and tweet['retweeted'] == False:
        tweets.append(tweet['text'])
'''

with open('tweetDataSet.txt') as f:
    tweets = f.readlines()

#tweets.append("Nothing better than taking a super difficult exam that you're not prepared for!")
sar_tweets = []

for tweet in tweets:
    if "*y*" in tweet:
        sar_tweets.append(tweet)
        tweets.remove(tweet)

#init tokens list
org_tokens = []

#tokenize tweets
for tweet in tweets:
    org_tokens.append(word_tokenize(tweet))

for t in org_tokens:
    print t

for tweet in org_tokens:
    for i,t in enumerate(tweet):
        if t.isalnum() != True:
            tweet.remove(t)

'''print "\nafter rem---------------\n"

for t in org_tokens:
    print t

print '\n\n-------------------------------------------------------------\n\n'

'''

#init sentiments list
sentiments = []

#get avg sentiments for the tweets
for t in org_tokens:
    sentiments.append(sentiment.sentiment(t).items())

sent_dict = {}
for i in range(len(tweets)):
    sent_dict[tweets[i]] = sentiments[i]

for k,v in sent_dict.items():
    if v[0][1] < 1 or v[1][1] < 1:
        del sent_dict[k]

#print sent_dict.items()[0][0]
#print sent_dict.items()[0][1]

values = []
lengths = []
arousal = []
valence = []
fpv = []
fpvl = []
fpa = []
fpva = []

for i,(k,v) in enumerate(sent_dict.items()):
    sv = v[1][1] - v[0][1]
    val = v[1][1]
    aro = v[0][1]
    sl = len(sent_dict.items()[i][0].split())
    if sv >= 1.6 and sl >= 5.8:
        fpv.append(sv)
        fpvl.append(sl)
        fpa.append(aro)
        fpva.append(val)
    else:
        values.append(sv)
        lengths.append(sl)
        arousal.append(aro)
        valence.append(val)

    if sv < .5:
        print "NOT SARCASTIC" + " (" + str(sl) +"): " + sent_dict.items()[i][0] + "\n"
    if sv > .5 and sv < 1:
        print "PROBABLY NOT SARCASTIC" + " (" + str(sl) +"): " + sent_dict.items()[i][0] + "\n"
    if sv > 1 and sv < 2:
        print "MAYBE SARCASTIC" + " (" + str(sl) +"): " + sent_dict.items()[i][0] + "\n"
    if sv > 2:
        print "PROBABLY SARCASTIC" + " (" + str(sl) +"): " + sent_dict.items()[i][0] + "\n"

#init tokens list
org_tokens = []

#tokenize tweets
for tweet in sar_tweets:
    org_tokens.append(word_tokenize(tweet))

#for t in org_tokens:
    #print t

for tweet in org_tokens:
    for i,t in enumerate(tweet):
        if t.isalnum() != True:
            tweet.remove(t)

#init sentiments list
sentiments = []

#get avg sentiments for the tweets
for t in org_tokens:
    sentiments.append(sentiment.sentiment(t).items())

sent_dict = {}
for i in range(len(sar_tweets)):
    sent_dict[sar_tweets[i]] = sentiments[i]

for k,v in sent_dict.items():
    if v[0][1] < 1 or v[1][1] < 1:
        del sent_dict[k]

#print sent_dict.items()[0][0]
#print sent_dict.items()[0][1]

sar_values = []
sar_lengths = []
sar_arousal = []
sar_valence = []

for i,(k,v) in enumerate(sent_dict.items()):
    sv = v[1][1] - v[0][1]
    val = v[1][1]
    aro = v[0][1]
    sl = len(sent_dict.items()[i][0].split())
    sar_values.append(sv)
    sar_lengths.append(sl)
    sar_arousal.append(aro)
    sar_valence.append(val)

plt.plot(arousal, valence, 'ro')
#plt.plot(fpa, fpva, 'go')
plt.plot(sar_arousal, sar_valence, 'bo')
plt.axis([2, 8, 2, 8])
plt.xlabel('arousal')
plt.ylabel('valence')
plt.show()

x = 0

for val in sar_values:
    if val >= 1.5:
        x = x + 1

y = 0

for val in values:
    if val >= 1.5:
        y = y + 1

print "sarcastic:"
print x
print len(sar_values)
print"\n"
print y
print len(values)

#for key in search['statuses'][0].keys():
 # print(key)

ext: py

max_stars_repo_name: schubertr/Twitter-Sarcasm-Detection

max_issues_repo_name: schubertr/Twitter-Sarcasm-Detection

max_forks_repo_name: schubertr/Twitter-Sarcasm-Detection

reason: import numpy,from numpy,import scipy


text:
"""
Functions for quantum parameters, including electron degenerate
gases and warm dense matter.

"""
__all__ = [
    "chemical_potential",
    "deBroglie_wavelength",
    "Ef_",
    "Fermi_energy",
    "lambdaDB_",
    "lambdaDB_th_",
    "Thomas_Fermi_length",
    "thermal_deBroglie_wavelength",
    "Wigner_Seitz_radius",
]

import astropy.units as u
import numpy as np

from astropy.constants.si import c, e, eps0, h, hbar, k_B, m_e

from plasmapy import particles
from plasmapy.formulary import mathematics
from plasmapy.formulary.relativity import Lorentz_factor
from plasmapy.utils import RelativityError
from plasmapy.utils.decorators import validate_quantities

# TODO: Use @check_relativistic and @particle_input
@validate_quantities(
    V={"can_be_negative": True}, validations_on_return={"can_be_negative": False}
)
def deBroglie_wavelength(V: u.m / u.s, particle) -> u.m:
    r"""
    Return the de Broglie wavelength.

    The de Broglie wavelength (:math:`λ_{dB}`) of a particle is defined by

    .. math::

        λ_{dB} = \frac{h}{p} = \frac{h}{γ m V}

    where :math:`h` is the Planck constant, :math:`p` is the
    relativistic momentum of the particle, :math:`γ` is the
    Lorentz factor, :math:`m` is the mass of the particle, and
    :math:`V` is the velocity of the particle.

    **Aliases:** `lambdaDB_`

    Parameters
    ----------
    V : `~astropy.units.Quantity`
        Particle velocity in units convertible to meters per second.

    particle : `str`, `~plasmapy.particles.Particle`, or `~astropy.units.Quantity`
        An instance of `~plasmapy.particles.particle_class.Particle`, or
        an equvalent representation (e.g., ``'e'``, ``'p'``, ``'D+'``, or
        ``'He-4 1+'``), for the particle of interest, or the particle
        mass in units convertible to kg.  If a
        `~plasmapy.particles.particle_class.Particle` instance is given, then the
        particle mass is retrieved from the object.

    Returns
    -------
    lambda_dB : `~astropy.units.Quantity`
        The de Broglie wavelength in units of meters.

    Raises
    ------
    `TypeError`
        If the velocity is not a `~astropy.units.Quantity` and cannot be
        converted into a `~astropy.units.Quantity`.

    `~astropy.units.UnitConversionError`
        If the velocity is not in appropriate units.

    `~plasmapy.utils.exceptions.RelativityError`
        If the magnitude of ``V`` is larger than the speed of light.

    Warns
    -----
    : `~astropy.units.UnitsWarning`
        If units are not provided, SI units are assumed.

    Examples
    --------
    >>> from astropy import units as u
    >>> velocity = 1.4e7 * u.m / u.s
    >>> deBroglie_wavelength(velocity, 'e')
    <Quantity 5.18997095e-11 m>
    >>> deBroglie_wavelength(V = 0 * u.m / u.s, particle = 'D+')
    <Quantity inf m>
    """

    V = np.abs(V)

    if np.any(V >= c):
        raise RelativityError(
            "Velocity input in deBroglie_wavelength cannot "
            "be greater than or equal to the speed of "
            "light."
        )

    if not isinstance(particle, u.Quantity):
        try:
            # TODO: Replace with more general routine!
            m = particles.particle_mass(particle)
        except Exception:
            raise ValueError("Unable to find particle mass.")
    else:
        try:
            m = particle.to(u.kg)
        except Exception:
            raise u.UnitConversionError(
                "The second argument for deBroglie_wavelength must be either a "
                "representation of a particle or a"
                " Quantity with units of mass."
            )

    if V.size > 1:

        lambda_dBr = np.ones(V.shape) * np.inf * u.m
        indices = V.value != 0
        lambda_dBr[indices] = h / (m * V[indices] * Lorentz_factor(V[indices]))

    else:

        if V == 0 * u.m / u.s:
            lambda_dBr = np.inf * u.m
        else:
            lambda_dBr = h / (Lorentz_factor(V) * m * V)

    return lambda_dBr

lambdaDB_ = deBroglie_wavelength
""" Alias to :func:`deBroglie_wavelength`. """

@validate_quantities(
    T_e={"can_be_negative": False, "equivalencies": u.temperature_energy()},
    validations_on_return={"can_be_negative": False},
)
def thermal_deBroglie_wavelength(T_e: u.K) -> u.m:
    r"""
    Calculate the thermal de Broglie wavelength for electrons.

    **Aliases:** `lambdaDB_th_`

    Parameters
    ----------
    T_e : `~astropy.units.Quantity`
        Electron temperature.

    Returns
    -------
    lambda_dbTh : `~astropy.units.Quantity`
        The thermal de Broglie wavelength for electrons in meters.

    Raises
    ------
    `TypeError`
        If argument is not a `~astropy.units.Quantity`.

    `~astropy.units.UnitConversionError`
        If argument is in incorrect units.

    `ValueError`
        If argument contains invalid values.

    Warns
    -----
    : `~astropy.units.UnitsWarning`
        If units are not provided, SI units are assumed.

    Notes
    -----
    The thermal de Broglie wavelength is approximately the average de Broglie
    wavelength for electrons in an ideal gas and is given by

    .. math::

       λ_{dbTh} = \frac{h}{\sqrt{2 π m_e k_B T_e}}

    Example
    -------
    >>> from astropy import units as u
    >>> thermal_deBroglie_wavelength(1 * u.eV)
    <Quantity 6.9193675e-10 m>
    """
    lambda_dbTh = h / np.sqrt(2 * np.pi * m_e * k_B * T_e)
    return lambda_dbTh

lambdaDB_th_ = thermal_deBroglie_wavelength
""" Alias to :func:`thermal_deBroglie_wavelength`. """

@validate_quantities(
    n_e={"can_be_negative": False}, validations_on_return={"can_be_negative": False}
)
def Fermi_energy(n_e: u.m ** -3) -> u.J:
    r"""
    Calculate the kinetic energy in a degenerate electron gas.

    **Aliases:** `Ef_`

    Parameters
    ----------
    n_e : ~astropy.units.Quantity
        Electron number density.

    Returns
    -------
    energy_F : `~astropy.units.Quantity`
        The Fermi energy in joules.

    Raises
    ------
    `TypeError`
        If argument is not a `~astropy.units.Quantity`.

    `~astropy.units.UnitConversionError`
        If argument is in incorrect units.

    `ValueError`
        If argument contains invalid values.

    Warns
    -----
    : `~astropy.units.UnitsWarning`
        If units are not provided, SI units are assumed.

    Notes
    -----
    The Fermi energy is the kinetic energy in a degenerate electron gas
    and is given by

    .. math::

       E_F = \frac{π^2 ℏ^2}{2 m_e}
       \left( \frac{3 n_e}{π} \right)^{2/3}

    This quantity is often used in place of thermal energy for analysis
    of cold, dense plasmas (e.g. warm dense matter, condensed matter).

    See Also
    --------
    Thomas_Fermi_length

    Example
    -------
    >>> from astropy import units as u
    >>> Fermi_energy(1e23 * u.cm**-3)
    <Quantity 1.2586761e-18 J>
    """
    coeff = (np.pi * hbar) ** 2 / (2 * m_e)
    energy_F = coeff * (3 * n_e / np.pi) ** (2 / 3)
    return energy_F

Ef_ = Fermi_energy
""" Alias to :func:`Fermi_energy`. """

@validate_quantities(
    n_e={"can_be_negative": False}, validations_on_return={"can_be_negative": False}
)
def Thomas_Fermi_length(n_e: u.m ** -3) -> u.m:
    r"""
    Calculate the exponential scale length for charge screening
    for cold and dense plasmas.

    Parameters
    ----------
    n_e : `~astropy.units.Quantity`
        Electron number density.

    Returns
    -------
    lambda_TF : `~astropy.units.Quantity`
        The Thomas-Fermi screening length in meters.

    Raises
    ------
    `TypeError`
        If argument is not a `~astropy.units.Quantity`.

    `~astropy.units.UnitConversionError`
        If argument is in incorrect units.

    `ValueError`
        If argument contains invalid values.

    Warns
    -----
    : `~astropy.units.UnitsWarning`
        If units are not provided, SI units are assumed.

    Notes
    -----
    The Thomas-Fermi screening length is the exponential scale length for
    charge screening and is given by

    .. math::

       λ_{TF} = \sqrt{\frac{2 ε_0 E_F}{3 n_e e^2}}

    for an electron degenerate gas.

    This quantity is often used in place of the Debye length for analysis
    of cold, dense plasmas (e.g. warm dense matter, condensed matter).

    The electrical potential will drop by a factor of :math:`1/e` every
    Thomas-Fermi screening length.

    Plasmas will generally be quasineutral on length scales significantly
    larger than the Thomas-Fermi screening length.

    See Also
    --------
    Fermi_energy
    plasmapy.formulary.Debye_length

    Example
    -------
    >>> from astropy import units as u
    >>> Thomas_Fermi_length(1e23 * u.cm**-3)
    <Quantity 5.37991409e-11 m>

    """
    energy_F = Fermi_energy(n_e)
    lambda_TF = np.sqrt(2 * eps0 * energy_F / (3 * n_e * e ** 2))
    return lambda_TF

@validate_quantities(
    n={"can_be_negative": False}, validations_on_return={"can_be_negative": False}
)
def Wigner_Seitz_radius(n: u.m ** -3) -> u.m:
    r"""
    Calculate the Wigner-Seitz radius, which approximates the inter-particle
    spacing.

    This function returns the radius of a sphere whose volume is
    equal to the mean volume per atom in a solid. This parameter is
    often used to calculate the coupling parameter.
    When ion density is used, this is the ion sphere radius, i.e., the
    space occupied by a single ion with no other ions in that space. Higher
    density means less space for each ion, so the radius is smaller.

    Parameters
    ----------
    n : `~astropy.units.Quantity`
        Particle number density.

    Returns
    -------
    radius : `~astropy.units.Quantity`
        The Wigner-Seitz radius in meters.

    Raises
    ------
    `TypeError`
        If argument is not a ~astropy.units.Quantity.

    `~astropy.units.UnitConversionError`
        If argument is in incorrect units.

    `ValueError`
        If argument contains invalid values.

    Warns
    -----
    : `~astropy.units.UnitsWarning`
        If units are not provided, SI units are assumed.

    Notes
    -----
    The Wigner-Seitz radius approximates the interparticle spacing.
    It is the radius of a sphere whose volume is equal to the mean
    volume per atom in a solid:

    .. math::
        r = \left(\frac{3}{4 π n}\right)^{1/3}

    See Also
    --------
    Fermi_energy

    Example
    -------
    >>> from astropy import units as u
    >>> Wigner_Seitz_radius(1e29 * u.m**-3)
    <Quantity 1.33650462e-10 m>

    """
    radius = (3 / (4 * np.pi * n)) ** (1 / 3)
    return radius

# TODO: remove NotImplementedError and 'doctest: +SKIP' when the following issues are addressed...
#       https://github.com/PlasmaPy/PlasmaPy/issues/726
#       https://github.com/astropy/astropy/issues/9721
@validate_quantities(
    n_e={"can_be_negative": False},
    T={"can_be_negative": False, "equivalencies": u.temperature_energy()},
)
def chemical_potential(n_e: u.m ** -3, T: u.K) -> u.dimensionless_unscaled:
    r"""
    Calculate the ideal chemical potential.

    Parameters
    ----------
    n_e : `~astropy.units.Quantity`
        Electron number density.

    T : ~astropy.units.Quantity
        The temperature.

    Returns
    -------
    beta_mu : `~astropy.units.Quantity`
        The dimensionless ideal chemical potential. That is the ratio of
        the ideal chemical potential to the thermal energy.

    Raises
    ------
    `TypeError`
        If argument is not a `~astropy.units.Quantity`.

    `~astropy.units.UnitConversionError`
        If argument is in incorrect units.

    `ValueError`
        If argument contains invalid values.

    Warns
    -----
    : `~astropy.units.UnitsWarning`
        If units are not provided, SI units are assumed.

    Notes
    -----
    The ideal chemical potential is given by [1]_:

    .. math::
        χ_a = I_{1/2}(β μ_a^{ideal})

    where :math:`χ` is the degeneracy parameter, :math:`I_{1/2}` is the
    Fermi integral with order 1/2, :math:`β` is the inverse thermal
    energy :math:`β = 1/(k_B T)`, and :math:`μ_a^{ideal}`
    is the ideal chemical potential.

    The definition for the ideal chemical potential is implicit, so it must
    be obtained numerically by solving for the Fermi integral for values
    of chemical potential approaching the degeneracy parameter. Since values
    returned from the Fermi_integral are complex, a nonlinear
    Levenberg-Marquardt least squares method is used to iteratively approach
    a value of :math:`μ` which minimizes
    :math:`I_{1/2}(β μ_a^{ideal}) - χ_a`

    This function returns :math:`β μ^{ideal}` the dimensionless
    ideal chemical potential.

    Warning: at present this function is limited to relatively small
    arguments due to limitations in the `~mpmath` package's implementation
    of `~mpmath.polylog`, which PlasmaPy uses in calculating the Fermi
    integral.

    References
    ----------
    .. [1] Bonitz, Michael. Quantum kinetic theory. Stuttgart: Teubner, 1998.

    Example
    -------
    >>> from astropy import units as u
    >>> chemical_potential(n_e=1e21*u.cm**-3,T=11000*u.K)  # doctest: +SKIP
    <Quantity 2.00039985e-12>

    """

    raise NotImplementedError(
        "This function has been temporarily disabled due to a bug.\n"
        "Please refer to https://github.com/PlasmaPy/PlasmaPy/issues/726 \n"
        "and https://github.com/astropy/astropy/issues/9721 "
        "for progress in fixing it."
    )
    # deBroglie wavelength
    lambdaDB = thermal_deBroglie_wavelength(T)
    # degeneracy parameter
    degen = (n_e * lambdaDB ** 3).to(u.dimensionless_unscaled)

    def residual(params, data, eps_data):
        """Residual function for fitting parameters to Fermi_integral."""
        alpha = params["alpha"].value
        # note that alpha = mu / (k_B * T)
        model = mathematics.Fermi_integral(alpha, 0.5)
        complexResidue = (data - model) / eps_data
        return complexResidue.view(np.float)

    # setting parameters for fitting along with bounds
    alphaGuess = 1 * u.dimensionless_unscaled
    try:
        from lmfit import minimize, Parameters
    except (ImportError, ModuleNotFoundError) as e:
        from plasmapy.optional_deps import lmfit_import_error

        raise lmfit_import_error from e

    params = Parameters()
    params.add("alpha", value=alphaGuess, min=0.0)
    # calling minimize function from lmfit to fit by minimizing the residual
    data = np.array([degen])  # result of Fermi_integral - degen should be zero
    eps_data = np.array([1e-15])  # numerical error
    minFit = minimize(residual, params, args=(data, eps_data))
    beta_mu = minFit.params["alpha"].value * u.dimensionless_unscaled
    return beta_mu

# TODO: decorate with validate_quantities
# TODO: remove NotImplementedError and 'doctest: +SKIP' when the following issues are addressed...
#       https://github.com/PlasmaPy/PlasmaPy/issues/726
#       https://github.com/astropy/astropy/issues/9721
def _chemical_potential_interp(n_e, T):
    r"""
    Fitting formula for interpolating chemical potential between classical
    and quantum regimes.

    See [1]_, [2]_ for more information.

    Parameters
    ----------
    n_e : `~astropy.units.Quantity`
        Electron number density.

    T : `~astropy.units.Quantity`
        Temperature in units of temperature or energy.

    Returns
    -------
    beta_mu : `~astropy.units.Quantity`
        The dimensionless chemical potential, which is a ratio of
        chemical potential energy to thermal kinetic energy.

    Raises
    ------
    `TypeError`
        If argument is not a `~astropy.units.Quantity`.

    `~astropy.units.UnitConversionError`
        If argument is in incorrect units.

    `ValueError`
        If argument contains invalid values.

    Warns
    -----
    : `~astropy.units.UnitsWarning`
        If units are not provided, SI units are assumed.

    Notes
    -----
    The ideal chemical potential is given by [1]_:

    .. math::
        \frac{μ}{k_B T_e} = - \frac{3}{2} \ln Θ + \ln
        \frac{4}{3 \sqrt{π}} +
        \frac{A Θ^{-b - 1} + B Θ^{-(b + 1) / 2}}{1 + A Θ^{-b}}

    where

    .. math::
        Θ = \frac{k_B T_e}{E_F}

    is the degeneracy parameter, comparing the thermal energy to the
    Fermi energy, and the coefficients for the fitting formula are
    :math:`A = 0.25945`\ , :math:`B = 0.0072`\ , and :math:`b = 0.858`\ .

    References
    ----------
    .. [1] Ichimaru, Statistical Plasma Physics Addison-Wesley,
       Reading, MA, 1991.

    .. [2] Gregori, G., et al. "Theoretical model of x-ray scattering as a
       dense matter probe." Physical Review E 67.2 (2003): 026412.

    Example
    -------
    >>> from astropy import units as u
    >>> _chemical_potential_interp(n_e=1e23*u.cm**-3, T=11000*u.K)  # doctest: +SKIP
    <Quantity 8.17649>

    """
    raise NotImplementedError(
        "This function has been temporarily disabled due to a bug.\n"
        "Please refer to https://github.com/PlasmaPy/PlasmaPy/issues/726 \n"
        "and https://github.com/astropy/astropy/issues/9721 "
        "for progress in fixing it."
    )
    A = 0.25945
    B = 0.072
    b = 0.858
    theta = k_B * T / Fermi_energy(n_e)
    term1 = -3 / 2 * np.log(theta)
    term2 = np.log(4 / (3 * np.sqrt(np.pi)))
    term3num = A * theta ** (-b - 1) + B * theta ** (-(b + 1) / 2)
    term3den = 1 + A * theta ** (-b)
    term3 = term3num / term3den
    beta_mu = term1 + term2 + term3
    return beta_mu.to(u.dimensionless_unscaled)

max_stars_repo_name: MarikinPaulina/PlasmaPy

max_issues_repo_name: MarikinPaulina/PlasmaPy

max_forks_repo_name: MarikinPaulina/PlasmaPy

reason: import numpy,import astropy,from astropy


text:
import settings
import numpy as np
import mxnet as mx
import logging
from fileiter import FileIter
import cv2
import os

import sys
sys.path.append("..")
from symbols.symbol_common import *

def convolution_module(net, kernel_size=(3,3), pad_size=(1,1), stride=(1, 1), filter_count = 32, work_space=2048, batch_norm=True, stage="stage1", bsize=settings.SEGMENT_BATCH_SIZE):
    net = mx.sym.Convolution(data=net, kernel=kernel_size, stride=stride, pad=pad_size, num_filter=filter_count,
                              workspace=work_space)
    if (batch_norm):
        net = mx.sym.BatchNorm(net)
    #net = mx.sym.Activation(net, act_type="relu")
    net = mx.sym.LeakyReLU(net, act_type='elu')
    print_inferred_shape(net, stage=stage, bsize=bsize)

    net = mx.sym.Convolution(data=net, kernel=kernel_size, stride=stride, pad=pad_size, num_filter=filter_count,
                              workspace=work_space)
    if (batch_norm):
        net = mx.sym.BatchNorm(net)
    #net = mx.sym.Activation(net, act_type="relu")
    net = mx.sym.LeakyReLU(net, act_type='elu')
    print_inferred_shape(net, stage=stage, bsize=bsize)

    return net

def upconvolution_module(net, kernel_size=(2,2), pad_size=(0,0), stride_size=(2, 2), filter_count = 32, work_space=2048, batch_norm=True, stage="stage1", bsize=settings.SEGMENT_BATCH_SIZE):
    net = mx.sym.Deconvolution(net, kernel=kernel_size, pad=pad_size, stride=stride_size, num_filter=filter_count, workspace = work_space)
    if (batch_norm):
        net = mx.sym.BatchNorm(net)
    #net = mx.sym.Activation(net, act_type="relu")
    net = mx.sym.LeakyReLU(net, act_type='elu')
    print_inferred_shape(net, stage=stage, bsize=bsize)

    return net

def downpool(net, kernel = (2, 2), stride = (2, 2), pad = (0, 0), stage="stage1", bsize=settings.SEGMENT_BATCH_SIZE):

    net = mx.symbol.Pooling(net, pool_type="max", kernel = kernel, stride = stride, pad = pad)
    print_inferred_shape(net, stage=stage, bsize=bsize)
    return net

def get_unet_small(batch_size=settings.SEGMENT_BATCH_SIZE):
    source = mx.sym.Variable("data")
    kernel_size = (3, 3)
    conv_pad_size = (1, 1)
    pad_size = (1, 1)
    stride_size = (1, 1)
    filter_count = 32

    conv1 = convolution_module(source, kernel_size, pad_size=(1,1), stride=stride_size, filter_count=filter_count, stage="stage1", bsize=batch_size)
    net = conv1
    net = downpool(net, stage="stage1", bsize=batch_size)

    conv2 = convolution_module(net, kernel_size, pad_size=(1,1) , stride = stride_size, filter_count=2*filter_count, stage="stage2", bsize=batch_size)
    net = conv2
    net = downpool(net, stage="stage2", bsize=batch_size)

    conv3 = convolution_module(net, kernel_size, pad_size, stride_size, filter_count=4*filter_count, stage="stage3", bsize=batch_size)
    net = conv3
    net = downpool(net, stage="stage3", bsize=batch_size)

    conv4 = convolution_module(net, kernel_size, pad_size, stride_size, filter_count=8*filter_count, stage="stage4", bsize=batch_size)
    net = conv4
    net = downpool(net, stage="stage4", bsize=batch_size)

    conv5 = convolution_module(net, kernel_size, pad_size, stride_size, filter_count=16*filter_count, stage="stage5", bsize=batch_size)
    net = conv5

    upconv6 = upconvolution_module(net, stage="stage6", filter_count=8*filter_count, bsize=batch_size)
    net = mx.sym.Concat(*[conv4, upconv6])
    conv7 = convolution_module(net, kernel_size, pad_size, stride_size, filter_count=8*filter_count, stage="stage7", bsize=batch_size)
    net = conv7

    upconv8 = upconvolution_module(net, stage="stage8", filter_count=4*filter_count, bsize=batch_size)
    net = mx.sym.Concat(*[conv3, upconv8])
    conv9 = convolution_module(net, kernel_size, pad_size, stride_size, filter_count=4*filter_count, stage="stage9", bsize=batch_size)
    net = conv9

    upconv10 = upconvolution_module(net, stage="stage10", filter_count=2*filter_count, bsize=batch_size)
    net = mx.sym.Concat(*[conv2, upconv10])
    conv11 = convolution_module(net, kernel_size, pad_size=(1,1), stride=(1,1), filter_count=2*filter_count, stage="stage11", bsize=batch_size)
    net = conv11

    upconv12 = upconvolution_module(net, pad_size=(0,0), stage="stage12", filter_count=filter_count, bsize=batch_size)
    net = mx.sym.Concat(*[conv1, upconv12])
    conv13 = convolution_module(net, kernel_size, pad_size=(1,1), stride=stride_size, filter_count=filter_count, stage="stage13", bsize=batch_size)
    net = conv13

    final_filter =  2
    final_kernel = (3, 3)
    final_pad = (1, 1)
    final_stride=(1, 1)
    net = mx.sym.Convolution(data=net, kernel=final_kernel, stride=final_stride, pad=final_pad, num_filter=final_filter,
                              workspace=1024)
    net = mx.sym.Reshape(net, shape=(batch_size, final_filter, settings.SCALE_WIDTH * settings.SCALE_HEIGHT))
    print_inferred_shape(net, stage="stage14", bsize=batch_size)

    net = mx.symbol.SoftmaxOutput(data=net, name='softmax', multi_output=True)

    #net = mx.symbol.LogisticRegressionOutput(data=net, name='softmax')
    return net

if __name__ == "__main__":
    get_unet_small()

reason: import numpy


text:
"""Work with chimera graphs/ tiles"""
import logging

import networkx as nx
from placeandroute.tilebased.detailed_channel_routing import detailed_channel_routing, get_chimera_quotient_graph

import dwave_networkx as dwnx

def chimera_tiles(w, h=None):
    if h is None: h = w
    hwg = dwnx.chimera_graph(w,n=h)  # type: nx.Graph
    quotient_graph, quotient_mapping, qubit_mapping = get_chimera_quotient_graph(hwg)

    choices = []
    for a, b, data in quotient_graph.edges(data=True):
        if data["weight"] == 16:
            choices.extend(([a, b], [b, a]))
    for nodeset, data in quotient_graph.nodes(data=True):
        data["capacity"] = len(nodeset)
        data["usage"] = 0
    return quotient_graph, choices

def test_chimera_tiles(chimera_graph, tile_graph):
    assert all(chimera_graph.has_edge(n1 * 4, n2 * 4) for n1, n2 in tile_graph.edges)
    assert all(chimera_graph.has_edge(n1 * 4 + 1, n2 * 4 + 1) for n1, n2 in tile_graph.edges)
    assert all(chimera_graph.has_edge(n1 * 4, n2 * 4 + 1) for n1, n2, data in tile_graph.edges.data() if
               data["capacity"] == 16)
    assert all(not chimera_graph.has_edge(n1 * 4, n2 * 4 + 1) for n1, n2, data in tile_graph.edges.data() if
               data["capacity"] != 16)

def load_chimera(tsize):
    g, chs = chimera_tiles(tsize, tsize)
    original_graph = dwnx.chimera_graph(tsize)

    logging.info("Hardware stats (compressed): nodes: %d(%d) edges: %d(%d)",
        original_graph.number_of_nodes(),g.number_of_nodes(),
        original_graph.number_of_edges(), g.number_of_edges())

    return chs, g, original_graph

def expand_solution(_, chains, chimera_graph):
    ret, _ = detailed_channel_routing(chains, chimera_graph)
    return ret

max_stars_repo_name: boothby/placeandroute

max_issues_repo_name: boothby/placeandroute

max_forks_repo_name: boothby/placeandroute

reason: import networkx


text:
from __future__ import print_function

import numpy as np

class Interpreter(object):
    """interpreting program vectors into understandable program strings"""
    def __init__(self, translate, rotate, end):
        self.translate = translate
        self.rotate = rotate
        self.end = end

    def interpret(self, pgm, param):

        n_block = pgm.shape[0]
        param = np.round(param).astype(np.int32)

        result = ""
        for i in range(n_block):
            res = self.interpret_block(pgm[i], param[i])
            if res is None:
                continue
            else:
                result += res
                result += "\n"

        return result

    def interpret_block(self, pgm, param):
        """
        interpret each block
        """
        flag = 1
        block_res = []
        if pgm[0] == self.translate:
            if pgm[1] == self.translate:
                if 1 <= pgm[2] < self.translate:

                    sentence = "for(i<{}, 'Trans', u1=({},{},{}))"\
                        .format(param[0, 0], param[0, 1], param[0, 2], param[0, 3])
                    block_res.append(sentence)

                    sentence = "for(i<{}, 'Trans', u2=({},{},{}))"\
                        .format(param[1, 0], param[1, 1], param[1, 2], param[1, 3])
                    block_res.append("    "+sentence)

                    sentence = self.interpret_sentence(pgm[2], param[2], num_trans=2, num_rot=0)
                    block_res.append("        "+sentence)

                else:
                    pass
            elif 1 <= pgm[1] < self.translate:

                sentence = "for(i<{}, 'Trans', u=({},{},{}))" \
                    .format(param[0, 0], param[0, 1], param[0, 2], param[0, 3])
                block_res.append(sentence)

                sentence = self.interpret_sentence(pgm[1], param[1], num_trans=1, num_rot=0)
                block_res.append("    " + sentence)

            else:
                pass
        elif pgm[0] == self.rotate:
            if pgm[1] == 10 or pgm[1] == 17:

                sentence = "for(i<{}, 'Rot', theta={}\N{DEGREE SIGN}, axis=({},{},{})"\
                    .format(param[0, 0], int(360/param[0,0]),
                            param[1, 0], param[1, 1], param[1, 2])
                block_res.append(sentence)

                sentence = self.interpret_sentence(pgm[1], param[1], num_trans=0, num_rot=1)
                block_res.append("    " + sentence)

            else:
                pass
        elif 1 <= pgm[0] < self.translate:

            sentence = self.interpret_sentence(pgm[0], param[0], num_trans=0, num_rot=0)
            block_res.append(sentence)

        else:
            pass

        if len(block_res) == 0:
            return None
        else:
            res = ''
            for i in range(len(block_res)):
                res += block_res[i] + '\n'
            return res

    def interpret_sentence(self, pgm, param, num_trans=0, num_rot=0):
        """
        interpret each sentence
        """
        if num_trans == 0 and num_rot == 0:
            if pgm == 1:
                sentence = "draw('Leg', 'Cub', P=({},{},{}), G=({},{},{}))"\
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 2:
                sentence = "draw('Top', 'Rec', P=({},{},{}), G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 3:
                sentence = "draw('Top', 'Square', P=({},{},{}), G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 4:
                sentence = "draw('Top', 'Circle', P=({},{},{}), G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 5:
                sentence = "draw('Layer', 'Rec', P=({},{},{}), G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 6:
                sentence = "draw('Sup', 'Cylinder', P=({},{},{}), G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 7:
                sentence = "draw('Sup', 'Cub', P=({},{},{}), G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 8:
                sentence = "draw('Base', 'Circle', P=({},{},{}), G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 9:
                sentence = "draw('Base', 'Square', P=({},{},{}), G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 10:
                angle = round(param[5]) % 4
                if angle == 0:
                    p1, p2, p3 = param[0], param[1], param[2] - param[4]
                elif angle == 1:
                    p1, p2, p3 = param[0], param[1] + param[4], param[2]
                elif angle == 2:
                    p1, p2, p3 = param[0], param[1], param[2] + param[4]
                elif angle == 3:
                    p1, p2, p3 = param[0], param[1] - param[4], param[2]
                else:
                    raise ValueError("The angle type of the cross is wrong")
                sentence = "draw('Base', 'Line', P1=({},{},{}), P2=({},{},{}))" \
                    .format(param[0], param[1], param[2], p1, p2, p3)
            elif pgm == 11:
                sentence = "draw('Sideboard', 'Cub', P=({},{},{}), G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 12:
                sentence = "draw('Hori_Bar', 'Cub', P=({},{},{}), G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 13:
                sentence = "draw('Vert_Board', 'Cub', P=({},{},{}), G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 14:
                sentence = "draw('Locker', 'Cub', P=({},{},{}), G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 15:
                theta = np.arctan(float(param[6])/param[3]) / np.pi * 180
                sentence = "draw('Back', 'Cub', P=({},{},{}), G=({},{},{}), theta={}\N{DEGREE SIGN})" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5], int(theta))
            elif pgm == 16:
                sentence = "draw('Chair_Beam', 'Cub', P=({},{},{}), G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 17:
                sentence = "draw('Connect', 'Line', P1=({},{},{}), P2=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 18:
                sentence = "draw('Back_sup', 'Cub', P=({},{},{}), G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif self.translate <= pgm <= self.end:
                sentence = None
            else:
                sentence = None

        elif num_trans == 1 and num_rot == 0:
            if pgm == 1:
                sentence = "draw('Leg', 'Cub', P=({},{},{})+i*u, G=({},{},{}))"\
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 2:
                sentence = "draw('Top', 'Rec', P=({},{},{})+i*u, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 3:
                sentence = "draw('Top', 'Square', P=({},{},{})+i*u, G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 4:
                sentence = "draw('Top', 'Circle', P=({},{},{})+i*u, G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 5:
                sentence = "draw('Layer', 'Rec', P=({},{},{})+i*u, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 6:
                sentence = "draw('Sup', 'Cylinder', P=({},{},{})+i*u, G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 7:
                sentence = "draw('Sup', 'Cub', P=({},{},{})+i*u, G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 8:
                sentence = "draw('Base', 'Circle', P=({},{},{})+i*u, G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 9:
                sentence = "draw('Base', 'Square', P=({},{},{})+i*u, G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 10:
                angle = round(param[5]) % 4
                if angle == 0:
                    p1, p2, p3 = param[0], param[1], param[2] - param[4]
                elif angle == 1:
                    p1, p2, p3 = param[0], param[1] + param[4], param[2]
                elif angle == 2:
                    p1, p2, p3 = param[0], param[1], param[2] + param[4]
                elif angle == 3:
                    p1, p2, p3 = param[0], param[1] - param[4], param[2]
                else:
                    raise ValueError("The angle type of the cross is wrong")
                sentence = "draw('Base', 'Line', P1=({},{},{})+i*u, P2=({},{},{}))+i*u" \
                    .format(param[0], param[1], param[2], p1, p2, p3)
            elif pgm == 11:
                sentence = "draw('Sideboard', 'Cub', P=({},{},{})+i*u, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 12:
                sentence = "draw('Hori_Bar', 'Cub', P=({},{},{})+i*u, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 13:
                sentence = "draw('Vert_Board', 'Cub', P=({},{},{})+i*u, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 14:
                sentence = "draw('Locker', 'Cub', P=({},{},{})+i*u, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 15:
                theta = np.arctan(float(param[6])/param[3]) / np.pi * 180
                sentence = "draw('Back', 'Cub', P=({},{},{})+i*u, G=({},{},{}), theta={}\N{DEGREE SIGN})" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5], int(theta))
            elif pgm == 16:
                sentence = "draw('Chair_Beam', 'Cub', P=({},{},{})+i*u, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 17:
                sentence = "draw('Connect', 'Line', P1=({},{},{})+i*u, P2=({},{},{}))+i*u" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 18:
                sentence = "draw('Back_sup', 'Cub', P=({},{},{})+i*u, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif self.translate <= pgm <= self.end:
                sentence = None
            else:
                sentence = None

        elif num_trans == 2 and num_rot == 0:
            if pgm == 1:
                sentence = "draw('Leg', 'Cub', P=({},{},{})+i*u1+j*u2, G=({},{},{}))"\
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 2:
                sentence = "draw('Top', 'Rec', P=({},{},{})+i*u1+j*u2, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 3:
                sentence = "draw('Top', 'Square', P=({},{},{})+i*u1+j*u2, G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 4:
                sentence = "draw('Top', 'Circle', P=({},{},{})+i*u1+j*u2, G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 5:
                sentence = "draw('Layer', 'Rec', P=({},{},{})+i*u1+j*u2, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 6:
                sentence = "draw('Sup', 'Cylinder', P=({},{},{})+i*u1+j*u2, G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 7:
                sentence = "draw('Sup', 'Cub', P=({},{},{})+i*u1+j*u2, G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 8:
                sentence = "draw('Base', 'Circle', P=({},{},{})+i*u1+j*u2, G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 9:
                sentence = "draw('Base', 'Square', P=({},{},{})+i*u1+j*u2, G=({},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4])
            elif pgm == 10:
                angle = round(param[5]) % 4
                if angle == 0:
                    p1, p2, p3 = param[0], param[1], param[2] - param[4]
                elif angle == 1:
                    p1, p2, p3 = param[0], param[1] + param[4], param[2]
                elif angle == 2:
                    p1, p2, p3 = param[0], param[1], param[2] + param[4]
                elif angle == 3:
                    p1, p2, p3 = param[0], param[1] - param[4], param[2]
                else:
                    raise ValueError("The angle type of the cross is wrong")
                sentence = "draw('Base', 'Line', P1=({},{},{})+i*u1+j*u2, P2=({},{},{}))+i*u1+j*u2" \
                    .format(param[0], param[1], param[2], p1, p2, p3)
            elif pgm == 11:
                sentence = "draw('Sideboard', 'Cub', P=({},{},{})+i*u1+j*u2, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 12:
                sentence = "draw('Hori_Bar', 'Cub', P=({},{},{})+i*u1+j*u2, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 13:
                sentence = "draw('Vert_Board', 'Cub', P=({},{},{})+i*u1+j*u2, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 14:
                sentence = "draw('Locker', 'Cub', P=({},{},{})+i*u1+j*u2, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 15:
                theta = np.arctan(float(param[6])/param[3]) / np.pi * 180
                sentence = "draw('Back', 'Cub', P=({},{},{})+i*u1+j*u2, G=({},{},{}), theta={}\N{DEGREE SIGN})" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5], int(theta))
            elif pgm == 16:
                sentence = "draw('Chair_Beam', 'Cub', P=({},{},{})+i*u1+j*u2, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 17:
                sentence = "draw('Connect', 'Line', P1=({},{},{})+i*u1+j*u2, P2=({},{},{}))+i*u" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif pgm == 18:
                sentence = "draw('Back_sup', 'Cub', P=({},{},{})+i*u1+j*u2, G=({},{},{}))" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            elif self.translate <= pgm <= self.end:
                sentence = None
            else:
                sentence = None

        elif num_trans == 0 and num_rot == 1:
            if pgm == 10:
                angle = round(param[5]) % 4
                if angle == 0:
                    p1, p2, p3 = param[0], param[1], param[2] - param[4]
                elif angle == 1:
                    p1, p2, p3 = param[0], param[1] + param[4], param[2]
                elif angle == 2:
                    p1, p2, p3 = param[0], param[1], param[2] + param[4]
                elif angle == 3:
                    p1, p2, p3 = param[0], param[1] - param[4], param[2]
                else:
                    raise ValueError("The angle type of the cross is wrong")
                sentence = "draw('Base', 'Line', P1=({},{},{}), P2=({},{},{}), theta*i, axis)" \
                    .format(param[0], param[1], param[2], p1, p2, p3)
            elif pgm == 17:
                sentence = "draw('Base', 'Line', P1=({},{},{}), P2=({},{},{}), theta*i, axis)" \
                    .format(param[0], param[1], param[2],
                            param[3], param[4], param[5])
            else:
                sentence = None

        else:
            sentence = None

        return sentence


text:
#  ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
#  MIT License
#
#  Copyright (c) 2021 Nathan Juraj Michlo
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#  SOFTWARE.
#  ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

"""
OneMax serial example based on:
https://github.com/DEAP/deap/blob/master/examples/ga/onemax_numpy.py
"""

import functools
import numpy as np
from ruck import *

class OneMaxModule(EaModule):

    def __init__(
        self,
        population_size: int = 300,
        offspring_num: int = None,  # offspring_num (lambda) is automatically set to population_size (mu) when `None`
        member_size: int = 100,
        p_mate: float = 0.5,
        p_mutate: float = 0.5,
        ea_mode: str = 'simple'
    ):
        # save the arguments to the .hparams property. values are taken from the
        # local scope so modifications can be captured if the call to this is delayed.
        self.save_hyperparameters()
        # implement the required functions for `EaModule`
        self.generate_offspring, self.select_population = R.make_ea(
            mode=self.hparams.ea_mode,
            offspring_num=self.hparams.offspring_num,
            mate_fn=R.mate_crossover_1d,
            mutate_fn=functools.partial(R.mutate_flip_bit_groups, p=0.05),
            select_fn=functools.partial(R.select_tournament, k=3),
            p_mate=self.hparams.p_mate,
            p_mutate=self.hparams.p_mutate,
        )

    def evaluate_values(self, values):
        return map(np.sum, values)

    def gen_starting_values(self) -> Population:
        return [
            np.random.random(self.hparams.member_size) < 0.5
            for i in range(self.hparams.population_size)
        ]

if __name__ == '__main__':
    # create and train the population
    module = OneMaxModule(population_size=300, member_size=100)
    pop, logbook, halloffame = Trainer(generations=40, progress=True).fit(module)

    print('initial stats:', logbook[0])
    print('final stats:', logbook[-1])
    print('best member:', halloffame.members[0])


text:
from firedrake import *
from fireshape import PdeConstraint
from slepc4py import SLEPc
from firedrake.petsc import PETSc
import numpy as np
from pyadjoint.tape import no_annotations

# Load file to load initial guesses
import sys
sys.path.append('../utils')
from vtktools import vtu

RB = __import__("rayleigh-benard")

# Enfore pointwise boundary condition on the pressure
class PointwiseBC(DirichletBC):
    @utils.cached_property
    def nodes(self):
        out = np.array([0], dtype=np.int32)
        return out

class Moore_Spence(PdeConstraint):
    """
    Define the generalized Moore-Spence system for computing Hopf bifurcations.
    """

    # Initialization
    def __init__(self, mesh_m):
        super().__init__()

        # Create function space
        self.mesh = mesh_m
        self.Vu = VectorFunctionSpace(self.mesh, "CG", 2)
        self.Vp = FunctionSpace(self.mesh, "CG", 1)
        self.VT = FunctionSpace(self.mesh, "CG", 1)
        self.V = MixedFunctionSpace([self.Vu, self.Vp, self.VT])
        self.R = FunctionSpace(self.mesh, "R", 0)
        self.Z = MixedFunctionSpace([self.V, self.R, self.R, self.V, self.V])

        # Create solution
        self.sol_opt = Function(self.Z)
        self.solution_n = Function(self.Z)
        self.solution = Function(self.Z, name="State")
        self.solution_test = TestFunction(self.Z)

        # Initialize some constans
        self.diff = Constant(-1.0)
        self.init_area = assemble(1.0*dx(self.mesh))
        self.failed_to_solve = False

        self.mesh_pvd = File("result/mesh.pvd")

        # Write generalized Moore-Spence system of equations
        (u, p, T, Ra, mu, v_u, v_p, v_T, w_u, w_p, w_T) = split(self.solution)
        (v, q, S, test_Ra, test_mu, v_v, v_q, v_S, w_v, w_q, w_S) = split(self.solution_test)

        # Residual of the PDE
        self.F1 = self.residual(u, p, T, Ra, v, q, S)

        # Define normalization function
        self.cu = Constant((1,1))
        self.cp = Constant(1)
        self.cT = Constant(1)
        self.inner_v = Constant(0)
        self.inner_w = Constant(1)

        # Define the weak form
        self.F2 = derivative(self.residual(u, p, T, Ra, v_v, v_q, v_S), self.solution, as_vector([vx for vx in v_u] + [v_p] + [v_T] + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])) \
                 + mu*inner(w_u,v_v)*dx + mu*inner(w_T,v_S)*dx
        self.F3 = derivative(self.residual(u, p, T, Ra, w_v, w_q, w_S), self.solution, as_vector([wx for wx in w_u] + [w_p] + [w_T] + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])) \
                 - mu*inner(v_u,w_v)*dx - mu*inner(v_T,w_S)*dx
        self.F4 = (inner(v_u, self.cu) + inner(v_p, self.cp) + inner(v_T, self.cT) - self.inner_v)*test_Ra*dx
        self.F5 = (inner(w_u, self.cu) + inner(w_p, self.cp) + inner(w_T, self.cT) - self.inner_w)*test_mu*dx
        self.F = self.F1 + self.F2 + self.F3 + self.F4 + self.F5

        # Boundary conditions
        self.bcs = self.boundary_conditions(self.Z)

        self.solver_params = {
                                "mat_type": "matfree",
                                "snes_type": "newtonls",
                                "snes_monitor": None,
                                "snes_converged_reason": None,
                                "snes_linesearch_type": "l2",
                                "snes_linesearch_maxstep": 1.0,
                                "snes_linesearch_damping": 1.0,
                                "snes_max_it": 100,
                                "snes_atol": 1.0e-8,
                                "snes_rtol": 0.0,
                                "snes_stol": 0.0,
                                "ksp_type": "fgmres",
                                "ksp_monitor_true_residual": None,
                                "ksp_max_it": 10,
                                "pc_type": "fieldsplit",
                                "pc_fieldsplit_type": "schur",
                                "pc_fieldsplit_schur_fact_type": "full",
                                "pc_fieldsplit_0_fields": "0,1,2,5,6,7,8,9,10",
                                "pc_fieldsplit_1_fields": "3,4",
                                "fieldsplit_0_ksp_type": "preonly",
                                "fieldsplit_0_pc_type": "python",
                                "fieldsplit_0_pc_python_type": "firedrake.AssembledPC",
                                "fieldsplit_0_assembled_pc_type": "lu",
                                "fieldsplit_0_assembled_pc_factor_mat_solver_type": "mumps",
                                "fieldsplit_0_assembled_mat_mumps_icntl_14": 200,
                                "mat_mumps_icntl_14": 200,
                                "fieldsplit_1_ksp_type": "gmres",
                                "fieldsplit_1_ksp_max_it": 2,
                                "fieldsplit_1_ksp_convergence_test": "skip",
                                "fieldsplit_1_pc_type": "none",
                            }

    # Boundary conditions
    def boundary_conditions(self, Z):
        bcs = [
                DirichletBC(Z.sub(0), Constant((0.0,0.0)), (1, 2, 3, 4)), # u = 0 at the Boundary
                PointwiseBC(Z.sub(1), Constant(0.0), (1,2,3,4)), # p(0,0) = 0
                DirichletBC(Z.sub(2), Constant(1.0), (3)), # T = 1 at y = 0
                DirichletBC(Z.sub(2), Constant(0.0), (4)), # T = 0 at y = 1
                DirichletBC(Z.sub(5), Constant((0.0,0.0)), (1, 2, 3, 4)), # u = 0 at the Boundary
                PointwiseBC(Z.sub(6), Constant(0.0), (1,2,3,4)), # p(0,0) = 0
                DirichletBC(Z.sub(7), Constant(0.0), (3,4)), # T = 0 at y = 0,1
                DirichletBC(Z.sub(8), Constant((0.0,0.0)), (1, 2, 3, 4)), # u = 0 at the Boundary
                PointwiseBC(Z.sub(9), Constant(0.0), (1,2,3,4)), # p(0,0) = 0
                DirichletBC(Z.sub(10), Constant(0.0), (3,4)) # T = 0 at y = 0,1
                ]
        return bcs

    # Residual of the system
    def residual(self, u, p, T, Ra, v, q, S):
        Pr = 1.0
        g = Constant((0, 1))
        nn  = FacetNormal(self.mesh)
        F = -1.0*(
            - inner(grad(u), grad(v))*dx + inner(dot(u,grad(u)),v)*dx + inner(p, div(v))*dx + Ra*Pr*inner(T*g, v)*dx
            + inner(div(u), q)*dx
            - inner(grad(T), grad(S))*dx
            - inner(dot(u, grad(T)), S)*dx + inner(dot(grad(T),nn),S)*ds(3) + inner(dot(grad(T),nn),S)*ds(4)
            )
        return F

    # Print bifurcation parameter
    def print_lambda(self, sol):
        with sol.sub(3).dat.vec_ro as x:
            param = x.norm()
        with sol.sub(4).dat.vec_ro as x:
            mu = x.norm()
        print("### Ra = %f, mu = %f ###\n" % (param, mu))

    # Compute initial guess for the generalized Moore-Spence system
    @no_annotations
    def compute_guess(self):

        self.problem = RB.RayleighBenardProblem()

        # Initial parameters
        eig_index = 1

        # Get boundary conditions
        bcs = self.problem.boundary_conditions(self.V, [])

        # Solver parameters
        solver_params = self.problem.solver_parameters([],[])

        # Get the residual
        z = Function(self.V)
        w = TestFunction(self.V)
        Ra = Constant(param)

        (u,p,T) = split(z)
        (v,q,S) = split(w)
        Fsol = self.residual(u, p, T, Ra, v, q, S)

        # Load the initial guess
        vtu_class = vtu("Initial_guess/solution.vtu")

        # Load velocity
        W_guess = VectorFunctionSpace(self.mesh, "CG", 2, dim=2)
        X_guess = interpolate(SpatialCoordinate(self.mesh), W_guess)
        reader_guess = lambda X: vtu_class.ProbeData(np.c_[X, np.zeros(X.shape[0])], "u")[:,0:2]
        u_vtk = Function(self.Vu)
        u_vtk.dat.data[:] = reader_guess(X_guess.dat.data_ro)

        # Load pressure
        W_guess = VectorFunctionSpace(self.mesh, "CG", 1, dim=2)
        X_guess = interpolate(SpatialCoordinate(self.mesh), W_guess)
        reader_guess = lambda X: vtu_class.ProbeData(np.c_[X, np.zeros(X.shape[0])], "p")[:,0]
        p_vtk = Function(self.Vp)
        p_vtk.dat.data[:] = reader_guess(X_guess.dat.data_ro)

        # Load temperature
        reader_guess = lambda X: vtu_class.ProbeData(np.c_[X, np.zeros(X.shape[0])], "T")[:,0]
        t_vtk = Function(self.VT)
        t_vtk.dat.data[:] = reader_guess(X_guess.dat.data_ro)

        # Assign initial guess
        z.split()[0].assign(u_vtk)
        z.split()[1].assign(p_vtk)
        z.split()[2].assign(t_vtk)

        # Solve the RB equation
        solve(Fsol == 0, z, bcs=bcs, solver_parameters=solver_params)

        # Save the solution
        pvd = File("result/sol.pvd")
        self.problem.save_pvd(z, pvd)

        # Solve the eigenvalue problem
        d = self.problem.compute_stability([Ra], [], z)

        # Select the eigenvalue and eigenvector
        mu_sol = d["eigenvalues"][eig_index].imag
        eig_R = d["eigenfunctions_R"][eig_index]
        eig_I = d["eigenfunctions_I"][eig_index]

        # Split the components
        (u_r,p_r,T_r) = split(eig_R)
        (u_i,p_i,T_i) = split(eig_I)

        # Compute the two inner product
        inner_v = assemble(inner(u_r, self.cu)*dx + inner(p_r, self.cp)*dx + inner(T_r, self.cT)*dx)
        inner_w = assemble(inner(u_i, self.cu)*dx + inner(p_i, self.cp)*dx + inner(T_i, self.cT)*dx)

        # Renormalize
        theta = np.arctan2(inner_v, inner_w)
        r = 1 / (inner_v*sin(theta) + inner_w*cos(theta))
        phi_r = Function(self.V).assign(eig_R)
        phi_i = Function(self.V).assign(eig_I)
        eig_R.assign(Constant(r*cos(theta))*phi_r-Constant(r*sin(theta))*phi_i)
        eig_I.assign(Constant(r*sin(theta))*phi_r+Constant(r*cos(theta))*phi_i)

        # Save the two eigenfunctions
        self.problem.save_pvd(eig_R, pvd)
        self.problem.save_pvd(eig_I, pvd)

        ### Assign the initial guess
        # Solution
        self.solution.split()[0].assign(z.split()[0])
        self.solution.split()[1].assign(z.split()[1])
        self.solution.split()[2].assign(z.split()[2])

        # Parameter
        self.solution.split()[3].assign(Ra)

        # Eigenvalue
        self.solution.split()[4].assign(mu_sol)

        # Eigenvector
        self.solution.split()[5].assign(eig_R.split()[0])
        self.solution.split()[6].assign(eig_R.split()[1])
        self.solution.split()[7].assign(eig_R.split()[2])
        self.solution.split()[8].assign(eig_I.split()[0])
        self.solution.split()[9].assign(eig_I.split()[1])
        self.solution.split()[10].assign(eig_I.split()[2])

    # Solve the generalized Moore-Spence system
    def solve(self):
        super().solve()

        area = assemble(1.0*dx(self.mesh))
        # Print relative area wrt to initial area
        print("\n### changed area = %f ###" % (area/self.init_area))

        try:
            print("### Solving Moore-Spence system ###")
            solve(self.F == 0, self.solution, bcs=self.bcs, solver_parameters=self.solver_params)
            self.failed_to_solve = False

            # Record the last successful PDE step
            self.solution_n.assign(self.solution)

        except:
            # assign last successful optimization step
            self.failed_to_solve = True