├── COMSOL.m ├── LICENSE └── README.md /COMSOL.m: -------------------------------------------------------------------------------- 1 | (* ::Package:: *) 2 | 3 | (* Package for interaction with COMSOL software and it's outputs 4 | * Author Karolis Misiunas (k.misiunas@gmail.com) 5 | * 6 | * ## Versions 7 | * 8 | * 2016-06-19 -- Moved to Mathematica's Import[] extension 9 | * 2016-09-11 -- support for reading parameter file and removed old ways 10 | * 2018-06-07 -- updated parameter import function to interpret um 11 | *) 12 | 13 | BeginPackage["COMSOL`"] 14 | 15 | COMSOL::usage = 16 | "Import[file_ , \"COMSOL\"] imports data from the comsol expor txt file. 17 | Import[file, {\"COMSOL\",\"Info\"}] gives the additional information stored in the file. 18 | Import[file, {\"COMSOL\",\"Headers\"}] gives the hearers of the table. 19 | Import[file, {\"COMSOL\",\"Parameters\"}] special import function for importing paraters file."; 20 | 21 | importParametersFile::usage = "" 22 | 23 | Begin["`Private`"] 24 | 25 | (* Extending Import[] *) 26 | 27 | 28 | ImportExport`RegisterImport[ 29 | "COMSOL", 30 | { 31 | "Headers" :> importComsolHeaders , 32 | "Info" :> importComsolInfo, 33 | "Parameters" :> importParametersFile, 34 | importComsolTable (*default importer *) 35 | } 36 | ]; 37 | 38 | importComsolTable[ filename_String, options___ ] := 39 | Import[filename, "Table"][[ CountCommentLines[filename] ;; ]] 40 | 41 | importComsolHeaders[ filename_String, options___ ] := { 42 | "Headers" -> 43 | ImportString[ StringDrop[ 44 | Import[filename, {"Text", "Lines", CountCommentLines[filename] - 1}], 45 | 1], "Table" , "FieldSeparators" -> " "] 46 | }; 47 | 48 | importComsolInfo[ filename_String, options___ ] := { 49 | "Info" -> StringDrop[# , 1] &/@ Import[file, {"Text", "Lines", Range[CountCommentLines[filename] -2]}] 50 | }; 51 | 52 | 53 | importParametersFile[ filename_String, options___ ] := { 54 | "Parameters" -> 55 | <| (#1 -> interpretQuantity[#2]) & @@@ Import[filename, "Table"][[ All, {1, 2}]] |> 56 | }; 57 | 58 | 59 | 60 | (*Secret helper methods*) 61 | 62 | (* count of comment lines in exported file *) 63 | CountCommentLines[file_String] := Module[{st, line, i}, 64 | st = OpenRead[file]; 65 | line = Read[st, String]; 66 | i = 1; 67 | While[ StringMatchQ[line, StartOfString ~~ "%" ~~ __] && line =!= EndOfFile, 68 | line = Read[st, String]; 69 | i = i + 1; 70 | ]; 71 | Close[st]; 72 | i 73 | ]; 74 | 75 | numericValueQ[ st_] := NumberQ@ToExpression[st] || NumberQ@extractValue[st]; 76 | 77 | getIfNotEmpty[list_] := If[Length[list] == 1 , First[list], list]; 78 | 79 | unitTraslate[unit_] := StringTake[ StringReplace[unit, {"um" -> "\[Mu]m"}] , {2, -2}]; 80 | 81 | extractUnits[st_] := unitTraslate@getIfNotEmpty@StringCases[st, "[" ~~ __ ~~ "]"]; 82 | 83 | 84 | extractValue[st_] := getIfNotEmpty@ToExpression@StringCases[st, num__ ~~ "[" ~~ __ ~~ "]" -> num]; 85 | 86 | interpretQuantity[st_] := Which[ 87 | Not@numericValueQ[ st], Return[st], 88 | NumberQ@ToExpression[st], ToExpression[st], 89 | True, Quantity[ extractValue[st], extractUnits[st]] 90 | ]; 91 | 92 | 93 | 94 | End[ ] 95 | 96 | EndPackage[ ] 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Karolis Misiunas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | mathematica-comsol 2 | ================== 3 | 4 | A link between from Wolfram Mathematica to COMSOL Multiphysics. 5 | 6 | At present it imports table data from COMSOL export or parameter files. 7 | 8 | ## Usage 9 | 10 | A [guide](http://mathematica.stackexchange.com/questions/669/how-to-install-packages) on installing packages. 11 | 12 | `Import[file_ , \"COMSOL\"]` imports data from the comsol expor txt file. 13 | `Import[file, {\"COMSOL\",\"Info\"}]` gives the additional information stored in the file. 14 | `Import[file, {\"COMSOL\",\"Headers\"}]` gives the hearers of the table. 15 | `Import[file, {\"COMSOL\",\"Parameters\"}]` special import function for importing paraters file. 16 | --------------------------------------------------------------------------------