def start_comsol(self):
     """Initialize the COMSOL file"""
     self.client = mph.Client()
     self.model = self.client.load(self.mph_file)
     ## Forces to get all the resonances from 0 MHz
     self.model.java.study(self.study_tag).feature("eig").set(
         'shiftactive', 'on')
     self.model.java.study(self.study_tag).feature("eig").set('shift', '0')
     ## Set density ----------------------------------------------------------
     self.model.parameter('rho', str(self._density) + " [kg/m^3]")
     ## Set Mesh -------------------------------------------------------------
     self.model.java.component('comp1').mesh("mesh1").automatic(True)
     self.model.java.component('comp1').mesh("mesh1").autoMeshSize(
         self._mesh)
Beispiel #2
0
def test_init():
    global client
    if mph.option('session') == 'platform-dependent':
        mph.option('session', 'client-server')
    client = mph.start(cores=1)
    assert client.java is not None
    assert client.cores == 1
    with logging_disabled():
        try:
            mph.start()
        except NotImplementedError:
            pass
        try:
            mph.Client()
        except NotImplementedError:
            pass
Beispiel #3
0
    def stop(self):
        """Stops timing the step, displaying the elapsed time."""
        elapsed = now() - self.t0
        print(f'{elapsed:.1f} seconds', flush=True)


########################################
# Main                                 #
########################################

# Display welcome message.
print('Compacting Comsol models in the current folder.')

# Start Comsol client.
print('Running Comsol client on single processor core.')
client = mph.Client(cores=1)

# Loop over model files.
timer = Timer()
for file in Path.cwd().glob('*.mph'):

    name = file.relative_to(Path.cwd())
    print(f'{name}:')

    timer.start('Loading')
    try:
        model = client.load(file)
        timer.stop()
    except Exception:
        timer.cancel('Failed.')
        continue
Beispiel #4
0
def test_start():
    global client
    client = mph.Client(cores=1)
    assert client.java is not None
Beispiel #5
0
def test_client_connect():
    global client
    client = mph.Client(port=server.port)
    assert not client.models()
    assert client.cores == cores
Beispiel #6
0
    def __init__(self, number_of_cores=1, save_path=None, gui=False):
        '''Comsol Modeler opens a Comsol server listening on port 2036
        and a comsol client connected to that server.
        Then it opens the Comsol GUI if neede to follow the model modifications
        One just needs to connect the session to the server in the gui: 
            File/Comsol Multiphysics server/connect to server
        And to import the running model:
            File/Comsol Multiphysics server/import application from server            
         '''

        self._number_of_cores = number_of_cores

        # Run server
        #### self.server = mph.Server(cores=number_of_cores)
        #### print(f"Comsol server started listening on port {self.server_port}")

        # Connect client to server
        if self.client is None:
            try:
                ComsolModeler.client = mph.Client(cores=number_of_cores,
                                                  version=self.comsol_version,
                                                  port=self.server_port)
            except BaseException as e:
                print(e)
                raise RuntimeError(
                    r"could not connect to server: browse to "
                    r"C:\Program Files\COMSOL\COMSOL56\Multiphysics\COMSOL Launchers"
                    r"and launch 'COMSOL Multiphysics Server 5.6'."
                    r"You should also restart this Kernel before attempting to reconnect"
                    r"Launch 'COMSOL Multiphysics Client 5.6' and connect to the same"
                    r"server to check your model in real time. Don't forget to import the"
                    r"current application via file->COMSOL Multiphysics Server->Import application from server"
                )
        print(f"Comsol client (v{self.comsol_version}) connected to server")

        # Save current model
        if save_path is None:
            self._save_path = osp.join(
                osp.dirname(__file__),
                "MyModel.mph")  #str(Path.home().joinpath('MyModel.mph'))
        #self.pymodel = self.client.create(self.save_path)
        # Saves in order to reload using the MPh library
        ### self.pymodel.java.save(self.save_path)
        # Remove model from client before loading it again...
        # This is needed due to the way MpH works (model.py is instantiated
        # by loading a .mph file)
        #self.client.remove(self.pymodel)
        #print(f"Comsol model saved in {self.save_path}")

        # Start GUI
        if gui:
            self.start_gui()

        if len(self.client.models()) == 0:
            self.model = self.client.load(self.save_path)
        # Load model using Mph module
        self.model = self.client.models()[-1]

        ### Remove e

        ### self.model = self.client.load(self.save_path)
        #

        self.deleted_entities = []

        # dict containing the number of transforms having been applied to a
        # given entity. Every new transformation is named tN_name where name
        # is the actual entity name and N-1 the number of transforms it has
        # already experienced.
        # New trasnforms should always be applied to self._last_transfrom_name(name)
        self.transforms = {}

        import com  ## can be done once the JVM has been started by instantiating mph.Client...
        try:
            self.main_comp = self.model.java.component().create(
                "main_comp", True)
        except com.comsol.util.exceptions.FlException as e:
            pass
        self.main_comp = self.model.java.component("main_comp")

        try:
            self.main_comp.geom().create("main_geom", 3)
        except com.comsol.util.exceptions.FlException as e:
            pass
        self.main_geom = self.model.java.component("main_comp").geom(
            "main_geom")

        ## remove existing work_planes:
        try:
            self.main_geom.feature().remove("main_wp")
        except com.comsol.util.exceptions.FlException as e:
            pass
        try:
            self.main_geom.feature().remove("mesh_port_wp")
        except com.comsol.util.exceptions.FlException as e:
            pass
        #two workplanes are created : one for all physical components (main_wp)
        # and one for MESH and PORT layers
        self.main_wp = self.main_geom.create("main_wp", "WorkPlane")
        self.main_wp_entities = []
        self.mesh_port_wp = self.main_geom.create("mesh_port_wp", "WorkPlane")
        #self.main_comp.mesh().create("main_mesh")

        #PEC assignment is tricky, we create a selection "pec_sel" in the main wp,
        #and make it visible from the physics by setting "selplaneshow" to "on"
        #the boundaries belonging to pec_sel are then the input of a PEC in the physics

        #self.emw_physics = self.main_comp.physics().create("emw", "ElectromagneticWaves", "emw_geom")
        #self.pec = self.emw_physics.create("pec", "PerfectElectricConductor", 2)
        #self.pec_sel = self.main_wp.geom().selection().create("pec_sel", "CumulativeSelection")
        #self.main_wp.set("selplaneshow", "on")
        #self.pec.selection().named("main_geom_main_wp_pec_sel_bnd")

        #Comsol fails to read to long expressions, so we create intermediray parameters in a second table
        #elf.inter_params = self.model.java.param().group().create("inter_params")

        self.objects = {}
        self.main_geom.run()
Beispiel #7
0
def setup_module():
    global client, model
    client = mph.Client()
    model = client.load(file)
Beispiel #8
0
def setup_module():
    global client
    client = mph.Client(cores=1)
Beispiel #9
0
import parent
import mph
import sys

client = mph.Client()
sys.exit(2)