import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
import torch.nn.functional as F

from util import generate_data_categorical, plot_loss
from models import StructuralModel

N = 5
num_episodes = 200
batch_size = 50 # 1
num_test = batch_size
num_training = 1 # 100
num_transfers = 10 # 100

model = StructuralModel(N, batch_size, dtype=torch.float64)
model1 = StructuralModel(N, batch_size, dtype=torch.float64)
optimizer = torch.optim.SGD(model.modules_parameters(), lr=0.1)
optimizer1 = torch.optim.SGD(model1.modules_parameters(), lr=0.1)
losses = np.zeros((2, num_training, num_transfers, num_episodes))

trainingLoss = {
    'marginalConditional': [],
    'joint': [],
    'nonCausal': [],
}
for k in tqdm.trange(num_training):
    pi_A_1 = np.random.dirichlet(np.ones(N))
    pi_B_A = np.random.dirichlet(np.ones(N), size=N)
    pi_all = np.multiply(pi_B_A, pi_A_1)
    for w in range(int(1E3)):
import numpy as np
from tqdm import tnrange
import matplotlib.pyplot as plt
import torch
import torch.nn.functional as F
from util import generate_data_categorical
from models import StructuralModel

N = 20
model = StructuralModel(N, dtype=torch.float64)

optimizer = torch.optim.SGD(model.modules_parameters(), lr=1e-1)
meta_optimizer = torch.optim.RMSprop([model.w], lr=1e-2)

num_runs = 1  # 10
num_training = 1  # 100
num_transfer = 1000
num_gradient_steps = 2
batch_size = 100

train_batch_size = 1000
transfer_batch_size = 10

alphas = np.zeros((num_runs, num_training, num_transfer))

for j in range(num_runs):
    model.w.data.zero_()
    for i in tnrange(num_training, leave=False):
        # Step 1: Sample a joint distribution before intervention
        pi_A_1 = np.random.dirichlet(np.ones(N))
        pi_B_A = np.random.dirichlet(np.ones(N), size=N)
Ejemplo n.º 3
0
def run_map_loads(cart3dGeom='Components.i.triq',
                  bdfModel='fem.bdf',
                  bdfModelOut='fem.loads.out'):
    assert os.path.exists(bdfModel), '%r doesnt exist' % bdfModel

    t0 = time()
    mesh = Cart3DReader()
    half_model = cart3dGeom + '_half'

    result_names = ['Cp', 'rho', 'rhoU', 'rhoV', 'rhoW', 'E']
    (nodes, elements, regions,
     loads) = mesh.read_cart3d(cart3dGeom, result_names=result_names)
    #Cp = loads['Cp']
    (nodes, elements, regions, loads) = mesh.make_half_model(nodes,
                                                             elements,
                                                             regions,
                                                             loads,
                                                             axis='y')

    Cp = loads['Cp']
    #(nodes, elements, regions, Cp) = mesh.renumber_mesh(nodes, elements, regions, Cp)
    mesh.write_cart3d(half_model, nodes, elements, regions, loads)

    Mach = 0.825
    pInf = 499.3  # psf, alt=35k (per Schaufele p. 11)
    pInf = pInf / 144.  # convert to psi
    qInf = 1.4 / 2. * pInf * Mach**2.
    aeroModel = AeroModel(nodes, elements, Cp, pInf, qInf)
    log.info("elements[1] = %s" % elements[1])
    del elements, nodes, Cp

    fem = BDF(debug=True, log=log)
    fem.read_bdf(bdfModel)
    sys.stdout.flush()

    propertyRegions = [
        1, 1101, 1501, 1601, 1701, 1801, 1901, 2101, 2501, 2601, 2701, 2801,
        2901, 10103, 10201, 10203, 10301, 10401, 10501, 10601, 10701, 10801,
        10901, 20103, 20203, 20301, 20401, 20501, 20601, 20701, 20801, 20901,
        701512, 801812
    ]

    # 1 inboard
    # 1000s upper - lower inboard
    # 2000s lower - lower inboard
    # big - fin

    structuralModel = StructuralModel(fem, propertyRegions)

    mapper = LoadMapping(aeroModel, structuralModel)
    t1 = time()
    mapper.set_flight_condition(pInf, qInf)
    mapper.setOutput(bdffile=bdfModelOut, loadCase=1)
    log.info("setup time = %g sec; %g min" % (t1 - t0, (t1 - t0) / 60.))

    mapper.build_mapping_matrix(debug=False)
    t2 = time()
    log.info("mapping matrix time = %g sec; %g min" % (t2 - t1,
                                                       (t2 - t1) / 60.))

    mapper.mapLoads()
    t3 = time()
    log.info("map loads time = %g sec" % (t3 - t2))
    log.info("total time = %g min" % ((t3 - t0) / 60.))